diff --git a/.travis.yml b/.travis.yml index 9a68c0c..65b8b0a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 \ No newline at end of file diff --git a/README.md b/README.md index 904f26c..4517aca 100644 --- a/README.md +++ b/README.md @@ -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, 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 diff --git a/lib/index.d.ts b/lib/index.d.ts new file mode 100644 index 0000000..4e1affa --- /dev/null +++ b/lib/index.d.ts @@ -0,0 +1,26 @@ +/// + +declare module "node-persistent-software" { + + class PersistantSoftware extends require("asynchronous-eventemitter") { + + protected _software: string; + protected _args: Array; + protected _options: object; + protected _ended: boolean; + + public maxCountRun: number; + public successCountRun: number; + + constructor(software: string, args?: Array, options?: object); + + public max(max: number): PersistantSoftware; + public infinite(): PersistantSoftware; + public start(): PersistantSoftware; + public end(): PersistantSoftware; + + } + + export = PersistantSoftware; + +} diff --git a/lib/main.js b/lib/main.js index 6afd741..302e9ee 100644 --- a/lib/main.js +++ b/lib/main.js @@ -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(); @@ -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 { @@ -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(); @@ -109,14 +109,14 @@ 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 @@ -124,18 +124,11 @@ module.exports = class PersistantSoftware extends require("asynchronous-eventemi } - 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"); } diff --git a/package.json b/package.json index 53c4102..dfbedfe 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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": { diff --git a/tests/0_compilation_typescript.js b/tests/0_compilation_typescript.js new file mode 100644 index 0000000..82b619d --- /dev/null +++ b/tests/0_compilation_typescript.js @@ -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); + +}); diff --git a/tests/tests.js b/tests/tests.js index 65e479b..fca9bcc 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -9,6 +9,7 @@ // consts const IPCONFIG = "win32" === require("os").platform() ? "ipconfig" : "ifconfig"; + const MAX_TIMEOUT = 10000; // tests @@ -36,7 +37,7 @@ describe("run", () => { }); - }).timeout(1 * 1000); + }).timeout(MAX_TIMEOUT); it("should check no args running", () => { @@ -63,7 +64,7 @@ describe("run", () => { }); - }).timeout(1 * 1000); + }).timeout(MAX_TIMEOUT); it("should check normal running with max", () => { @@ -90,7 +91,7 @@ describe("run", () => { }); - }).timeout(1 * 1000); + }).timeout(MAX_TIMEOUT); it("should check normal running with infinite and end", () => { @@ -113,7 +114,7 @@ describe("run", () => { }); - }).timeout(1 * 1000); + }).timeout(MAX_TIMEOUT); it("should check normal running with infinite and end", () => { @@ -151,6 +152,6 @@ describe("run", () => { }); - }).timeout(5 * 1000); + }).timeout(MAX_TIMEOUT); }); diff --git a/tests/typescript/compilation.ts b/tests/typescript/compilation.ts new file mode 100644 index 0000000..f7cd573 --- /dev/null +++ b/tests/typescript/compilation.ts @@ -0,0 +1,44 @@ +/// + +import PersistantSoftware = require("../../lib/main.js"); + +new PersistantSoftware("node", [ "-v" ]).on("error", (err: Error) => { + console.log(err); +}) + +.infinite() + +.on("firststart", () => { + console.log("node is started for the first time !"); +}).on("restart", () => { + console.log("node is started again..."); +}).on("start", (child_process) => { + console.log("anyway, node is started."); +}) + +.on("stop", () => { + console.log("node is stopped, trying to restart..."); +}).on("end", () => { + console.log("/!\\ node is stopped and cannot be restarted /!\\"); +}).start(); + + +new PersistantSoftware("node", [ "-v" ]).on("error", (err: Error) => { + console.log(err); +}) + +.max(5) + +.on("firststart", () => { + console.log("node is started for the first time !"); +}).on("restart", () => { + console.log("node is started again..."); +}).on("start", () => { + console.log("anyway, node is started."); +}) + +.on("stop", () => { + console.log("node is stopped, trying to restart..."); +}).on("end", () => { + console.log("/!\\ node is stopped and cannot be restarted /!\\"); +}).start();