From b68d8b9e06d4e5d7111dbd5bda478e07388a28fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=B3=96=E9=A5=BC?= Date: Mon, 12 Jun 2017 16:46:15 +0800 Subject: [PATCH 1/4] dependencies npm-run-path --- CHANGELOG.md | 0 README.ZH-CN.md | 2 +- README.md | 2 +- lib/worker.js | 115 ++++++++++++++++------------------------ package.json | 5 +- test/lib/worker.test.js | 19 ++++--- 6 files changed, 61 insertions(+), 82 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..e69de29 diff --git a/README.ZH-CN.md b/README.ZH-CN.md index d0fac9a..9870f0d 100644 --- a/README.ZH-CN.md +++ b/README.ZH-CN.md @@ -172,7 +172,7 @@ tasks 最外层的任务名是串行运行,如果遇到数组则会并行运 } ``` -上述例子中:当 dll 构建完成后,mod1、mod2、mod3 会以多线程的方式并行构建。 +上述例子中:当 dll 构建完成后,mod1、mod2、mod3 会以多进程的方式并行构建。 ### 依赖变更触发构建 diff --git a/README.md b/README.md index 3bdaa2c..bdec8ad 100644 --- a/README.md +++ b/README.md @@ -171,7 +171,7 @@ Tasks outside task name is serial run, if array will parallel running: } ``` -Above-mentioned: when dll has build, mod1、mod2、mod3 will parallel building by multi-thread. +Above-mentioned: when dll has build, mod1、mod2、mod3 will parallel building by mutiprocess. ### Change Dependencies Trigger Buliding diff --git a/lib/worker.js b/lib/worker.js index da7dfef..1c2fa67 100644 --- a/lib/worker.js +++ b/lib/worker.js @@ -1,10 +1,6 @@ -const fs = require('fs'); -const path = require('path'); const childProcess = require('child_process'); const defaultsDeep = require('lodash.defaultsdeep'); -const promiseify = require('./promiseify'); -const access = promiseify(fs.access); -const WORKER_ENV = 'WORKER_ENV'; +const npmRunPath = require('npm-run-path'); /** @@ -18,84 +14,67 @@ module.exports = (command, options = {}) => { return new Promise((resolve, reject) => { - // 子进程继承父进程的环境变量 options = defaultsDeep({}, options, { - env: process.env + // 子进程继承父进程的环境变量 + // 使用 npm 的 PATH + env: npmRunPath.env({ + cwd: options.cwd || process.cwd() + }) }); - options.env[WORKER_ENV] = '1'; - - - // 将 node_modules/.bin 加入环境变量,优先使用本地模块执行命令 - let addPath = cwd => { - let env = options.env; - if (cwd && env) { - let localBin = path.resolve(cwd, 'node_modules', '.bin'); - if (env.PATH && !env.PATH.includes(localBin)) { - return access(localBin).then(() => { - env.PATH = localBin + ':' + env.PATH; - }).catch(() => {}); - } - } - }; - Promise.all([ - addPath(process.cwd()), - addPath(options.cwd) - ]).then(() => { - let child; - let closed; - let timer = null; + let child; + let closed; + let timer = null; - try { - child = childProcess.exec(command, options); - } catch (errors) { - callback(errors); - } + try { + child = childProcess.exec(command, options); + } catch (errors) { + callback(errors); + } - child.stdout.on('data', content => { - process.stdout.write(content); - }); + child.stdout.on('data', content => { + process.stdout.write(content); + }); - child.stderr.on('data', content => { - process.stderr.write(content); - }); + child.stderr.on('data', content => { + process.stderr.write(content); + }); - child.on('exit', code => { - if (code === 0) { - callback(null); - } else { - callback(new Error(`exec "${command}": child process exited with code ${code}`)); - } - }); + child.on('exit', code => { + if (code === 0) { + callback(null); + } else { + callback(new Error(`exec "${command}": child process exited with code ${code}`)); + } + }); - // 解决 node timeout 不会走异常流程的问题 - if (options.timeout) { - timer = setTimeout(() => { - callback(new Error(`exec "${command}": child process timeout`)); - child.kill(); - }, options.timeout); - } + // 解决 node timeout 不会走异常流程的问题 + if (options.timeout) { + timer = setTimeout(() => { + callback(new Error(`exec "${command}": child process timeout`)); + child.kill(); + }, options.timeout); + } - function callback(errors, data) { - if (closed) { - return; - } else { - closed = true; - } + function callback(errors, data) { + if (closed) { + return; + } else { + closed = true; + } - clearTimeout(timer); + clearTimeout(timer); - if (errors) { - errors.messsage = `exec "${command}": ${errors.messsage}`; - reject(errors); - } else { - resolve(data); - } + if (errors) { + errors.messsage = `exec "${command}": ${errors.messsage}`; + reject(errors); + } else { + resolve(data); } - }).catch(reject); + } }); }; \ No newline at end of file diff --git a/package.json b/package.json index dcde77f..6fbdb3e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ci-task-runner", - "version": "1.0.0", + "version": "1.0.1", "description": "在 CI 上运行的、支持增量与多进程并行构建任务调度程序", "main": "src/index.js", "bin": "bin/ci-task-runner", @@ -39,7 +39,8 @@ "homepage": "https://github.com/aui/ci-task-runner#readme", "dependencies": { "commander": "^2.9.0", - "lodash.defaultsdeep": "^4.6.0" + "lodash.defaultsdeep": "^4.6.0", + "npm-run-path": "^2.0.2" }, "devDependencies": { "jquery": "^3.1.1", diff --git a/test/lib/worker.test.js b/test/lib/worker.test.js index d1f48b1..3581ae0 100644 --- a/test/lib/worker.test.js +++ b/test/lib/worker.test.js @@ -7,16 +7,15 @@ describe('#worker', () => { return worker('echo "hello"'); }); - it('childProcess.exec error', () => { - return worker("", { - cwd: null, - env: null - }).then(() => { - throw new Error('error'); - }, errors => { - assert.equal('object', typeof errors); - }); - }); + // it('childProcess.exec error', () => { + // return worker("", { + // cwd: {} + // }).then(() => { + // throw new Error('error'); + // }, errors => { + // assert.equal('object', typeof errors); + // }); + // }); it('exec error', () => { return worker(`@error%`, { From 21bb2797e6ad4e88a3f314e80a668c5d44900d4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=B3=96=E9=A5=BC?= Date: Thu, 15 Jun 2017 11:29:38 +0800 Subject: [PATCH 2/4] improved cache --- CHANGELOG.md | 4 ++++ README.ZH-CN.md | 8 +------- README.md | 8 +------- bin/ci-task-runner | 1 - example/.ci-task-runner.json | 1 - package.json | 3 ++- src/config/config.default.json | 1 - src/index.js | 8 +++++++- test/file/parallel/.ci-task-runner.json | 1 - test/file/tasks/.ci-task-runner.json | 1 - 10 files changed, 15 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e69de29..3653ac7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -0,0 +1,4 @@ +## v1.1.0 + +1. 使用 `npm-run-path` 来设置环境变量,避免 +2. `options.cache` 如果没有设置,则在 node_modules/.cache/ci-task-runner 中添加缓存 \ No newline at end of file diff --git a/README.ZH-CN.md b/README.ZH-CN.md index 9870f0d..b5bb7f8 100644 --- a/README.ZH-CN.md +++ b/README.ZH-CN.md @@ -37,7 +37,6 @@ ci-task-runner 的任务都是在 JSON 配置文件定义的,在项目中新 ```json { "tasks": ["mod1", "mod2", "mod3"], - "cache": ".ci-task-runner-cache.json", "repository": "git", "program": "cd ${taskPath} && webpack --color" } @@ -91,9 +90,7 @@ ci-task-runner ### `cache` -ci-task-runner 缓存文件写入路径,用来保存上一次任务的信息。默认为:`.ci-task-runner-cache.json` - -> 请在代码仓库库中忽略 `.ci-task-runner-cache.json`。 +ci-task-runner 缓存文件写入路径,用来保存上一次任务的信息。默认为:`node_modules/.cache/ci-task-runner/${Package.version}.json` ### `dependencies` @@ -166,7 +163,6 @@ tasks 最外层的任务名是串行运行,如果遇到数组则会并行运 ```json { "tasks": ["dll", ["mod1", "mod2", "mod3"]], - "cache": "dist/.ci-task-runner-cache.json", "repository": "git", "program": "cd ${taskPath} && webpack --color" } @@ -180,7 +176,6 @@ tasks 最外层的任务名是串行运行,如果遇到数组则会并行运 { "tasks": ["dll", ["mod1", "mod2", "mod3"]], "dependencies": ["dll", "package.json"], - "cache": "dist/.ci-task-runner-cache.json", "repository": "git", "program": "cd ${taskPath} && webpack --color" } @@ -201,7 +196,6 @@ tasks 最外层的任务名是串行运行,如果遇到数组则会并行运 ["mod1", "mod2", "mod3"] ], "dependencies": ["package.json", "dll"], - "cache": "dist/.ci-task-runner-cache.json", "repository": "git", "program": "cd ${taskPath} && webpack --color" } diff --git a/README.md b/README.md index bdec8ad..d133e0e 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,6 @@ Ci-task-runner's tasks are defined in the JSON, in the project to create a new ` ```json { "tasks": ["mod1", "mod2", "mod3"], - "cache": ".ci-task-runner-cache.json", "repository": "git", "program": "cd ${taskPath} && webpack --color" } @@ -90,9 +89,7 @@ Advanced: `{Object[]}` 2. [`tasks`](#tasks) support configure parallel tasks, reference: [Mutiprocess Parallel Tasks](#mutiprocess-parallel-tasks). ### `cache` -ci-task-runner cache files write path, used to save the last task info. Default: `ci-task-runner-cache.json` - -> Ignore `.ci-task-runner-cache.json` in repository. +ci-task-runner cache files write path, used to save the last task info. Default: `node_modules/.cache/ci-task-runner/${Package.version}.json` ### `dependencies` @@ -165,7 +162,6 @@ Tasks outside task name is serial run, if array will parallel running: ```json { "tasks": ["dll", ["mod1", "mod2", "mod3"]], - "cache": "dist/.ci-task-runner-cache.json", "repository": "git", "program": "cd ${taskPath} && webpack --color" } @@ -179,7 +175,6 @@ Above-mentioned: when dll has build, mod1、mod2、mod3 will parallel building b { "tasks": ["dll", ["mod1", "mod2", "mod3"]], "dependencies": ["dll", "package.json"], - "cache": "dist/.ci-task-runner-cache.json", "repository": "git", "program": "cd ${taskPath} && webpack --color" } @@ -200,7 +195,6 @@ Above-mentioned: when dll and package.json has changed, whatever other task's ta ["mod1", "mod2", "mod3"] ], "dependencies": ["package.json", "dll"], - "cache": "dist/.ci-task-runner-cache.json", "repository": "git", "program": "cd ${taskPath} && webpack --color" } diff --git a/bin/ci-task-runner b/bin/ci-task-runner index f17d8b8..68ac436 100755 --- a/bin/ci-task-runner +++ b/bin/ci-task-runner @@ -59,7 +59,6 @@ function init() { "program": "npm install" }], "dependencies": ["package.json"], - "cache": ".ci-task-runner-cache.json", "repository": "git", "program": "cd ${taskPath}" }; diff --git a/example/.ci-task-runner.json b/example/.ci-task-runner.json index 12f2025..cd85469 100644 --- a/example/.ci-task-runner.json +++ b/example/.ci-task-runner.json @@ -1,7 +1,6 @@ { "tasks": ["module-example"], "dependencies": ["package.json"], - "cache": "dist/.ci-task-runner-cache.json", "repository": "git", "program": "cd ${taskPath} && webpack --color" } \ No newline at end of file diff --git a/package.json b/package.json index 6fbdb3e..ea728e2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ci-task-runner", - "version": "1.0.1", + "version": "1.0.1-beta", "description": "在 CI 上运行的、支持增量与多进程并行构建任务调度程序", "main": "src/index.js", "bin": "bin/ci-task-runner", @@ -39,6 +39,7 @@ "homepage": "https://github.com/aui/ci-task-runner#readme", "dependencies": { "commander": "^2.9.0", + "find-cache-dir": "^1.0.0", "lodash.defaultsdeep": "^4.6.0", "npm-run-path": "^2.0.2" }, diff --git a/src/config/config.default.json b/src/config/config.default.json index e30751d..19fb612 100644 --- a/src/config/config.default.json +++ b/src/config/config.default.json @@ -1,7 +1,6 @@ { "tasks": [], "dependencies": [], - "cache": ".ci-task-runner-cache.json", "repository": "git", "program": {} } \ No newline at end of file diff --git a/src/index.js b/src/index.js index 528f736..b225fb5 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,5 @@ const path = require('path'); +const findCacheDir = require('find-cache-dir'); const fsp = require('../lib/fs-promise'); const defaultsDeep = require('lodash.defaultsdeep'); const promiseTask = require('../lib/promise-task'); @@ -34,7 +35,12 @@ const taskRunner = (options = {}, context = process.cwd()) => { const time = Date.now(); options = defaultsDeep({}, options, DEFAULT); - options.cache = path.resolve(context, options.cache); + + if (typeof options.cache === 'string') { + options.cache = path.resolve(context, options.cache); + } else { + options.cache = path.resolve(findCacheDir({name: PACKAGE.name}), PACKAGE.version + '.json'); + } const loger = new Loger(); const cache = {}; diff --git a/test/file/parallel/.ci-task-runner.json b/test/file/parallel/.ci-task-runner.json index 60f0c83..df5affd 100644 --- a/test/file/parallel/.ci-task-runner.json +++ b/test/file/parallel/.ci-task-runner.json @@ -1,7 +1,6 @@ { "tasks": [["mod1", "mod2", "mod3", "mod4", "mod5", "mod6", "mod7", "mod8"]], "dependencies": ["package.json"], - "cache": "dist/.ci-task-runner-cache.json", "program": { "command": "webpack -p --color --config ${taskPath}/webpack.config.js", "options": { diff --git a/test/file/tasks/.ci-task-runner.json b/test/file/tasks/.ci-task-runner.json index aa9afc4..a83315b 100644 --- a/test/file/tasks/.ci-task-runner.json +++ b/test/file/tasks/.ci-task-runner.json @@ -4,6 +4,5 @@ "program": "node ${taskPath}/builder.js" }]], "dependencies": ["lib"], - "cache": "../../dist/.ci-task-runner-cache.json", "program": "cd ${taskPath} && webpack -p --color" } \ No newline at end of file From c5621a28dc9cd046a83e67f8ab4c048756a940df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=B3=96=E9=A5=BC?= Date: Thu, 15 Jun 2017 14:06:02 +0800 Subject: [PATCH 3/4] v1.0.1 --- package.json | 4 ++-- src/build.js | 4 ++-- src/index.js | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index ea728e2..1c4c781 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ci-task-runner", - "version": "1.0.1-beta", + "version": "1.0.1", "description": "在 CI 上运行的、支持增量与多进程并行构建任务调度程序", "main": "src/index.js", "bin": "bin/ci-task-runner", @@ -10,7 +10,7 @@ "tasks-test": "node bin/ci-task-runner --config test/file/tasks/.ci-task-runner.json", "test": "export TEST_DIST=$PWD/test/dist && istanbul cover node_modules/mocha/bin/_mocha -- --recursive --bail 'test/**/*test.js'", "posttest": "npm run example && npm run tasks-test", - "clear": "rm -rf example/dist & rm -rf test/dist", + "clear": "rm -rf node_modules/.cache/ci-task-runner", "new-test": "npm run clear && npm run test", "coverage": "cat ./coverage/lcov.info | coveralls" }, diff --git a/src/build.js b/src/build.js index bbdee1b..8bb9b9b 100644 --- a/src/build.js +++ b/src/build.js @@ -33,8 +33,8 @@ module.exports = (tasks, parallel = require('os').cpus().length) => { return worker(program.command, program.options).then(() => { let loger = new Loger([{ color: 'green' }, ...logStyles]); - let timeEnd = Date.now() - time; - loger.log('░░', `${PACKAGE.name}:`, task.name, '[success]', `${timeEnd}ms`); + let timeEnd = Math.round((Date.now() - time) / 1000) + loger.log('░░', `${PACKAGE.name}:`, task.name, '[success]', `${timeEnd}s`); }).catch(errors => { let loger = new Loger([{ color: 'red' }, ...logStyles]); diff --git a/src/index.js b/src/index.js index b225fb5..6683f3a 100644 --- a/src/index.js +++ b/src/index.js @@ -126,8 +126,8 @@ const taskRunner = (options = {}, context = process.cwd()) => { } ]).then(results => { - let timeEnd = Date.now() - time; - loger.log('░░', `${PACKAGE.name}:`, `${timeEnd}ms`); + let timeEnd = Math.round((Date.now() - time) / 1000); + loger.log('░░', `${PACKAGE.name}:`, `${timeEnd}s`); return results[results.length - 1]; }); }; From 3f8964a5e2703759bdbe79e72fd3704ca819e426 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=B3=96=E9=A5=BC?= Date: Thu, 15 Jun 2017 14:09:55 +0800 Subject: [PATCH 4/4] Create CHANGELOG.md --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3653ac7..784a74e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ ## v1.1.0 -1. 使用 `npm-run-path` 来设置环境变量,避免 -2. `options.cache` 如果没有设置,则在 node_modules/.cache/ci-task-runner 中添加缓存 \ No newline at end of file +1. 使用 `npm-run-path` 来设置环境变量,避免跨平台 bug +2. `options.cache` 如果没有设置,则在 node_modules/.cache/ci-task-runner 中添加缓存