Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(edit): let git handle the edit instead of the custom impleme… #96

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 1 addition & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

const CZ_CONFIG_NAME = '.cz-config.js';
const findConfig = require('find-config');
const editor = require('editor');
const temp = require('temp').track();
const fs = require('fs');
const path = require('path');
const log = require('./logger');
const buildCommit = require('./buildCommit');
Expand Down Expand Up @@ -58,24 +55,7 @@ module.exports = {

cz.prompt(questions).then(answers => {
if (answers.confirmCommit === 'edit') {
temp.open(null, (err, info) => {
/* istanbul ignore else */
if (!err) {
fs.writeSync(info.fd, buildCommit(answers, config));
fs.close(info.fd, () => {
editor(info.path, code => {
if (code === 0) {
const commitStr = fs.readFileSync(info.path, {
encoding: 'utf8',
});
commit(commitStr);
} else {
log.info(`Editor returned non zero value. Commit message was:\n${buildCommit(answers, config)}`);
}
});
});
}
});
commit(buildCommit(answers, config), { args: ['--edit'] });
} else if (answers.confirmCommit === 'yes') {
commit(buildCommit(answers, config));
} else {
Expand Down
37 changes: 18 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@
],
"license": "MIT",
"dependencies": {
"editor": "1.0.0",
"find-config": "^1.0.0",
"inquirer": "^6.3.1",
"lodash": "^4.17.11",
"temp": "^0.9.0",
"word-wrap": "^1.2.3"
},
"devDependencies": {
Expand Down
22 changes: 1 addition & 21 deletions spec/czCustomizableSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,26 +125,6 @@ describe('cz-customizable', () => {
});

it('should allow edit message before commit', done => {
process.env.EDITOR = 'true';

const answers = {
confirmCommit: 'edit',
type: 'feat',
subject: 'create a new cool feature',
};

const mockCz = getMockedCz(answers);
module.prompter(mockCz, commit);

setTimeout(() => {
expect(commit).toHaveBeenCalledWith('feat: create a new cool feature');
done();
}, 100);
});

it('should not commit if editor returned non-zero value', done => {
process.env.EDITOR = 'false';

const answers = {
confirmCommit: 'edit',
type: 'feat',
Expand All @@ -155,7 +135,7 @@ describe('cz-customizable', () => {
module.prompter(mockCz, commit);

setTimeout(() => {
expect(commit.wasCalled).toEqual(false);
expect(commit).toHaveBeenCalledWith('feat: create a new cool feature', { args: ['--edit'] });
done();
}, 100);
});
Expand Down
6 changes: 4 additions & 2 deletions standalone.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ const log = require('./logger');

log.info('cz-customizable standalone version');

const commit = commitMessage => {
const commit = (commitMessage, options) => {
try {
execSync(`git commit -m "${commitMessage}"`, { stdio: [0, 1, 2] });
const args = [`commit`, `-m`, `"${commitMessage}"`, ...((options && options.args) || [])];
const argsString = args.toString().replace(/,/g, ' ');
execSync(`git ${argsString}`, { stdio: 'inherit' });
} catch (error) {
log.error('>>> ERROR', error.error);
}
Expand Down