Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: execArgv convert rule #20

Merged
merged 1 commit into from
Mar 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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