diff --git a/lib/cli.js b/lib/cli.js index a7289342..511e087f 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -11,9 +11,8 @@ const formatCommitMessage = require('./formatCommitMessage'); const getGitRootDir = require('./util/getGitRootDir'); // eslint-disable-next-line no-process-env -const executeCommand = (command, args = [], env = process.env) => { - const formattedCommand = shellescape([command, ...args]); - const proc = spawn(formattedCommand, [], { +const executeCommand = (command, env = process.env) => { + const proc = spawn(command, [], { env, shell: true, stdio: [0, 1, 2] @@ -73,18 +72,6 @@ const main = async () => { const message = formatCommitMessage(state); - /** - * @author https://github.com/oxyii - * @see https://github.com/streamich/git-cz/issues/79 - */ - if (cliOptions.hook) { - const commitMsgFile = join(getGitRootDir(), '.git', 'COMMIT_EDITMSG'); - - fs.writeFileSync(commitMsgFile, message); - // eslint-disable-next-line no-process-exit - process.exit(0); - } - const appendedArgs = []; // eslint-disable-next-line guard-for-in @@ -102,22 +89,40 @@ const main = async () => { } } - const executeCommandArgs = [ + const commitMsgFile = join(getGitRootDir(), '.git', 'COMMIT_EDITMSG'); + + const command = shellescape([ + 'git', 'commit', - '--message', - message, + '--file', + commitMsgFile, ...appendedArgs - ]; + ]); if (cliOptions.dryRun) { - const command = shellescape(['git', ...executeCommandArgs]); - // eslint-disable-next-line no-console console.log('Will execute command:'); + + // The full path is replaced with a relative path to make the test pass on every machine + // eslint-disable-next-line no-console + console.log(command.replace(commitMsgFile, '.git/COMMIT_EDITMSG')); // eslint-disable-next-line no-console - console.log(command); + console.log('Message:'); + // eslint-disable-next-line no-console + console.log(message); } else { - executeCommand('git', executeCommandArgs); + fs.writeFileSync(commitMsgFile, message); + + /** + * @author https://github.com/oxyii + * @see https://github.com/streamich/git-cz/issues/79 + */ + if (cliOptions.hook) { + // eslint-disable-next-line no-process-exit + process.exit(0); + } + + executeCommand(command); } } catch (error) { signale.fatal(error); diff --git a/test/__snapshots__/cli.test.js.snap b/test/__snapshots__/cli.test.js.snap index 51160f59..9dedd3f1 100644 --- a/test/__snapshots__/cli.test.js.snap +++ b/test/__snapshots__/cli.test.js.snap @@ -27,6 +27,8 @@ exports[`git-cz --help 1`] = ` exports[`git-cz --non-interactive 1`] = ` "Running in dry mode. Will execute command: -git commit --message 'chore: 🤖 automated commit' +git commit --file '.git/COMMIT_EDITMSG' +Message: +chore: 🤖 automated commit " `;