Pass args as an array
Andrew Clark committed
Mar 15, 2017 at 14:54 UTC
7cf2950ebbd9043077e67a6b9b533cf67446e6ff
2 files changed
+10
-44
scripts/prettier/index.js
+10
-4
@@ -13,7 +13,7 @@
13
const chalk = require('chalk');
14
const glob = require('glob');
15
const path = require('path');
16
-const runCommand = require('./runCommand');
16
+const execFileSync = require('child_process').execFileSync;
17
18
const shouldWrite = process.argv[2] === 'write';
19
const isWindows = process.platform === 'win32';
@@ -36,6 +36,12 @@ const config = {
36
},
37
};
38
39
+function exec(command, args) {
40
+ console.log('> ' + [command].concat(args).join(' '));
41
+ var options = {};
42
+ return execFileSync(command, args, options).toString();
43
+}
44
+
45
Object.keys(config).forEach(key => {
46
const patterns = config[key].patterns;
47
const options = config[key].options;
@@ -49,14 +55,14 @@ Object.keys(config).forEach(key => {
55
const args = Object.keys(defaultOptions).map(
56
k => `--${k}=${(options && options[k]) || defaultOptions[k]}`
57
);
52
- args.push(`--${shouldWrite ? 'write' : 'l'} {${files.join(' ')}}`);
58
+ args.push(`--${shouldWrite ? 'write' : 'l'}`);
59
60
try {
55
- runCommand(prettierCmd, args.join(' '), path.resolve(__dirname, '../..'));
61
+ exec(prettierCmd, [...args, ...files]);
62
} catch (e) {
57
- console.log(e);
63
if (!shouldWrite) {
64
console.log(
65
+ '\n' +
66
chalk.red(
67
` This project uses prettier to format all JavaScript code.\n`
68
) +
scripts/prettier/runCommand.js
deleted
-40
@@ -1,40 +0,0 @@
1
-/**
2
- * Copyright (c) 2014, Facebook, Inc. All rights reserved.
3
- *
4
- * This source code is licensed under the BSD-style license found in the
5
- * LICENSE file in the root directory of this source tree. An additional grant
6
- * of patent rights can be found in the PATENTS file in the same directory.
7
- */
8
-'use strict';
9
-
10
-const chalk = require('chalk');
11
-const spawn = require('child_process').spawnSync;
12
-
13
-module.exports = function runCommand(cmd, args, cwd) {
14
- if (!cwd) {
15
- cwd = __dirname;
16
- }
17
-
18
- const callArgs = args.split(' ');
19
- console.log(
20
- chalk.dim('$ cd ' + cwd) +
21
- '\n' +
22
- chalk.dim(
23
- ' $ ' +
24
- cmd +
25
- ' ' +
26
- (args.length > 1000 ? args.slice(0, 1000) + '...' : args)
27
- ) +
28
- '\n'
29
- );
30
- const result = spawn(cmd, callArgs, {
31
- cwd,
32
- stdio: 'inherit',
33
- });
34
- if (result.error || result.status !== 0) {
35
- const message = 'Error running command.';
36
- const error = new Error(message);
37
- error.stack = message;
38
- throw error;
39
- }
40
-};