clean-scripts
Advanced tools
Comparing version 1.9.2 to 1.10.0
@@ -16,3 +16,3 @@ /// <reference types="node" /> | ||
script: string; | ||
processKey: string | undefined; | ||
processKey?: string | undefined; | ||
constructor(script: string, processKey?: string | undefined); | ||
@@ -26,5 +26,20 @@ } | ||
timeout: number; | ||
processKey: string | undefined; | ||
processKey?: string | undefined; | ||
constructor(script: string, timeout: number, processKey?: string | undefined); | ||
} | ||
/** | ||
* @public | ||
*/ | ||
export declare class Tasks { | ||
tasks: Task[]; | ||
constructor(tasks: Task[]); | ||
} | ||
/** | ||
* @public | ||
*/ | ||
export interface Task { | ||
name: string; | ||
script: Script; | ||
dependencies?: string[]; | ||
} | ||
import * as childProcess from 'child_process'; | ||
@@ -40,3 +55,3 @@ /** | ||
[key: string]: any; | ||
}, parameters: string[]) => Promise<void>) | any[] | Set<any> | Service | { | ||
}, parameters: string[]) => Promise<void>) | any[] | Set<any> | Service | Program | Tasks | { | ||
[name: string]: any; | ||
@@ -43,0 +58,0 @@ } | null | undefined; |
@@ -46,2 +46,11 @@ "use strict"; | ||
exports.Program = Program; | ||
/** | ||
* @public | ||
*/ | ||
class Tasks { | ||
constructor(tasks) { | ||
this.tasks = tasks; | ||
} | ||
} | ||
exports.Tasks = Tasks; | ||
const childProcess = tslib_1.__importStar(require("child_process")); | ||
@@ -64,4 +73,8 @@ const util = tslib_1.__importStar(require("util")); | ||
}); | ||
subProcess.stdout.pipe(process.stdout); | ||
subProcess.stderr.pipe(process.stderr); | ||
if (subProcess.stdout) { | ||
subProcess.stdout.pipe(process.stdout); | ||
} | ||
if (subProcess.stderr) { | ||
subProcess.stderr.pipe(process.stderr); | ||
} | ||
if (processKey) { | ||
@@ -132,2 +145,26 @@ context[processKey] = subProcess; | ||
} | ||
else if (script instanceof Tasks) { | ||
let remainTasks = script.tasks; | ||
let currentTasks = []; | ||
const execuateTasks = async () => { | ||
let tasks = getTasks(remainTasks, currentTasks); | ||
currentTasks.push(...tasks.current); | ||
if (tasks.current.length > 0) { | ||
remainTasks = tasks.remain; | ||
const times = await Promise.all(tasks.current.map(async (c) => { | ||
const time = await executeScriptAsync(c.script, parameters, context, subProcesses); | ||
currentTasks = currentTasks.filter((r) => r !== c); | ||
const newTimes = await execuateTasks(); | ||
return [...time, ...newTimes]; | ||
})); | ||
return getLongestTime(times); | ||
} | ||
return []; | ||
}; | ||
const times = await execuateTasks(); | ||
if (remainTasks.length > 0) { | ||
console.warn(`remain ${remainTasks.length} tasks: ${remainTasks.map((r) => r.name).join(', ')}`); | ||
} | ||
return times; | ||
} | ||
else { | ||
@@ -141,15 +178,33 @@ const promises = []; | ||
const times = await Promise.all(promises); | ||
let result = []; | ||
let maxTotalTime = 0; | ||
for (const time of times) { | ||
const totalTime = time.reduce((p, c) => p + c.time, 0); | ||
if (totalTime > maxTotalTime) { | ||
result = time; | ||
maxTotalTime = totalTime; | ||
} | ||
return getLongestTime(times); | ||
} | ||
} | ||
exports.executeScriptAsync = executeScriptAsync; | ||
function getLongestTime(times) { | ||
let result = []; | ||
let maxTotalTime = 0; | ||
for (const time of times) { | ||
const totalTime = time.reduce((p, c) => p + c.time, 0); | ||
if (totalTime > maxTotalTime) { | ||
result = time; | ||
maxTotalTime = totalTime; | ||
} | ||
return result; | ||
} | ||
return result; | ||
} | ||
exports.executeScriptAsync = executeScriptAsync; | ||
function getTasks(remainTasks, currentTasks) { | ||
const current = []; | ||
const remain = []; | ||
for (const task of remainTasks) { | ||
if (task.dependencies | ||
&& task.dependencies.length > 0 | ||
&& task.dependencies.some((d) => remainTasks.some((t) => t.name === d) || currentTasks.some((t) => t.name === d))) { | ||
remain.push(task); | ||
} | ||
else { | ||
current.push(task); | ||
} | ||
} | ||
return { current, remain }; | ||
} | ||
const fs = tslib_1.__importStar(require("fs")); | ||
@@ -156,0 +211,0 @@ /** |
@@ -23,2 +23,3 @@ "use strict"; | ||
} | ||
// tslint:disable-next-line:max-union-size | ||
const scripts = require(path.resolve(process.cwd(), argv.config || defaultConfigName)); | ||
@@ -25,0 +26,0 @@ const scriptNames = argv._; |
{ | ||
"name": "clean-scripts", | ||
"version": "1.9.2", | ||
"version": "1.10.0", | ||
"description": "A CLI tool to make scripts in package.json clean.", | ||
@@ -24,25 +24,29 @@ "main": "dist/core.js", | ||
}, | ||
"typeCoverage": { | ||
"atLeast": 97 | ||
}, | ||
"homepage": "https://github.com/plantain-00/clean-scripts#readme", | ||
"devDependencies": { | ||
"@commitlint/cli": "6.2.0", | ||
"@commitlint/config-conventional": "6.1.3", | ||
"@types/jasmine": "2.8.7", | ||
"@commitlint/cli": "7.5.2", | ||
"@commitlint/config-conventional": "7.5.0", | ||
"@types/jasmine": "3.3.12", | ||
"@types/minimist": "1.2.0", | ||
"@types/node": "10.0.8", | ||
"@types/pretty-ms": "3.0.0", | ||
"clean-release": "2.5.0", | ||
"jasmine": "3.1.0", | ||
"markdownlint-cli": "0.8.1", | ||
"no-unused-export": "1.5.0", | ||
"rimraf": "2.6.2", | ||
"standard": "11.0.1", | ||
"tslint": "5.10.0", | ||
"tslint-config-standard": "7.0.0", | ||
"tslint-sonarts": "1.6.0", | ||
"typescript": "2.8.3" | ||
"@types/node": "11.13.8", | ||
"@types/pretty-ms": "4.0.0", | ||
"clean-release": "2.7.0", | ||
"jasmine": "3.4.0", | ||
"markdownlint-cli": "0.15.0", | ||
"no-unused-export": "1.7.0", | ||
"rimraf": "2.6.3", | ||
"standard": "12.0.1", | ||
"tslint": "5.16.0", | ||
"tslint-config-standard": "8.0.1", | ||
"tslint-sonarts": "1.9.0", | ||
"type-coverage": "1.13.0", | ||
"typescript": "3.4.5" | ||
}, | ||
"dependencies": { | ||
"minimist": "1.2.0", | ||
"pretty-ms": "3.1.0", | ||
"tslib": "1.9.1" | ||
"pretty-ms": "5.0.0", | ||
"tslib": "1.9.3" | ||
}, | ||
@@ -49,0 +53,0 @@ "bin": { |
@@ -9,2 +9,3 @@ # clean-scripts | ||
[![Downloads](https://img.shields.io/npm/dm/clean-scripts.svg)](https://www.npmjs.com/package/clean-scripts) | ||
[![type-coverage](https://img.shields.io/badge/dynamic/json.svg?label=type-coverage&prefix=%E2%89%A5&suffix=%&query=$.typeCoverage.atLeast&uri=https%3A%2F%2Fraw.githubusercontent.com%2Fplantain-00%2Fclean-scripts%2Fmaster%2Fpackage.json)](https://github.com/plantain-00/clean-scripts) | ||
@@ -180,2 +181,46 @@ A CLI tool to make scripts in package.json clean. | ||
### tasks | ||
```js | ||
const { Tasks } = require('clean-scripts') | ||
module.exports = { | ||
build: new Tasks([ | ||
{ | ||
name: 'build a', | ||
script: 'yarn workspace a run build' | ||
}, | ||
{ | ||
name: 'test a', | ||
script: 'yarn workspace a run test', | ||
dependencies: [ | ||
'build a' | ||
] | ||
}, | ||
{ | ||
name: 'build b', | ||
script: 'yarn workspace b run build', | ||
dependencies: [ | ||
'build a' | ||
] | ||
}, | ||
{ | ||
name: 'test b', | ||
script: 'yarn workspace b run test', | ||
dependencies: [ | ||
'build b' | ||
] | ||
} | ||
]) | ||
} | ||
``` | ||
the 4 tasks will be execuated in following order: | ||
1. `build a` | ||
2. `build b` and `test a` | ||
3. `test b` as soon as `build b` completed | ||
This can be very useful and effective for complex or dynamic tasks. | ||
### short-hand methods | ||
@@ -182,0 +227,0 @@ |
20534
379
256
17
+ Addedparse-ms@2.1.0(transitive)
+ Addedpretty-ms@5.0.0(transitive)
+ Addedtslib@1.9.3(transitive)
- Removedirregular-plurals@1.4.0(transitive)
- Removedparse-ms@1.0.1(transitive)
- Removedplur@2.1.2(transitive)
- Removedpretty-ms@3.1.0(transitive)
- Removedtslib@1.9.1(transitive)
Updatedpretty-ms@5.0.0
Updatedtslib@1.9.3