From 7cf2950ebbd9043077e67a6b9b533cf67446e6ff Mon Sep 17 00:00:00 2001 From: Andrew Clark Date: Wed, 15 Mar 2017 14:54:07 -0700 Subject: [PATCH] Pass args as an array --- scripts/prettier/index.js | 14 ++++++++---- scripts/prettier/runCommand.js | 40 ---------------------------------- 2 files changed, 10 insertions(+), 44 deletions(-) delete mode 100644 scripts/prettier/runCommand.js diff --git a/scripts/prettier/index.js b/scripts/prettier/index.js index 7864e6edca..e17a799a4b 100644 --- a/scripts/prettier/index.js +++ b/scripts/prettier/index.js @@ -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` ) + diff --git a/scripts/prettier/runCommand.js b/scripts/prettier/runCommand.js deleted file mode 100644 index 81e3b9a943..0000000000 --- a/scripts/prettier/runCommand.js +++ /dev/null @@ -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; - } -};