Skip to content

Commit

Permalink
Merge pull request #13 from aui/master
Browse files Browse the repository at this point in the history
fix #11
  • Loading branch information
aui committed Jul 21, 2017
2 parents f3a230f + ee6ab42 commit 2993d84
Show file tree
Hide file tree
Showing 25 changed files with 257 additions and 189 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v1.0.4

1. 修复日志太多可能产生的溢出问题 [#11](https://github.com/huanleguang/ci-task-runner/issues/11)

## v1.0.3

1. 修复子进程意外退出后的错误信息显示
Expand Down
8 changes: 6 additions & 2 deletions README.ZH-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,13 @@ ci-task-runner 缓存文件写入路径,用来保存上一次任务的信息
#### `program.options`

进程配置。参考:[child_process.exec](https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback)
设置进程的选项

> `program.options` 中的 `timeout` 字段生效后会终止进程,并且抛出错误。这点和 `child_process.exec` 不一样,它只抛出错误。
* `cwd` 当前子进程的工作目录
* `env` 环境变量
* `timeout` 超时时间
* `uid` 设置进程的用户标识
* `gid` 设置进程的组标识

#### 变量

Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,13 @@ Setting start command.
#### `program.options`

Progress configuration. Reference: [child_process.exec](https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback).
Progress configuration.

> The `timeout` field in `program.options` takes effect and terminates the process and throws an error. `child_process.exec` only throw error.
* `cwd` Current working directory of the child process
* `env` Environment key-value pairs
* `timeout` Timeout
* `uid` Sets the user identity of the process
* `gid` Sets the group identity of the process

#### Variable

Expand Down
82 changes: 0 additions & 82 deletions lib/worker.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ci-task-runner",
"version": "1.0.3",
"version": "1.0.4",
"description": "this is a multiprocess building tasks scheduler",
"main": "src/index.js",
"bin": "bin/ci-task-runner",
Expand Down
22 changes: 11 additions & 11 deletions src/parse.js → src/create-tasks.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const path = require('path');
const template = require('../lib/template');
const template = require('./template');
const defaultsDeep = require('lodash.defaultsdeep');
const DEFAULT = require('./config/config.program.default.json');

Expand Down Expand Up @@ -41,10 +41,10 @@ class Task extends File {
* @return {Task[]}
*/
module.exports = (options, context) => {
let tasks = [];
const tasks = [];


let parseDependencie = lib => {
const parseDependencie = lib => {
if (typeof lib === 'string') {
lib = {
name: lib,
Expand All @@ -58,7 +58,7 @@ module.exports = (options, context) => {
};


let parseProgram = program => {
const parseProgram = program => {
if (typeof program === 'string') {
program = {
command: program,
Expand All @@ -71,14 +71,14 @@ module.exports = (options, context) => {


// 设置字符串变量
let setVariables = (target, variables) => {
let type = typeof target;
const setVariables = (target, variables) => {
const type = typeof target;
if (type === 'string') {
return template(target, variables);
} else if (Array.isArray(target)) {
return target.map(target => setVariables(target, variables));
} else if (type === 'object' && type !== null) {
let object = {};
const object = {};
Object.keys(target).forEach(key => object[key] = setVariables(target[key], variables));
return object;
} else {
Expand All @@ -87,7 +87,7 @@ module.exports = (options, context) => {
};


let parseTask = (task, order) => {
const parseTask = (task, order) => {

if (typeof task === 'string') {
task = {
Expand All @@ -109,8 +109,8 @@ module.exports = (options, context) => {
)
};

let dependencies = task.dependencies.map(parseDependencie);
let program = setVariables(task.program, {
const dependencies = task.dependencies.map(parseDependencie);
const program = setVariables(task.program, {
taskName: task.name,
taskPath: task.path,
taskDirname: path.dirname(task.path)
Expand All @@ -126,7 +126,7 @@ module.exports = (options, context) => {
});
};

let each = (task, index) => {
const each = (task, index) => {
if (Array.isArray(task)) {
task.forEach(task => each(task, index));
} else {
Expand Down
4 changes: 2 additions & 2 deletions lib/fs-promise.js → src/fs-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const mkdirs = (dirname, callback) => {
* @return {Promise}
*/
const writeFile = promiseify((...params) => {
let file = params[0];
let dirname = path.dirname(file);
const file = params[0];
const dirname = path.dirname(file);
mkdirs(dirname, ()=> {
fs.writeFile(...params);
});
Expand Down
27 changes: 13 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
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');
const Repository = require('../lib/repository');
const Loger = require('../lib/loger');
const fsp = require('./fs-promise');
const queue = require('./queue');
const Repository = require('./repository');
const Loger = require('./loger');
const DEFAULT = require('./config/config.default.json');
const CACHE_DEFAULT = require('./config/cache.default.json');
const PACKAGE = require('../package.json');

const parse = require('./parse');
const build = require('./build');
const createTasks = require('./create-tasks');
const runTasks = require('./run-tasks');


/**
Expand Down Expand Up @@ -49,11 +48,11 @@ const taskRunner = (options = {}, context = process.cwd()) => {

const repository = new Repository(options.cache, options.repository, 'revision');

return promiseTask.serial([
return queue.serial([


// 将外部输入的配置转换成内部任务描述队列
parse(options, context),
createTasks(options, context),


// 检查任务是否有变更
Expand All @@ -67,8 +66,8 @@ const taskRunner = (options = {}, context = process.cwd()) => {

]).then(([modCommit, ...libCommits]) => {

let modChanged = modCommit[0] !== modCommit[1];
let libChanged = libCommits.filter(libCommit => libCommit[0] !== libCommit[1]).length !== 0;
const modChanged = modCommit[0] !== modCommit[1];
const libChanged = libCommits.filter(libCommit => libCommit[0] !== libCommit[1]).length !== 0;
task.dirty = options.force || modChanged || libChanged;

cache.tasks[task.name] = {
Expand Down Expand Up @@ -102,7 +101,7 @@ const taskRunner = (options = {}, context = process.cwd()) => {

// 运行构建器
tasks => {
return build(tasks, options.parallel);
return runTasks(tasks, options.parallel);
},


Expand All @@ -113,7 +112,7 @@ const taskRunner = (options = {}, context = process.cwd()) => {
.catch(() => defaultsDeep({}, CACHE_DEFAULT))
.then(oldAssets => defaultsDeep(cache, oldAssets))
.then(cache => {
let json = JSON.stringify(cache, null, 2);
const json = JSON.stringify(cache, null, 2);
return fsp.writeFile(options.cache, json, 'utf8').then(() => cache);
});
},
Expand All @@ -126,7 +125,7 @@ const taskRunner = (options = {}, context = process.cwd()) => {
}

]).then(results => {
let timeEnd = Math.round((Date.now() - time) / 1000);
const timeEnd = Math.round((Date.now() - time) / 1000);
loger.log('░░', `${PACKAGE.name}:`, `${timeEnd}s`);
return results[results.length - 1];
});
Expand Down
16 changes: 8 additions & 8 deletions lib/loger.js → src/loger.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class StringStyle {
Object.keys(TYPES).filter(key => {
return this[key] !== undefined;
}).forEach(key => {
let value = this[key];
const value = this[key];
this[TYPES[key]](value);
});

Expand All @@ -41,7 +41,7 @@ class StringStyle {
}

[textAlign](value) {
let content = this.string.join('');
const content = this.string.join('');
let str;
let length;
switch (value) {
Expand All @@ -66,31 +66,31 @@ class StringStyle {

[color](value) {
if (this.displayColor && StringStyle.colors[value]) {
let style = StringStyle.colors[value];
const style = StringStyle.colors[value];
this.string.unshift(style[0]);
this.string.push(style[1]);
}
}

[fontWeight](value) {
if (this.displayColor && value === 'bold') {
let style = ['\x1B[1m', '\x1B[22m'];
const style = ['\x1B[1m', '\x1B[22m'];
this.string.unshift(style[0]);
this.string.push(style[1]);
}
}

[fontStyle](value) {
if (this.displayColor && value === 'italic') {
let style = ['\x1B[3m', '\x1B[23m'];
const style = ['\x1B[3m', '\x1B[23m'];
this.string.unshift(style[0]);
this.string.push(style[1]);
}
}

[textDecoration](value) {
if (this.displayColor && value === 'underline') {
let style = ['\x1B[4m', '\x1B[24m'];
const style = ['\x1B[4m', '\x1B[24m'];
this.string.unshift(style[0]);
this.string.push(style[1])
}
Expand Down Expand Up @@ -136,8 +136,8 @@ class Loger {

setStyles(messages) {
return messages.map((message, index) => {
let stringStyle = new StringStyle(message, this.displayColor);
let style = this.styles[index];
const stringStyle = new StringStyle(message, this.displayColor);
const style = this.styles[index];

Object.keys(style || {}).forEach(name => {
stringStyle[name] = style[name];
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions lib/promise-task.js → src/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
*/
const serial = tasks => {
let p = Promise.resolve();
let results = [];
const results = [];

let each = task => {
const each = task => {
if (typeof task === 'function') {
p = p.then(task).then(result => {
results.push(result);
Expand Down Expand Up @@ -39,7 +39,7 @@ const parallel = (tasks, limit = tasks.length) => {
}
}));
} else {
let chunks = [];
const chunks = [];
tasks = [...tasks];

for (let i = 0, len = tasks.length; i < len; i += limit) {
Expand Down
Loading

0 comments on commit 2993d84

Please sign in to comment.