Skip to content

Commit c4e8bce

Browse files
committed
feat(build): use options to control cli/shell run
1 parent 5823a18 commit c4e8bce

File tree

10 files changed

+55
-30
lines changed

10 files changed

+55
-30
lines changed

packages/build/bin/compile-package.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ function run(argv, dryRun) {
114114

115115
args.push(...compilerOpts);
116116

117-
return utils.runCLI('typescript/lib/tsc', args, dryRun);
117+
return utils.runCLI('typescript/lib/tsc', args, {dryRun});
118118
}
119119

120120
module.exports = run;

packages/build/bin/generate-apidocs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function run(argv, dryRun) {
4545
args.push('-o', 'api-docs');
4646
}
4747
args.push(...apidocsOpts);
48-
return utils.runCLI('strong-docs/bin/cli', args, dryRun);
48+
return utils.runCLI('strong-docs/bin/cli', args, {dryRun});
4949
}
5050

5151
module.exports = run;

packages/build/bin/run-clean.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ function run(argv, dryRun) {
3535
files.forEach(f => {
3636
var file = path.relative(process.cwd(), f);
3737
if (file.indexOf('..') !== -1) {
38-
console.error('Skipping ' + f + ' as it is not inside the project');
38+
if (!dryRun) {
39+
console.error('Skipping ' + f + ' as it is not inside the project');
40+
}
3941
} else {
4042
if (!dryRun) fs.removeSync(f);
4143
removed.push(f);

packages/build/bin/run-mocha.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function run(argv, dryRun) {
5252

5353
const args = [...mochaOpts];
5454

55-
return utils.runCLI('mocha/bin/mocha', args, dryRun);
55+
return utils.runCLI('mocha/bin/mocha', args, {dryRun});
5656
}
5757

5858
module.exports = run;

packages/build/bin/run-nyc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function run(argv, dryRun) {
2222
const nycOpts = argv.slice(2);
2323
const args = [...nycOpts];
2424

25-
return utils.runCLI('nyc/bin/nyc', args, dryRun);
25+
return utils.runCLI('nyc/bin/nyc', args, {dryRun});
2626
}
2727

2828
module.exports = run;

packages/build/bin/run-prettier.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function run(argv, dryRun) {
3434
}
3535
args.push(...prettierOpts);
3636

37-
return utils.runCLI('prettier/bin-prettier', args, dryRun);
37+
return utils.runCLI('prettier/bin-prettier', args, {dryRun});
3838
}
3939

4040
module.exports = run;

packages/build/bin/run-tslint.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function run(argv, dryRun) {
3939
}
4040
args.push(...tslintOpts);
4141

42-
return utils.runCLI('tslint/bin/tslint', args, dryRun);
42+
return utils.runCLI('tslint/bin/tslint', args, {dryRun});
4343
}
4444

4545
module.exports = run;

packages/build/bin/select-dist.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function run(argv, dryRun) {
3232
const args = argv.slice(2).map(a => a.replace(/\bDIST\b/g, dist));
3333
const command = args.shift();
3434

35-
return utils.runShell(command, args, dryRun);
35+
return utils.runShell(command, args, {dryRun});
3636
}
3737

3838
module.exports = run;

packages/build/bin/utils.js

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -104,23 +104,30 @@ function resolveCLI(cli) {
104104
* Run a command with the given arguments
105105
* @param {string} cli Path of the cli command
106106
* @param {string[]} args The arguments
107-
* @param {boolean} dryRun Controls if the cli will be executed or not. If set
107+
* @param {object} options Options to control dryRun and spawn
108+
* - dryRun Controls if the cli will be executed or not. If set
108109
* to true, the command itself will be returned without running it
109110
*/
110-
function runCLI(cli, args, dryRun) {
111+
function runCLI(cli, args, options) {
111112
cli = resolveCLI(cli);
112113
args = [cli].concat(args);
113114
debug('%s', args.join(' '));
114-
if (dryRun) {
115+
// Keep it backward compatible as dryRun
116+
if (typeof options === 'boolean') options = {dryRun: options};
117+
options = options || {};
118+
if (options.dryRun) {
115119
return util.format('%s %s', process.execPath, args.join(' '));
116120
}
117121
var child = spawn(
118122
process.execPath, // Typically '/usr/local/bin/node'
119123
args,
120-
{
121-
stdio: 'inherit',
122-
env: Object.create(process.env),
123-
}
124+
Object.assign(
125+
{
126+
stdio: 'inherit',
127+
env: Object.create(process.env),
128+
},
129+
options
130+
)
124131
);
125132
child.on('close', (code, signal) => {
126133
debug('%s exits: %d', cli, code);
@@ -133,23 +140,34 @@ function runCLI(cli, args, dryRun) {
133140
* Run the command in a shell
134141
* @param {string} command The command
135142
* @param {string[]} args The arguments
136-
* @param {boolean} dryRun Controls if the cli will be executed or not. If set
143+
* @param {object} options Options to control dryRun and spawn
144+
* - dryRun Controls if the cli will be executed or not. If set
137145
* to true, the command itself will be returned without running it
138146
*/
139-
function runShell(command, args, dryRun) {
147+
function runShell(command, args, options) {
140148
args = args.map(a => JSON.stringify(a));
141149
debug('%s %s', command, args.join(' '));
142-
if (dryRun) {
150+
// Keep it backward compatible as dryRun
151+
if (typeof options === 'boolean') options = {dryRun: options};
152+
options = options || {};
153+
if (options.dryRun) {
143154
return util.format('%s %s', command, args.join(' '));
144155
}
145-
var child = spawn(command, args, {
146-
stdio: 'inherit',
147-
env: Object.create(process.env),
148-
// On Windows, npm creates `.cmd` files instead of symlinks in
149-
// `node_modules/.bin` folder. These files cannot be executed directly,
150-
// only via a shell.
151-
shell: true,
152-
});
156+
var child = spawn(
157+
command,
158+
args,
159+
Object.assign(
160+
{
161+
stdio: 'inherit',
162+
env: Object.create(process.env),
163+
// On Windows, npm creates `.cmd` files instead of symlinks in
164+
// `node_modules/.bin` folder. These files cannot be executed directly,
165+
// only via a shell.
166+
shell: true,
167+
},
168+
options
169+
)
170+
);
153171
child.on('close', (code, signal) => {
154172
debug('%s exits: %d', command, code);
155173
process.exitCode = code;

packages/cli/test/app-run.test.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,15 @@ describe('app-generator', function() {
4343
it('passes `npm test` for the generated project', () => {
4444
process.chdir(sandbox);
4545
return new Promise((resolve, reject) => {
46-
build.runShell('npm', ['test']).on('close', code => {
47-
assert.equal(code, 0);
48-
resolve();
49-
});
46+
build
47+
.runShell('npm', ['test'], {
48+
// Disable stdout
49+
stdio: [process.stdin, 'ignore', process.stderr],
50+
})
51+
.on('close', code => {
52+
assert.equal(code, 0);
53+
resolve();
54+
});
5055
});
5156
});
5257

0 commit comments

Comments
 (0)