Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
language: node_js
node_js:
- "6"
- "8"
install:
- "npm install -g typescript"
- "npm install"
script: "npm run-script tests"
sudo: false
78 changes: 40 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,90 +15,92 @@ $ npm install node-persistent-software

### Attributes

* ``` string software ``` path to the software
* ``` array arguments ``` arguments passed to the software
* ``` object options ``` options passed to the software
* ``` object currentChildProcess ``` current child_process
* ``` boolean ended ``` if ended, don't restart it
* ``` int maxCountRun ``` max start iteration
* ``` int successCountRun ``` current success start iteration
* ``` asynchronous-eventemitter eventEmitter ``` async events manager
* ``` maxCountRun: number ``` max start iteration
* ``` successCountRun: number ``` current success start iteration

### Constructor

* ``` constructor(string software [, array arguments [, object options ] ] ) ``` => see [spawn documentation](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options)
* ``` constructor(software: string, args?: Array<string>, options?: object) ``` => see [spawn documentation](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options)

### Methods

* ``` max(int maxIteration) : this ``` change max iterations and reset current
* ``` max(maxIteration: number) : this ``` change max iterations and reset current
* ``` infinite() : this ``` no max iteration and reset current
* ``` start() : this ``` run the software for the first time
* ``` end() : this ``` stop the software and don't restart it
* ``` end() : this ``` stop the software and does not restart it

### Events

* ``` on("error", (string err) => {}) : this ``` fire if an error occurs (use try/catch to avoid loop)
* ``` on("error", (err: Error) => void) : this ``` fire if an error occurs (use try/catch to avoid loop)

* ``` on("firststart", () => {}) : this ``` fire if the software starts for the first time
* ``` on("restart", () => {}) : this ``` fire if the software restarts
* ``` on("start", (object child_process) => {}) : this ``` fire if the software starts (firststart && restart) => see [spawn documentation](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options)
* ``` on("firststart", () => void) : this ``` fire if the software starts for the first time
* ``` on("restart", () => void) : this ``` fire if the software restarts
* ``` on("start", (child_process: child_process.ChildProcess) => void) : this ``` fire if the software starts (firststart && restart) => see [spawn documentation](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options)

* ``` on("stop", () => {}) : this ``` fire if the software is killed
* ``` on("end", () => {}) : this ``` fire if the software is killed and cannot be restarted
* ``` on("stop", () => void) : this ``` fire if the software is killed
* ``` on("end", () => void) : this ``` fire if the software is killed and cannot be restarted

## Examples

```js
const PersistantSoftware = require('node-persistent-software');
### Native

new PersistantSoftware(
"C:\\Program Files\\Mozilla Firefox\\firefox.exe",
[ "https://www.npmjs.com/package/node-persistent-software" ]
).on("error", (msg) => {
```javascript
const PersistantSoftware = require("node-persistent-software");

new PersistantSoftware("node", [ "-v" ]).on("error", (msg) => {
console.log(msg);
})

.infinite()

.on("firststart", () => {
console.log("Firefox is started for the first time !");
console.log("node is started for the first time !");
}).on("restart", () => {
console.log("Firefox is started again...");
console.log("node is started again...");
}).on("start", (child_process) => {
console.log("anyway, Firefox is started.");
console.log("anyway, node is started.");
})

.on("stop", () => {
console.log("Firefox is stopped, trying to restart...");
console.log("node is stopped, trying to restart...");
}).on("end", () => {
console.log("/!\\ Firefox is stopped and cannot be restarted /!\\");
console.log("/!\\ node is stopped and cannot be restarted /!\\");
}).start();


