From 34cad2ad20781ec671bc8d7b846a1de1a1e3fc2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?TZ=20=7C=20=E5=A4=A9=E7=8C=AA?= Date: Fri, 3 Jul 2020 10:29:01 +0800 Subject: [PATCH] feat: expose proc (#42) --- lib/helper.js | 6 +++++- test/fixtures/my-helper/command/fork.js | 4 +++- test/fixtures/my-helper/scripts/test_script.js | 1 + test/my-helper.test.js | 1 + 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/helper.js b/lib/helper.js index 1480489..9c5f29e 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -49,7 +49,7 @@ exports.forkNode = (modulePath, args = [], options = {}) => { const proc = cp.fork(modulePath, args, options); gracefull(proc); - return new Promise((resolve, reject) => { + const promise = new Promise((resolve, reject) => { proc.once('exit', code => { childs.delete(proc); if (code !== 0) { @@ -61,6 +61,10 @@ exports.forkNode = (modulePath, args = [], options = {}) => { } }); }); + + promise.proc = proc; + + return promise; }; /** diff --git a/test/fixtures/my-helper/command/fork.js b/test/fixtures/my-helper/command/fork.js index 9448ad2..b72d02f 100644 --- a/test/fixtures/my-helper/command/fork.js +++ b/test/fixtures/my-helper/command/fork.js @@ -14,7 +14,9 @@ class ForkCommand extends Command { } * run({ argv, rawArgv }) { - yield this.helper.forkNode(path.join(__dirname, '../scripts', argv.target), rawArgv.concat('--from=test')); + const task = this.helper.forkNode(path.join(__dirname, '../scripts', argv.target), rawArgv.concat('--from=test')); + console.log('task proc: %s', !!task.proc); + yield task; } get description() { diff --git a/test/fixtures/my-helper/scripts/test_script.js b/test/fixtures/my-helper/scripts/test_script.js index 4852dfe..7a6ae1b 100644 --- a/test/fixtures/my-helper/scripts/test_script.js +++ b/test/fixtures/my-helper/scripts/test_script.js @@ -4,3 +4,4 @@ console.log('node version: %s', process.version); console.log('process.argv: %j', process.argv.slice(2)); +console.log('process.pid: %s, process.ppid: %s', process.pid, process.ppid); diff --git a/test/my-helper.test.js b/test/my-helper.test.js index 4402cda..e495915 100644 --- a/test/my-helper.test.js +++ b/test/my-helper.test.js @@ -35,6 +35,7 @@ describe('test/my-helper.test.js', () => { coffee.fork(myBin, [ 'fork', '--target=test_script' ], { cwd }) // .debug() // .coverage(false) + .expect('stdout', /task proc: true/) .expect('stdout', /process.argv: \["--target=test_script","--from=test"]/) .expect('code', 0) .end(done);