Skip to content

Commit

Permalink
Merge pull request #117 from alibaba/dev
Browse files Browse the repository at this point in the history
0.9.3
  • Loading branch information
Houfeng authored Jan 5, 2018
2 parents 071ad98 + 77c7df7 commit 7ca94ff
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
31 changes: 25 additions & 6 deletions lib/common/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

const shify = require('shify');
const path = require('path');
const BufferHelper = require('bufferhelper');
const debug = require('debug')('exec');

module.exports = function (script, opts) {
function exec(script, opts) {
opts = opts || {};
opts.cwd = opts.cwd || process.cwd();
opts.env = opts.env || Object.assign({}, process.env);
Expand All @@ -19,13 +20,31 @@ module.exports = function (script, opts) {
script = script || '';
debug('script', opts);
return new Promise((resolve, reject) => {
let io = shify(script, opts);
io.on('exit', code => {
let childProcess = shify(script, opts);
if (opts.onStart) opts.onStart(childProcess);
childProcess.on('exit', code => {
debug('exit', code);
if (code > 0) {
if (code !== 0) {
return reject(new Error(`Script Error, exit ${code}`));
}
resolve(io);
if (opts.onExit) opts.onExit(code, childProcess);
resolve(childProcess);
});
});
};
}

async function withResult(script, opts) {
const helper = new BufferHelper();
opts = Object.assign({}, opts, {
stdio: 'pipe',
onStart: child => {
child.stdout.on('data', chunk => helper.concat(chunk));
child.stderr.on('data', chunk => helper.concat(chunk));
}
});
await exec(script, opts);
return helper.toBuffer().toString();
}

exec.withResult = withResult;
module.exports = exec;
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dawn",
"version": "0.9.2",
"version": "0.9.3",
"description": "dawn cli",
"main": "./lib/index.js",
"bin": {
Expand Down Expand Up @@ -53,6 +53,7 @@
},
"dependencies": {
"buffer-to-stream": "^1.0.0",
"bufferhelper": "^0.2.1",
"cmdline": "^2.0.4",
"cnpm": "^5.1.1",
"confman": "^0.2.8",
Expand Down Expand Up @@ -84,4 +85,4 @@
"nyc": "^11.1.0",
"pre-push": "^0.1.1"
}
}
}
1 change: 0 additions & 1 deletion packages/dn-middleware-shell/.dawn/pipe.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ dev:
location: ./lib/index.js
script:
- echo 你好${project.name}
- echo $PATH
- name: $local
location: ./lib/index.js
script:
Expand Down

0 comments on commit 7ca94ff

Please sign in to comment.