From 838d47b6c4fedf0d19d50ecf0e48a3afd22ba308 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Gra=C3=A7a?= Date: Wed, 11 Nov 2020 18:39:37 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20multiple=20lines=20on=20W?= =?UTF-8?q?indows=20(#210)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 🐛 multiple lines on Windows The description input was disappearing on Windows because of the new line character. This writes the commit message to a file first and then uses the --file parameter to read from it. ✅ Closes: #188 #197 * refactor: 💡 improved code readability and execution flow "command" was being built and escaped in multiple places, now its all in one place * test: 💍 update snapchot * fix: 🐛 eslint * fix: 🐛 command execution * refactor: ♻️ change commit file to COMMIT_EDITMSG * refactor: ♻️ move hook check down --- lib/cli.js | 51 ++++++++++++++++------------- test/__snapshots__/cli.test.js.snap | 4 ++- 2 files changed, 31 insertions(+), 24 deletions(-) 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 " `;