clean-scripts
Advanced tools
Comparing version 1.4.2 to 1.5.0
@@ -19,1 +19,6 @@ /// <reference types="node" /> | ||
} | ||
import * as childProcess from "child_process"; | ||
/** | ||
* @public | ||
*/ | ||
export declare const execAsync: typeof childProcess.exec.__promisify__; |
@@ -34,1 +34,7 @@ "use strict"; | ||
exports.Service = Service; | ||
const childProcess = require("child_process"); | ||
const util = require("util"); | ||
/** | ||
* @public | ||
*/ | ||
exports.execAsync = util.promisify(childProcess.exec); |
@@ -26,17 +26,20 @@ "use strict"; | ||
const scriptNames = argv._; | ||
for (const scriptName of scriptNames) { | ||
// tslint:disable-next-line:no-eval | ||
const scriptValues = scripts[scriptName] || eval("scripts." + scriptName); | ||
if (!scriptValues) { | ||
throw new Error(`Unknown script name: ${scriptName}`); | ||
} | ||
const times = await executeScript(scriptValues); | ||
const totalTime = times.reduce((p, c) => p + c.time, 0); | ||
printInConsole(`----------------total: ${prettyMs(totalTime)}----------------`); | ||
for (const { time, script } of times) { | ||
const pecent = Math.round(100.0 * time / totalTime); | ||
printInConsole(`${prettyMs(time)} ${pecent}% ${script}`); | ||
} | ||
printInConsole(`----------------total: ${prettyMs(totalTime)}----------------`); | ||
if (!scriptNames || scriptNames.length === 0) { | ||
throw new Error(`Need a script`); | ||
} | ||
const scriptName = scriptNames[0]; | ||
const parameters = scriptNames.slice(1); | ||
// tslint:disable-next-line:no-eval | ||
const scriptValues = scripts[scriptName] || eval("scripts." + scriptName); | ||
if (!scriptValues) { | ||
throw new Error(`Unknown script name: ${scriptName}`); | ||
} | ||
const times = await executeScript(scriptValues, parameters); | ||
const totalTime = times.reduce((p, c) => p + c.time, 0); | ||
printInConsole(`----------------total: ${prettyMs(totalTime)}----------------`); | ||
for (const { time, script } of times) { | ||
const pecent = Math.round(100.0 * time / totalTime); | ||
printInConsole(`${prettyMs(time)} ${pecent}% ${script}`); | ||
} | ||
printInConsole(`----------------total: ${prettyMs(totalTime)}----------------`); | ||
} | ||
@@ -66,3 +69,3 @@ const serviceProcesses = []; | ||
} | ||
async function executeScript(script) { | ||
async function executeScript(script, parameters) { | ||
if (typeof script === "string") { | ||
@@ -76,3 +79,3 @@ printInConsole(script); | ||
for (const child of script) { | ||
const time = await executeScript(child); | ||
const time = await executeScript(child, parameters); | ||
times.push(...time); | ||
@@ -85,3 +88,3 @@ } | ||
for (const child of script) { | ||
promises.push(executeScript(child)); | ||
promises.push(executeScript(child, parameters)); | ||
} | ||
@@ -108,4 +111,4 @@ const times = await Promise.all(promises); | ||
const now = Date.now(); | ||
await script(context); | ||
return [{ time: Date.now() - now, script: "Custom Promise" }]; | ||
await script(context, parameters); | ||
return [{ time: Date.now() - now, script: script.name || "custom function script" }]; | ||
} | ||
@@ -116,3 +119,3 @@ else { | ||
if (script.hasOwnProperty(key)) { | ||
promises.push(executeScript(script[key])); | ||
promises.push(executeScript(script[key], parameters)); | ||
} | ||
@@ -138,3 +141,2 @@ } | ||
printInConsole("script success."); | ||
process.exit(); | ||
}, error => { | ||
@@ -141,0 +143,0 @@ printInConsole(error); |
{ | ||
"name": "clean-scripts", | ||
"version": "1.4.2", | ||
"version": "1.5.0", | ||
"description": "A CLI tool to make scripts in package.json clean.", | ||
@@ -29,3 +29,3 @@ "main": "dist/core.js", | ||
"@types/minimist": "1.2.0", | ||
"@types/node": "8.0.33", | ||
"@types/node": "8.0.46", | ||
"@types/pretty-ms": "3.0.0", | ||
@@ -32,0 +32,0 @@ "clean-release": "1.3.5", |
@@ -88,3 +88,3 @@ [![Dependency Status](https://david-dm.org/plantain-00/clean-scripts.svg)](https://david-dm.org/plantain-00/clean-scripts) | ||
the type of the function should be `(context: { [key: string]: any }) => Promise<void>` | ||
the type of the function should be `(context: { [key: string]: any }, parameters: string[]) => Promise<void>` | ||
@@ -121,2 +121,13 @@ ```js | ||
The `parameters` can be passed from CLI parameters | ||
```js | ||
module.exports = { | ||
build: (context, parameters) => { | ||
console.log(parameters) // run `clean-scripts build foo bar`, got `["foo", "bar"]` | ||
return Promise.resolve() | ||
} | ||
} | ||
``` | ||
##### child script | ||
@@ -158,3 +169,3 @@ | ||
```js | ||
const { sleep, readableStreamEnd } = require('clean-scripts') | ||
const { sleep, readableStreamEnd, execAsync } = require('clean-scripts') | ||
@@ -170,2 +181,9 @@ module.exports = { | ||
await readableStreamEnd(readable) | ||
}, | ||
async () => { | ||
const { stdout } = await execAsync('git status -s') | ||
if (stdout) { | ||
console.log(stdout) | ||
throw new Error(`generated files doesn't match.`) | ||
} | ||
} | ||
@@ -172,0 +190,0 @@ ] |
13016
203
189
3