Skip to content
This repository has been archived by the owner on Jun 16, 2021. It is now read-only.

Commit

Permalink
Merge pull request streamich#141 from theashraf/feat/help-version-flags
Browse files Browse the repository at this point in the history
feat: 🎸 add help & version flags
  • Loading branch information
streamich committed Feb 2, 2020
2 parents 58595df + 4c78ff9 commit 1092da5
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 42 deletions.
49 changes: 13 additions & 36 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ const {spawn} = require('child_process');
const fs = require('fs');
const {join} = require('path');
const shellescape = require('any-shell-escape');
const minimist = require('minimist');
const signale = require('signale');
const parseArgs = require('./parseArgs');
const createState = require('./createState');
const runInteractiveQuestions = require('./runInteractiveQuestions');
const runNonInteractiveMode = require('./runNonInteractiveMode');
Expand All @@ -29,42 +29,14 @@ const main = async () => {
try {
const state = createState();

const {_: args, ...params} = minimist(process.argv.slice(2));
const {cliAnswers, cliOptions, passThroughParams} = parseArgs();

for (const arg of args) {
params[arg] = true;
}

const {
'dry-run': isDryRun,
'non-interactive': isNonInteractiveMode,
hook: isHook,
body,
breaking,
issues,
lerna,
scope,
subject,
type,
...passThroughParams
} = params;

const cliAnswers = {
body,
breaking,
issues,
lerna,
scope,
subject,
type
};

if (isDryRun) {
if (cliOptions.dryRun) {
// eslint-disable-next-line no-console
console.log('Running in dry mode.');
}

if (isNonInteractiveMode) {
if (cliOptions.nonInteractive) {
await runNonInteractiveMode(state, cliAnswers);
} else {
await runInteractiveQuestions(state, cliAnswers);
Expand All @@ -76,7 +48,7 @@ const main = async () => {
* @author https://github.com/oxyii
* @see https://github.com/streamich/git-cz/issues/79
*/
if (isHook) {
if (cliOptions.hook) {
const commitMsgFile = join(getGitRootDir(), '.git', 'COMMIT_EDITMSG');

fs.writeFileSync(commitMsgFile, message);
Expand All @@ -88,7 +60,7 @@ const main = async () => {

// eslint-disable-next-line guard-for-in
for (const key in passThroughParams) {
const value = params[key];
const value = passThroughParams[key];

if (key.length === 1) {
appendedArgs.push('-' + key);
Expand All @@ -101,9 +73,14 @@ const main = async () => {
}
}

const executeCommandArgs = ['commit', '--message', message, ...appendedArgs];
const executeCommandArgs = [
'commit',
'--message',
message,
...appendedArgs
];

if (isDryRun) {
if (cliOptions.dryRun) {
const command = shellescape(['git', ...executeCommandArgs]);

// eslint-disable-next-line no-console
Expand Down
88 changes: 88 additions & 0 deletions lib/parseArgs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/* eslint-disable no-process-exit */
/* eslint-disable no-console */
/* eslint-disable id-length */
const minimist = require('minimist');
const pkg = require('../package.json');

const helpScreen = `
${pkg.description}
Usage: git-cz [options]
options:
-h, --help show usage information
-v, --version print version info and exit
--non-interactive run git-cz in non-interactive mode
non-interactive mode options:
--type type of the commit, defaults to "chore"
--subject message of the commit, defaults to "automated commit"
--scope semantic commit scope
--body extended description of the commit
--breaking description of breaking changes, if any
--issues GitHub issues this commit closed, e.g "#123"
--lerna Lerna mono-repo packages this commit affects
`;

const parseArgs = () => {
const {
// eslint-disable-next-line no-unused-vars
_: inputs,
'dry-run': dryRun,
hook,
'non-interactive': nonInteractive,
body,
breaking,
issues,
lerna,
scope,
subject,
type,
help,
version,
...passThroughParams
} = minimist(process.argv.slice(2), {
alias: {
h: 'help',
v: 'version'
},
boolean: ['version', 'help', 'non-interactive', 'hook', 'dry-run'],
string: ['type', 'subject', 'scope', 'body', 'breaking', 'issues', 'learna']
});

if (help) {
console.log(helpScreen);
process.exit();
}

if (version) {
console.log(pkg.version);
process.exit();
}

const cliOptions = {
dryRun,
help,
hook,
nonInteractive,
version
};

const cliAnswers = {
body,
breaking,
issues,
lerna,
scope,
subject,
type
};

return {
cliAnswers,
cliOptions,
passThroughParams
};
};

module.exports = parseArgs;
2 changes: 1 addition & 1 deletion lib/util/getGitRootDir.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const {execSync} = require('child_process');

const getGitRootDir = () => {
const devNull = process.platform === 'win32' ? ' nul' : '/dev/null'
const devNull = process.platform === 'win32' ? ' nul' : '/dev/null';
const dir = execSync('git rev-parse --show-toplevel 2>' + devNull)
.toString()
.trim();
Expand Down
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"eslint": "eslint lib/*.js"
},
"devDependencies": {
"semantic-release": "15.14.0",
"@semantic-release/changelog": "3.0.6",
"@semantic-release/git": "8.0.0",
"any-shell-escape": "0.1.1",
Expand All @@ -37,10 +36,11 @@
"eslint": "4.19.1",
"eslint-config-mailonline": "9.0.0",
"fuzzy": "0.1.3",
"global": "^4.3.2",
"husky": "4.2.1",
"inquirer": "6.5.2",
"inquirer-list-search-prompt": "1.0.2",
"minimist": "1.2.0",
"minimist": "^1.2.0",
"mocha": "6.2.2",
"pkg": "4.4.2",
"rimraf": "3.0.1",
Expand All @@ -65,9 +65,6 @@
"scripts": "./build/readme.js",
"backup": false
},
"dependencies": {
"global": "^4.3.2"
},
"release": {
"verifyConditions": [
"@semantic-release/changelog",
Expand Down

0 comments on commit 1092da5

Please sign in to comment.