Skip to content

Commit

Permalink
fix: execArgv convert rule (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 authored and dead-horse committed Mar 26, 2018
1 parent 02f8910 commit 037b1d0
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 7 deletions.
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 node-modules and other contributors
Copyright (c) 2017-present node-modules and other contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
26 changes: 21 additions & 5 deletions lib/command.js
Expand Up @@ -309,12 +309,28 @@ class CommonBin {
argv[changeCase.camel(key)] = undefined;
}

// only exports execArgv when any match
if (Object.keys(execArgvObj).length) {
context.execArgv = this.helper.unparseArgv(execArgvObj);
context.execArgvObj = execArgvObj;
context.debugOptions = debugOptions;
// exports execArgv
const self = this;
context.execArgvObj = execArgvObj;

// convert execArgvObj to execArgv
// `--require` should be `--require abc --require 123`, not allow `=`
// `--debug` should be `--debug=9999`, only allow `=`
Object.defineProperty(context, 'execArgv', {
get() {
const lazyExecArgvObj = context.execArgvObj;
const execArgv = self.helper.unparseArgv(lazyExecArgvObj, { excludes: [ 'require' ] });
if (lazyExecArgvObj.require) {
execArgv.push(...self.helper.unparseArgv({ require: lazyExecArgvObj.require }, { useEquals: false }));
}
return execArgv;
},
});

// only exports debugPort when any match
if (Object.keys(debugOptions).length) {
context.debugPort = debugPort;
context.debugOptions = debugOptions;
}
}

Expand Down
1 change: 1 addition & 0 deletions test/fixtures/my-bin/command/parserDebug.js
Expand Up @@ -22,6 +22,7 @@ class ContextCommand extends Command {
* run({ argv, execArgv, debugPort, debugOptions }) {
console.log('argv: %j', argv);
console.log('execArgv: %s', execArgv);
console.log('execArgv.length: %s', execArgv.length);
console.log('debugPort: %s', debugPort);
console.log('debugOptions: %j', debugOptions);
}
Expand Down
35 changes: 35 additions & 0 deletions test/fixtures/my-bin/command/parserRequire.js
@@ -0,0 +1,35 @@
'use strict';

const Command = require('../../../..');

class ContextCommand extends Command {
constructor(rawArgv) {
super(rawArgv);
this.parserOptions = {
execArgv: true,
removeAlias: true,
};

this.options = {
baseDir: {
description: 'target directory',
alias: 'b',
},
};
}


* run(context) {
context.execArgvObj = { require: 'abc' };
console.log('execArgv: %j', context.execArgv);

context.execArgvObj = { require: [ 'abc', '123' ] };
console.log('execArgv: %j', context.execArgv);
}

get description() {
return 'custom context';
}
}

module.exports = ContextCommand;
15 changes: 14 additions & 1 deletion test/my-bin.test.js
Expand Up @@ -126,6 +126,19 @@ describe('test/my-bin.test.js', () => {
.end(done);
});

it('my-bin parserRequire', done => {
const args = [
'parserRequire',
];
coffee.fork(myBin, args, { cwd })
// .debug()
// .coverage(false)
.expect('stdout', /execArgv: \["--require","abc"]/)
.expect('stdout', /execArgv: \["--require","abc","123"]/)
.expect('code', 0)
.end(done);
});

it('my-bin parserDebug without execArgv', done => {
const args = [
'parserDebug',
Expand All @@ -136,7 +149,7 @@ describe('test/my-bin.test.js', () => {
// .debug()
// .coverage(false)
.expect('stdout', /"debug-invalid":true,"debugInvalid":true/)
.expect('stdout', /execArgv: undefined/)
.expect('stdout', /execArgv.length: 0/)
.expect('stdout', /debugPort: undefined/)
.expect('stdout', /debugOptions: undefined/)
.expect('code', 0)
Expand Down

0 comments on commit 037b1d0

Please sign in to comment.