new PersistantSoftware(
"C:\\Program Files\\Mozilla Firefox\\firefox.exe",
[ "https://github.com/Psychopoulet/node-persistent-software" ]
).on("error", (msg) {
console.log(msg);
new PersistantSoftware("node", [ "-v" ]).on("error", (err) => {
console.log(err);
})

.max(5)

.on("firststart", () => {
console.log("Firefox is started for the first time !");
console.log("node is started for the first time !");
}).on("restart", () => {
console.log("Firefox is started again...");
console.log("node is started again...");
}).on("start", () => {
console.log("anyway, Firefox is started.");
console.log("anyway, node is started.");
})

.on("stop", () => {
console.log("Firefox is stopped, trying to restart...");
console.log("node is stopped, trying to restart...");
}).on("end", () => {
console.log("/!\\ Firefox is stopped and cannot be restarted /!\\");
console.log("/!\\ node is stopped and cannot be restarted /!\\");
}).start();
```

### Typescript

```typescript
import PersistantSoftware = require("node-persistent-software");

new PersistantSoftware("node", [ "-v" ]).on("error", (err) => {
console.log(err);
})

.infinite();
```

## Tests

```bash
Expand Down
26 changes: 26 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/// <reference types="node" />

declare module "node-persistent-software" {

class PersistantSoftware extends require("asynchronous-eventemitter") {

protected _software: string;
protected _args: Array<string>;
protected _options: object;
protected _ended: boolean;

public maxCountRun: number;
public successCountRun: number;

constructor(software: string, args?: Array<string>, options?: object);

public max(max: number): PersistantSoftware;
public infinite(): PersistantSoftware;
public start(): PersistantSoftware;
public end(): PersistantSoftware;

}

export = PersistantSoftware;

}
53 changes: 23 additions & 30 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@

module.exports = class PersistantSoftware extends require("asynchronous-eventemitter") {

constructor (software, args, options) {
constructor (software, args = null, options = null) {

super();

this.software = software;
this.args = args && "object" === typeof args && args instanceof Array ? args : null;
this.options = options && "object" === typeof options ? options : null;
this._software = software;
this._args = args && "object" === typeof args && args instanceof Array ? args : null;
this._options = options && "object" === typeof options ? options : null;

this.currentChildProcess = null;
this._currentChildProcess = null;

this.ended = false;
this._ended = false;

this.infinite();

Expand All @@ -42,23 +42,23 @@ module.exports = class PersistantSoftware extends require("asynchronous-eventemi

try {

if (!this.ended) {
if (!this._ended) {

if (!this.args) {
this.currentChildProcess = spawn(this.software);
if (!this._args) {
this._currentChildProcess = spawn(this._software);
}
else if (!this.options) {
this.currentChildProcess = spawn(this.software, this.args);
else if (!this._options) {
this._currentChildProcess = spawn(this._software, this._args);
}
else {
this.currentChildProcess = spawn(this.software, this.args, this.options);
this._currentChildProcess = spawn(this._software, this._args, this._options);
}

this.currentChildProcess.on("error", (err) => {
this._currentChildProcess.on("error", (err) => {
this.emit("error", err);
});

if (!this.currentChildProcess || !this.currentChildProcess.pid) {
if (!this._currentChildProcess || !this._currentChildProcess.pid) {
this.end();
}
else {
Expand All @@ -72,13 +72,13 @@ module.exports = class PersistantSoftware extends require("asynchronous-eventemi
this.emit("firststart");
}

this.emit("start", this.currentChildProcess);
this.emit("start", this._currentChildProcess);

this.currentChildProcess.on("exit", () => {
this._currentChildProcess.on("exit", () => {

this.emit("stop");

if (!this.ended) {
if (!this._ended) {

if (0 >= this.maxCountRun) {
this.start();
Expand Down Expand Up @@ -109,33 +109,26 @@ module.exports = class PersistantSoftware extends require("asynchronous-eventemi

end () {

this.ended = true;
this._ended = true;

if (this.currentChildProcess) {
if (this._currentChildProcess) {

if (this.currentChildProcess.pid) {
if (this._currentChildProcess.pid) {

try {
process.kill(this.currentChildProcess.pid);
process.kill(this._currentChildProcess.pid);
}
catch (e) {
// nothing to do here
}

}

this.currentChildProcess = null;
this._currentChildProcess = null;

}

this.emit("end")
.removeAllListeners("start")
.removeAllListeners("firststart")
.removeAllListeners("restart")
.removeAllListeners("stop")
.removeAllListeners("end");

return this;
return this.emit("end");

}

Expand Down
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "node-persistent-software",
"version": "1.3.0",
"version": "1.4.0",
"description": "Spawn a software and keep it running",
"main": "lib/main.js",
"typings": "lib/index.d.ts",
"scripts": {
"start": "node lib/main.js",
"tests": "gulp tests"
},
"pre-commit": [
"test"
"pre-push": [
"tests"
],
"repository": {
"type": "git",
Expand All @@ -28,16 +28,17 @@
"url": "https://github.com/Psychopoulet/node-persistent-software/issues"
},
"dependencies": {
"asynchronous-eventemitter": "0.3.0"
"asynchronous-eventemitter": "0.3.2"
},
"devDependencies": {
"@types/node": "9.6.6",
"gulp": "3.9.1",
"gulp-plumber": "1.1.0",
"gulp-eslint": "4.0.0",
"gulp-mocha": "3.0.1",
"gulp-istanbul": "1.1.2",
"gulp-coveralls": "0.1.4",
"pre-commit": "1.2.2"
"pre-push": "0.1.1"
},
"homepage": "https://github.com/Psychopoulet/node-persistent-software#readme",
"engines": {
Expand Down
37 changes: 37 additions & 0 deletions tests/0_compilation_typescript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

"use strict";

// deps

const { exec } = require("child_process");
const { join } = require("path");
const { unlink } = require("fs");

// consts

const MAX_TIMEOUT = 10000;

// tests

describe("compilation typescript", () => {

after((done) => {

unlink(join(__dirname, "typescript", "compilation.js"), (err) => {
return err ? done(err) : done();
});

});

it("should compile typescript file", (done) => {

exec("tsc " + join(__dirname, "typescript", "compilation.ts"), {
"cwd": join(__dirname, ".."),
"windowsHide": true
}, (err) => {
return err ? done(err) : done();
});

}).timeout(MAX_TIMEOUT);

});
Loading