clean-scripts
Advanced tools
Comparing version 1.11.2 to 1.12.0
@@ -16,4 +16,4 @@ /// <reference types="node" /> | ||
script: string; | ||
processKey?: string | undefined; | ||
constructor(script: string, processKey?: string | undefined); | ||
options?: string | Options | undefined; | ||
constructor(script: string, options?: string | Options | undefined); | ||
} | ||
@@ -23,7 +23,21 @@ /** | ||
*/ | ||
export interface Options { | ||
processKey?: string; | ||
/** | ||
* percent | ||
*/ | ||
maximumCpu?: number; | ||
/** | ||
* bytes | ||
*/ | ||
maximumMemory?: number; | ||
} | ||
/** | ||
* @public | ||
*/ | ||
export declare class Program { | ||
script: string; | ||
timeout: number; | ||
processKey?: string | undefined; | ||
constructor(script: string, timeout: number, processKey?: string | undefined); | ||
options?: string | Options | undefined; | ||
constructor(script: string, timeout: number, options?: string | Options | undefined); | ||
} | ||
@@ -30,0 +44,0 @@ /** |
@@ -29,5 +29,5 @@ "use strict"; | ||
class Service { | ||
constructor(script, processKey) { | ||
constructor(script, options) { | ||
this.script = script; | ||
this.processKey = processKey; | ||
this.options = options; | ||
} | ||
@@ -40,6 +40,6 @@ } | ||
class Program { | ||
constructor(script, timeout, processKey) { | ||
constructor(script, timeout, options) { | ||
this.script = script; | ||
this.timeout = timeout; | ||
this.processKey = processKey; | ||
this.options = options; | ||
} | ||
@@ -64,6 +64,15 @@ } | ||
exports.execAsync = util.promisify(childProcess.exec); | ||
async function executeStringScriptAsync(script, context, subProcesses, processKey, timeout) { | ||
const pidusage_1 = tslib_1.__importDefault(require("pidusage")); | ||
// tslint:disable-next-line:cognitive-complexity | ||
async function executeStringScriptAsync(script, context, subProcesses, options) { | ||
return new Promise((resolve, reject) => { | ||
const now = Date.now(); | ||
const subProcess = childProcess.exec(script, { encoding: 'utf8' }, (error, stdout, stderr) => { | ||
let timer; | ||
const cleanTimer = () => { | ||
if (timer) { | ||
clearInterval(timer); | ||
} | ||
}; | ||
const subProcess = childProcess.exec(script, { encoding: 'utf8' }, (error) => { | ||
cleanTimer(); | ||
if (error) { | ||
@@ -82,13 +91,52 @@ reject(error); | ||
} | ||
if (processKey) { | ||
context[processKey] = subProcess; | ||
} | ||
subProcesses.push(subProcess); | ||
if (timeout) { | ||
setTimeout(() => { | ||
resolve(Date.now() - now); | ||
}, timeout); | ||
if (options) { | ||
if (options.processKey) { | ||
context[options.processKey] = subProcess; | ||
} | ||
if (options.maximumCpu || options.maximumMemory) { | ||
timer = setInterval(() => { | ||
pidusage_1.default(subProcess.pid, (error, stats) => { | ||
if (error) { | ||
cleanTimer(); | ||
reject(error); | ||
} | ||
else { | ||
if (options.maximumCpu) { | ||
if (stats.cpu > options.maximumCpu) { | ||
cleanTimer(); | ||
reject(new Error(`cpu ${stats.cpu} should <= ${options.maximumCpu}`)); | ||
} | ||
} | ||
else if (options.maximumMemory) { | ||
// tslint:disable-next-line:no-collapsible-if | ||
if (stats.memory > options.maximumMemory) { | ||
cleanTimer(); | ||
reject(new Error(`memory ${stats.memory} should <= ${options.maximumMemory}`)); | ||
} | ||
} | ||
} | ||
}); | ||
}, 1000); | ||
} | ||
if (options.timeout) { | ||
setTimeout(() => { | ||
cleanTimer(); | ||
resolve(Date.now() - now); | ||
}, options.timeout); | ||
} | ||
} | ||
}); | ||
} | ||
function getOptions(options) { | ||
if (typeof options === 'string') { | ||
return { | ||
processKey: options | ||
}; | ||
} | ||
if (options) { | ||
return options; | ||
} | ||
return undefined; | ||
} | ||
/** | ||
@@ -135,3 +183,3 @@ * @public | ||
const now = Date.now(); | ||
executeStringScriptAsync(script.script, context, subProcesses, script.processKey); | ||
executeStringScriptAsync(script.script, context, subProcesses, getOptions(script.options)); | ||
return [{ time: Date.now() - now, script: script.script }]; | ||
@@ -141,3 +189,6 @@ } | ||
console.log(script.script); | ||
const time = await executeStringScriptAsync(script.script, context, subProcesses, script.processKey, script.timeout); | ||
const time = await executeStringScriptAsync(script.script, context, subProcesses, { | ||
timeout: script.timeout, | ||
...getOptions(script.options) | ||
}); | ||
return [{ time, script: script.script }]; | ||
@@ -144,0 +195,0 @@ } |
@@ -62,7 +62,3 @@ "use strict"; | ||
} | ||
executeCommandLine().then(() => { | ||
cleanup(); | ||
console.log('script success.'); | ||
process.exit(); | ||
}, error => { | ||
function handleError(error) { | ||
if (error instanceof Error) { | ||
@@ -76,3 +72,13 @@ console.log(error.message); | ||
process.exit(1); | ||
} | ||
executeCommandLine().then(() => { | ||
cleanup(); | ||
console.log('script success.'); | ||
process.exit(); | ||
}, error => { | ||
handleError(error); | ||
}); | ||
process.on('unhandledRejection', (error) => { | ||
handleError(error); | ||
}); | ||
function collectPids(pid, ps, result) { | ||
@@ -79,0 +85,0 @@ const children = ps.filter(p => p.ppid === pid); |
{ | ||
"name": "clean-scripts", | ||
"version": "1.11.2", | ||
"version": "1.12.0", | ||
"description": "A CLI tool to make scripts in package.json clean.", | ||
@@ -33,3 +33,4 @@ "main": "dist/core.js", | ||
"@types/minimist": "1.2.0", | ||
"@types/node": "12.0.10", | ||
"@types/node": "12.6.2", | ||
"@types/pidusage": "2.0.1", | ||
"@types/pretty-ms": "4.0.0", | ||
@@ -39,5 +40,5 @@ "clean-release": "2.7.0", | ||
"markdownlint-cli": "0.17.0", | ||
"no-unused-export": "1.7.0", | ||
"no-unused-export": "1.9.0", | ||
"rimraf": "2.6.3", | ||
"standard": "12.0.1", | ||
"standard": "13.0.2", | ||
"tslint": "5.18.0", | ||
@@ -47,6 +48,7 @@ "tslint-config-standard": "8.0.1", | ||
"type-coverage": "2.0.4", | ||
"typescript": "3.5.2" | ||
"typescript": "3.5.3" | ||
}, | ||
"dependencies": { | ||
"minimist": "1.2.0", | ||
"pidusage": "^2.0.17", | ||
"pretty-ms": "5.0.0", | ||
@@ -53,0 +55,0 @@ "tslib": "1" |
@@ -159,3 +159,4 @@ # clean-scripts | ||
new Service('http-server -p 8000'), | ||
new Service('http-server', 'server2') // the child process can be accessed by `context.server2` later | ||
new Service('http-server', 'server2'), // the child process can be accessed by `context.server2` later | ||
new Service('http-server -p 8000', { maximumCpu: 50, maximumMemory: 1175552 }), // if the cpu usage of this service > maximumCpu, throw an error. same to the memory usage | ||
] | ||
@@ -167,2 +168,4 @@ } | ||
The cpu and memory check runs every 1 second. | ||
### start program | ||
@@ -175,3 +178,4 @@ | ||
build: [ | ||
new Program('http-server -p 8000', 10000) // the program will last at most 10 seconds, can be used to test the start process of a program | ||
new Program('http-server -p 8000', 10000), // the program will last at most 10 seconds, can be used to test the start process of a program | ||
new Program('http-server -p 8000', 10000, { maximumCpu: 50, maximumMemory: 1175552 }), // if the cpu usage of this program > maximumCpu, throw an error. same to the memory usage | ||
] | ||
@@ -183,2 +187,4 @@ } | ||
The cpu and memory check runs every 1 second. | ||
### tasks | ||
@@ -185,0 +191,0 @@ |
23519
454
262
4
18
+ Addedpidusage@^2.0.17
+ Addedpidusage@2.0.21(transitive)
+ Addedsafe-buffer@5.2.1(transitive)