Skip to content

Commit

Permalink
fix: πŸ› multiple lines on Windows (#210)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rodrigograca31 committed Nov 11, 2020
1 parent 0576907 commit 838d47b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
51 changes: 28 additions & 23 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion test/__snapshots__/cli.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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
"
`;

1 comment on commit 838d47b

@streamich
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Build version: 4.5.0-master.1024 🀞 master on Travis πŸŽ‰

Please sign in to comment.