Skip to content

Commit

Permalink
Merge pull request #9 from aui/master
Browse files Browse the repository at this point in the history
v1.0.1
  • Loading branch information
aui authored Jun 15, 2017
2 parents 37313a8 + 3f8964a commit e6f5218
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 105 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## v1.1.0

1. 使用 `npm-run-path` 来设置环境变量,避免跨平台 bug
2. `options.cache` 如果没有设置,则在 node_modules/.cache/ci-task-runner 中添加缓存
8 changes: 1 addition & 7 deletions README.ZH-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down Expand Up @@ -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`

Expand Down Expand Up @@ -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"
}
Expand All @@ -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"
}
Expand All @@ -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"
}
Expand Down
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down Expand Up @@ -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`

Expand Down Expand Up @@ -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"
}
Expand All @@ -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"
}
Expand All @@ -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"
}
Expand Down
1 change: 0 additions & 1 deletion bin/ci-task-runner
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ function init() {
"program": "npm install"
}],
"dependencies": ["package.json"],
"cache": ".ci-task-runner-cache.json",
"repository": "git",
"program": "cd ${taskPath}"
};
Expand Down
1 change: 0 additions & 1 deletion example/.ci-task-runner.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"tasks": ["module-example"],
"dependencies": ["package.json"],
"cache": "dist/.ci-task-runner-cache.json",
"repository": "git",
"program": "cd ${taskPath} && webpack --color"
}
115 changes: 47 additions & 68 deletions lib/worker.js
Original file line number Diff line number Diff line change
@@ -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');


/**
Expand All @@ -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);
}
});
};
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
},
Expand Down Expand Up @@ -39,7 +39,9 @@
"homepage": "https://github.com/aui/ci-task-runner#readme",
"dependencies": {
"commander": "^2.9.0",
"lodash.defaultsdeep": "^4.6.0"
"find-cache-dir": "^1.0.0",
"lodash.defaultsdeep": "^4.6.0",
"npm-run-path": "^2.0.2"
},
"devDependencies": {
"jquery": "^3.1.1",
Expand Down
4 changes: 2 additions & 2 deletions src/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
1 change: 0 additions & 1 deletion src/config/config.default.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"tasks": [],
"dependencies": [],
"cache": ".ci-task-runner-cache.json",
"repository": "git",
"program": {}
}
12 changes: 9 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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 = {};
Expand Down Expand Up @@ -120,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];
});
};
Expand Down
1 change: 0 additions & 1 deletion test/file/parallel/.ci-task-runner.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
1 change: 0 additions & 1 deletion test/file/tasks/.ci-task-runner.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@
"program": "node ${taskPath}/builder.js"
}]],
"dependencies": ["lib"],
"cache": "../../dist/.ci-task-runner-cache.json",
"program": "cd ${taskPath} && webpack -p --color"
}
19 changes: 9 additions & 10 deletions test/lib/worker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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%`, {
Expand Down

0 comments on commit e6f5218

Please sign in to comment.