Pass args as an array

This commit is contained in:
Andrew Clark
2017-03-15 14:54:07 -07:00
parent ecf2e44908
commit 7cf2950ebb
2 changed files with 10 additions and 44 deletions

View File

@@ -13,7 +13,7 @@
const chalk = require('chalk');
const glob = require('glob');
const path = require('path');
const runCommand = require('./runCommand');
const execFileSync = require('child_process').execFileSync;
const shouldWrite = process.argv[2] === 'write';
const isWindows = process.platform === 'win32';
@@ -36,6 +36,12 @@ const config = {
},
};
function exec(command, args) {
console.log('> ' + [command].concat(args).join(' '));
var options = {};
return execFileSync(command, args, options).toString();
}
Object.keys(config).forEach(key => {
const patterns = config[key].patterns;
const options = config[key].options;
@@ -49,14 +55,14 @@ Object.keys(config).forEach(key => {
const args = Object.keys(defaultOptions).map(
k => `--${k}=${(options && options[k]) || defaultOptions[k]}`
);
args.push(`--${shouldWrite ? 'write' : 'l'} {${files.join(' ')}}`);
args.push(`--${shouldWrite ? 'write' : 'l'}`);
try {
runCommand(prettierCmd, args.join(' '), path.resolve(__dirname, '../..'));
exec(prettierCmd, [...args, ...files]);
} catch (e) {
console.log(e);
if (!shouldWrite) {
console.log(
'\n' +
chalk.red(
` This project uses prettier to format all JavaScript code.\n`
) +

View File

@@ -1,40 +0,0 @@
/**
* Copyright (c) 2014, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
const chalk = require('chalk');
const spawn = require('child_process').spawnSync;
module.exports = function runCommand(cmd, args, cwd) {
if (!cwd) {
cwd = __dirname;
}
const callArgs = args.split(' ');
console.log(
chalk.dim('$ cd ' + cwd) +
'\n' +
chalk.dim(
' $ ' +
cmd +
' ' +
(args.length > 1000 ? args.slice(0, 1000) + '...' : args)
) +
'\n'
);
const result = spawn(cmd, callArgs, {
cwd,
stdio: 'inherit',
});
if (result.error || result.status !== 0) {
const message = 'Error running command.';
const error = new Error(message);
error.stack = message;
throw error;
}
};