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

Commit

Permalink
feat: 🎸 add help & version flags
Browse files Browse the repository at this point in the history
  • Loading branch information
theashraf committed Feb 1, 2020
1 parent 58595df commit 799fff2
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 41 deletions.
51 changes: 14 additions & 37 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, cliOtherParams} = 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 @@ -87,8 +59,8 @@ const main = async () => {
const appendedArgs = [];

// eslint-disable-next-line guard-for-in
for (const key in passThroughParams) {
const value = params[key];
for (const key in cliOtherParams) {
const value = cliOtherParams[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
115 changes: 115 additions & 0 deletions lib/parseArgs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
const meow = require('meow');

const cliOptionFlags = {
dryRun: {
type: 'boolean'
},
help: {
alias: 'h',
type: 'boolean'
},
hook: {
type: 'boolean'
},
nonInteractive: {
type: 'boolean'
},
version: {
alias: 'v',
type: 'boolean'
}
};

const cliAnswerFlags = {
body: {
type: 'string'
},
breaking: {
type: 'string'
},
issues: {
type: 'string'
},
lerna: {
type: 'string'
},
scope: {
type: 'string'
},
subject: {
type: 'string'
},
type: {
type: 'string'
}
};

const options = {
flags: {
...cliOptionFlags,
...cliAnswerFlags
},
help: `
Usage: git-cz [options]
options:
-h, --help show usage information
-v, --version print version info and exit
--non-interactive [non-interactive cli params] run git-cz non-interactive mode
non-interactive cli params:
--type
--subject
--scope
--body
--breaking
--issues
--lerna
`
};

const parseArgs = () => {
const {flags} = meow(options);

const {
dryRun,
hook,
nonInteractive,
body,
breaking,
issues,
lerna,
scope,
subject,
type,
help,
version,
...cliOtherParams
} = flags;

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

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

return {
cliAnswers,
cliOptions,
cliOtherParams
};
};

module.exports = parseArgs;
4 changes: 2 additions & 2 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 Down Expand Up @@ -66,7 +65,8 @@
"backup": false
},
"dependencies": {
"global": "^4.3.2"
"global": "^4.3.2",
"meow": "^6.0.0"
},
"release": {
"verifyConditions": [
Expand Down
83 changes: 81 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,11 @@
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"
integrity sha512-tHq6qdbT9U1IRSGf14CL0pUlULksvY9OZ+5eEgl1N7t+OA3tGvNpxJCzuKQlsNgCVwbAs670L1vcVQi8j9HjnA==

"@types/minimist@^1.2.0":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.0.tgz#69a23a3ad29caf0097f06eda59b361ee2f0639f6"
integrity sha1-aaI6OtKcrwCX8G7aWbNh7i8GOfY=

"@types/node@*", "@types/node@>= 8":
version "13.1.7"
resolved "https://registry.yarnpkg.com/@types/node/-/node-13.1.7.tgz#db51d28b8dfacfe4fb2d0da88f5eb0a2eca00675"
Expand Down Expand Up @@ -1130,12 +1135,21 @@ camelcase-keys@^4.0.0:
map-obj "^2.0.0"
quick-lru "^1.0.0"

camelcase-keys@^6.1.1:
version "6.1.2"
resolved "https://registry.yarnpkg.com/camelcase-keys/-/camelcase-keys-6.1.2.tgz#531a289aeea93249b63ec1249db9265f305041f7"
integrity sha512-QfFrU0CIw2oltVvpndW32kuJ/9YOJwUnmWrjlXt1nnJZHCaS9i6bfOpg9R4Lw8aZjStkJWM+jc0cdXjWBgVJSw==
dependencies:
camelcase "^5.3.1"
map-obj "^4.0.0"
quick-lru "^4.0.1"

camelcase@^4.0.0, camelcase@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-4.1.0.tgz#d545635be1e33c542649c69173e5de6acfae34dd"
integrity sha1-1UVjW+HjPFQmScaRc+Xeas+uNN0=

camelcase@^5.0.0:
camelcase@^5.0.0, camelcase@^5.3.1:
version "5.3.1"
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-5.3.1.tgz#e3c9b31569e106811df242f715725a1f4c494320"
integrity sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==
Expand Down Expand Up @@ -1797,7 +1811,7 @@ debuglog@^1.0.1:
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=

decamelize-keys@^1.0.0:
decamelize-keys@^1.0.0, decamelize-keys@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/decamelize-keys/-/decamelize-keys-1.1.0.tgz#d171a87933252807eb3cb61dc1c1445d078df2d9"
integrity sha1-0XGoeTMlKAfrPLYdwcFEXQeN8tk=
Expand Down Expand Up @@ -3227,6 +3241,11 @@ har-validator@~5.1.0:
ajv "^6.5.5"
har-schema "^2.0.0"

hard-rejection@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/hard-rejection/-/hard-rejection-2.1.0.tgz#1c6eda5c1685c63942766d79bb40ae773cecd883"
integrity sha512-VIZB+ibDhx7ObhAe7OVtoEbuP4h/MuOTHJ+J8h/eBXotJYl0fBgR72xDFCKgIh22OJZIOVNxBMWuhAr10r8HdA==

has-ansi@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/has-ansi/-/has-ansi-2.0.0.tgz#34f5049ce1ecdf2b0649af3ef24e45ed35416d91"
Expand Down Expand Up @@ -4750,6 +4769,11 @@ map-obj@^2.0.0:
resolved "https://registry.yarnpkg.com/map-obj/-/map-obj-2.0.0.tgz#a65cd29087a92598b8791257a523e021222ac1f9"
integrity sha1-plzSkIepJZi4eRJXpSPgISIqwfk=

map-obj@^4.0.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/map-obj/-/map-obj-4.1.0.tgz#b91221b542734b9f14256c0132c897c5d7256fd5"
integrity sha512-glc9y00wgtwcDmp7GaE/0b0OnxpNJsVf3ael/An6Fe2Q51LLwN1er6sdomLRzz5h0+yMpiYLhWYF5R7HeqVd4g==

map-visit@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/map-visit/-/map-visit-1.0.0.tgz#ecdca8f13144e660f1b5bd41f12f3479d98dfb8f"
Expand Down Expand Up @@ -4822,6 +4846,23 @@ meow@^5.0.0:
trim-newlines "^2.0.0"
yargs-parser "^10.0.0"

meow@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/meow/-/meow-6.0.0.tgz#949196fdf21d979379e3bdccb0411e60f8cffd93"
integrity sha512-x4rYsjigPBDAxY+BGuK83YLhUIqui5wYyZoqb6QJCUOs+0fiYq+i/NV4Jt8OgIfObZFxG9iTyvLDu4UTohGTFw==
dependencies:
"@types/minimist" "^1.2.0"
camelcase-keys "^6.1.1"
decamelize-keys "^1.1.0"
hard-rejection "^2.0.0"
minimist-options "^4.0.1"
normalize-package-data "^2.5.0"
read-pkg-up "^7.0.0"
redent "^3.0.0"
trim-newlines "^3.0.0"
type-fest "^0.8.1"
yargs-parser "^16.1.0"

merge-stream@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/merge-stream/-/merge-stream-2.0.0.tgz#52823629a14dd00c9770fb6ad47dc6310f2c1f60"
Expand Down Expand Up @@ -4925,6 +4966,11 @@ min-document@^2.19.0:
dependencies:
dom-walk "^0.1.0"

min-indent@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/min-indent/-/min-indent-1.0.0.tgz#cfc45c37e9ec0d8f0a0ec3dd4ef7f7c3abe39256"
integrity sha1-z8RcN+nsDY8KDsPdTvf3w6vjklY=

minimalistic-assert@^1.0.0, minimalistic-assert@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/minimalistic-assert/-/minimalistic-assert-1.0.1.tgz#2e194de044626d4a10e7f7fbc00ce73e83e4d5c7"
Expand All @@ -4950,6 +4996,14 @@ minimist-options@^3.0.1:
arrify "^1.0.1"
is-plain-obj "^1.1.0"

minimist-options@^4.0.1:
version "4.0.2"
resolved "https://registry.yarnpkg.com/minimist-options/-/minimist-options-4.0.2.tgz#29c4021373ded40d546186725e57761e4b1984a7"
integrity sha512-seq4hpWkYSUh1y7NXxzucwAN9yVlBc3Upgdjz8vLCP97jG8kaOmzYrVH/m7tQ1NYD1wdtZbSLfdy4zFmRWuc/w==
dependencies:
arrify "^1.0.1"
is-plain-obj "^1.1.0"

minimist@0.0.8:
version "0.0.8"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d"
Expand Down Expand Up @@ -6360,6 +6414,11 @@ quick-lru@^1.0.0:
resolved "https://registry.yarnpkg.com/quick-lru/-/quick-lru-1.1.0.tgz#4360b17c61136ad38078397ff11416e186dcfbb8"
integrity sha1-Q2CxfGETatOAeDl/8RQW4Ybc+7g=

quick-lru@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/quick-lru/-/quick-lru-4.0.1.tgz#5b8878f113a58217848c6482026c73e1ba57727f"
integrity sha512-ARhCpm70fzdcvNQfPoy49IaanKkTlRWF2JMzqhcJbhSFRZv7nPTvZJdcY7301IPmvW+/p0RgIWnQDLJxifsQ7g==

qw@~1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/qw/-/qw-1.0.1.tgz#efbfdc740f9ad054304426acb183412cc8b996d4"
Expand Down Expand Up @@ -6575,6 +6634,14 @@ redent@^2.0.0:
indent-string "^3.0.0"
strip-indent "^2.0.0"

redent@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/redent/-/redent-3.0.0.tgz#e557b7998316bb53c9f1f56fa626352c6963059f"
integrity sha512-6tDA8g98We0zd0GvVeMT9arEOnTw9qM03L9cJXaCjrip1OO764RDBLBfrB4cwzNGDj5OA5ioymC9GkizgWJDUg==
dependencies:
indent-string "^4.0.0"
strip-indent "^3.0.0"

redeyed@~2.1.0:
version "2.1.1"
resolved "https://registry.yarnpkg.com/redeyed/-/redeyed-2.1.1.tgz#8984b5815d99cb220469c99eeeffe38913e6cc0b"
Expand Down Expand Up @@ -7488,6 +7555,13 @@ strip-indent@^2.0.0:
resolved "https://registry.yarnpkg.com/strip-indent/-/strip-indent-2.0.0.tgz#5ef8db295d01e6ed6cbf7aab96998d7822527b68"
integrity sha1-XvjbKV0B5u1sv3qrlpmNeCJSe2g=

strip-indent@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/strip-indent/-/strip-indent-3.0.0.tgz#c32e1cee940b6b3432c771bc2c54bcce73cd3001"
integrity sha512-laJTa3Jb+VQpaC6DseHhF7dXVqHTfJPCRDaEbid/drOhgitgYku/letMUqOXFoWV0zIIUbjpdH2t+tYj4bQMRQ==
dependencies:
min-indent "^1.0.0"

strip-json-comments@2.0.1, strip-json-comments@~2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-2.0.1.tgz#3c531942e908c2697c0ec344858c286c7ca0a60a"
Expand Down Expand Up @@ -7725,6 +7799,11 @@ trim-newlines@^2.0.0:
resolved "https://registry.yarnpkg.com/trim-newlines/-/trim-newlines-2.0.0.tgz#b403d0b91be50c331dfc4b82eeceb22c3de16d20"
integrity sha1-tAPQuRvlDDMd/EuC7s6yLD3hbSA=

trim-newlines@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/trim-newlines/-/trim-newlines-3.0.0.tgz#79726304a6a898aa8373427298d54c2ee8b1cb30"
integrity sha512-C4+gOpvmxaSMKuEf9Qc134F1ZuOHVXKRbtEflf4NTtuuJDEIJ9p5PXsalL8SkeRw+qit1Mo+yuvMPAKwWg/1hA==

trim-off-newlines@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/trim-off-newlines/-/trim-off-newlines-1.0.1.tgz#9f9ba9d9efa8764c387698bcbfeb2c848f11adb3"
Expand Down

0 comments on commit 799fff2

Please sign in to comment.