|
1 | 1 | #!/usr/bin/env node |
2 | 2 |
|
3 | 3 | const cli = require('commander'); |
4 | | -const util = require('util'); |
5 | 4 | const process = require('process'); |
6 | 5 | const cosmiconfig = require('cosmiconfig'); |
7 | | -const exec = util.promisify(require('child_process').exec); |
| 6 | +const execa = require('execa'); |
| 7 | +const findBinary = require('./find-binary'); |
8 | 8 | const pkg = require('./package.json'); |
9 | 9 |
|
10 | 10 | cli |
|
13 | 13 |
|
14 | 14 | const binary = `${__dirname}/bin/git-run-if-changed.sh`; |
15 | 15 |
|
16 | | -async function runCommandsIfFileChanged(fileToCheck, commands) { |
| 16 | +function runCommandsIfFileChanged(fileToCheck, commands) { |
17 | 17 | const commandsList = Array.isArray(commands) ? commands : [commands]; |
18 | | - const commandsString = commandsList.map(x => `"${x}"`).join(' '); |
19 | | - const command = `${binary} "${fileToCheck}" ${commandsString}`; |
20 | | - const response = await exec(command); |
21 | | - const { stdout, stderr } = response; |
22 | | - if (stdout) { |
23 | | - console.log(stdout); |
24 | | - } |
25 | | - if (response instanceof Error) { |
26 | | - console.error('There was an error executing script:'); |
27 | | - } |
28 | | - if (stderr) { |
29 | | - console.error(stderr); |
30 | | - } |
| 18 | + const commandsListResolved = commandsList.map((cmd) => { |
| 19 | + const { binaryPath, args: binaryArgs } = findBinary(cmd); |
| 20 | + |
| 21 | + return [binaryPath].concat(binaryArgs).join(' '); |
| 22 | + }); |
| 23 | + const args = [fileToCheck].concat(commandsListResolved); |
| 24 | + execa(binary, args).stdout.pipe(process.stdout); |
31 | 25 | } |
32 | 26 |
|
33 | 27 | const configResult = cosmiconfig('run-if-changed').searchSync(); |
|
0 commit comments