Skip to content

Commit c57e5b3

Browse files
authored
Implement yargs, add no-badges argument (#84)
* Implement yargs, add no-badges argument * Add docs * Fix some code style
1 parent e36b2ed commit c57e5b3

File tree

5 files changed

+98
-24
lines changed

5 files changed

+98
-24
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ By default, codehawk-cli generates 2 badges (in `generated/*.svg`) when called v
129129

130130
A higher score is better (simpler, more maintainable code).
131131

132+
To avoid generating badges, use the `--no-badge` option.
133+
132134
## Complexity Metrics
133135

134136
Codehawk gathers various complexity metrics, including:

generated/avg-maintainability.svg

Lines changed: 4 additions & 4 deletions
Loading

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,16 @@
3636
"is-dotdir": "^1.0.1",
3737
"is-dotfile": "^2.0.0",
3838
"slash": "^3.0.0",
39-
"typhonjs-escomplex": "^0.1.0"
39+
"typhonjs-escomplex": "^0.1.0",
40+
"yargs": "^16.2.0"
4041
},
4142
"devDependencies": {
4243
"@types/babel__core": "^7.1.8",
4344
"@types/is-dotdir": "^1.0.0",
4445
"@types/is-dotfile": "^2.0.0",
4546
"@types/jest": "^26.0.19",
4647
"@types/node": "^14.0.11",
48+
"@types/yargs": "^15.0.12",
4749
"@typescript-eslint/eslint-plugin": "3",
4850
"@typescript-eslint/parser": "^3.3.0",
4951
"eslint": "7",

src/index.ts

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,44 @@
33
import { analyzeProject, generateBadge } from './codehawk'
44
import { formatResultsAsTable } from './cli-util'
55

6-
// Sample CLI usage: `codehawk src`
7-
const scanDir = process.argv.slice(2)[0]
8-
9-
if (scanDir && scanDir !== '') {
10-
const output = analyzeProject(`${process.cwd()}/${scanDir}`, true)
11-
const formattedAsTable = output.resultsList.slice(0, 25)
12-
console.log(formatResultsAsTable(formattedAsTable))
13-
14-
try {
15-
console.log('[codehawk-cli] Generating maintainability badge...')
16-
generateBadge(output.summary)
17-
} catch (e) {
18-
console.warn('[codehawk-cli] Badge was not generated')
6+
import yargs from 'yargs'
7+
// @types/yargs is missing typings for yargs/helpers (see https://github.com/yargs/yargs/issues/1816)
8+
// @ts-expect-error
9+
import { hideBin } from 'yargs/helpers'
10+
11+
const run = (scanDir: string, createBadge: boolean): void => {
12+
if (scanDir && scanDir !== '') {
13+
const output = analyzeProject(`${process.cwd()}/${scanDir}`, true)
14+
const formattedAsTable = output.resultsList.slice(0, 25)
15+
console.log(formatResultsAsTable(formattedAsTable))
16+
17+
if (!createBadge) {
18+
return
19+
}
20+
21+
try {
22+
console.log('[codehawk-cli] Generating maintainability badge...')
23+
generateBadge(output.summary)
24+
console.log('[codehawk-cli] Badge was generated')
25+
} catch (e) {
26+
console.warn('[codehawk-cli] Badge was not generated')
27+
}
28+
} else {
29+
console.error(
30+
'[codehawk-cli] Unable to analyze project, please ensure you provide a directory e.g. "codehawk src"'
31+
)
1932
}
20-
} else {
21-
console.error(
22-
'[codehawk-cli] Unable to analyze project, please ensure you provide a directory e.g. "codehawk src"'
23-
)
2433
}
34+
35+
const rawArgs = hideBin(process.argv)
36+
37+
const argv = yargs(rawArgs)
38+
.demandCommand(1, 'Please provide a directory argument')
39+
.describe('no-badge', 'Exclude generated badges')
40+
.usage('Usage: codehawk [dir] <options>')
41+
.epilog(
42+
'For help, file an issue at https://github.com/sgb-io/codehawk-cli/issues'
43+
)
44+
.help('help').argv
45+
46+
run(argv._[0] as string, argv.badge !== false)

yarn.lock

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,13 @@
756756
dependencies:
757757
"@types/yargs-parser" "*"
758758

759+
"@types/yargs@^15.0.12":
760+
version "15.0.12"
761+
resolved "https://registry.yarnpkg.com/@types/yargs/-/yargs-15.0.12.tgz#6234ce3e3e3fa32c5db301a170f96a599c960d74"
762+
integrity sha512-f+fD/fQAo3BCbCDlrUpznF1A5Zp9rB0noS5vnoormHSIPFKL0Z2DcUJ3Gxp5ytH4uLRNxy7AwYUC9exZzqGMAw==
763+
dependencies:
764+
"@types/yargs-parser" "*"
765+
759766
"@typescript-eslint/eslint-plugin@3":
760767
version "3.3.0"
761768
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-3.3.0.tgz#89518e5c5209a349bde161c3489b0ec187ae5d37"
@@ -1422,6 +1429,15 @@ cliui@^6.0.0:
14221429
strip-ansi "^6.0.0"
14231430
wrap-ansi "^6.2.0"
14241431

1432+
cliui@^7.0.2:
1433+
version "7.0.4"
1434+
resolved "https://registry.yarnpkg.com/cliui/-/cliui-7.0.4.tgz#a0265ee655476fc807aea9df3df8df7783808b4f"
1435+
integrity sha512-OcRE68cOsVMXp1Yvonl/fzkQOyjLSu/8bhPDfQt0e0/Eb283TKP20Fs2MqoPsr9SwA595rRCA+QMzYc9nBP+JQ==
1436+
dependencies:
1437+
string-width "^4.2.0"
1438+
strip-ansi "^6.0.0"
1439+
wrap-ansi "^7.0.0"
1440+
14251441
clone-response@^1.0.2:
14261442
version "1.0.2"
14271443
resolved "https://registry.yarnpkg.com/clone-response/-/clone-response-1.0.2.tgz#d1dc973920314df67fbeb94223b4ee350239e96b"
@@ -1871,6 +1887,11 @@ es-to-primitive@^1.2.1:
18711887
is-date-object "^1.0.1"
18721888
is-symbol "^1.0.2"
18731889

1890+
escalade@^3.1.1:
1891+
version "3.1.1"
1892+
resolved "https://registry.yarnpkg.com/escalade/-/escalade-3.1.1.tgz#d8cfdc7000965c5a0174b4a82eaa5c0552742e40"
1893+
integrity sha512-k0er2gUkLf8O0zKJiAhmkTnJlTvINGv7ygDNPbeIsX/TJjGJZHuh9B2UxbsaEkmlEo9MfhrSzmhIlhRlI2GXnw==
1894+
18741895
escape-goat@^2.0.0:
18751896
version "2.1.1"
18761897
resolved "https://registry.yarnpkg.com/escape-goat/-/escape-goat-2.1.1.tgz#1b2dc77003676c457ec760b2dc68edb648188675"
@@ -2457,7 +2478,7 @@ gensync@^1.0.0-beta.1:
24572478
resolved "https://registry.yarnpkg.com/gensync/-/gensync-1.0.0-beta.1.tgz#58f4361ff987e5ff6e1e7a210827aa371eaac269"
24582479
integrity sha512-r8EC6NO1sngH/zdD9fiRDLdcgnbayXah+mLgManTaIZJqEC1MZstmnox8KpnI2/fxQwrp5OpCOYWLp4rBl4Jcg==
24592480

2460-
get-caller-file@^2.0.1:
2481+
get-caller-file@^2.0.1, get-caller-file@^2.0.5:
24612482
version "2.0.5"
24622483
resolved "https://registry.yarnpkg.com/get-caller-file/-/get-caller-file-2.0.5.tgz#4f94412a82db32f36e3b0b9741f8a97feb031f7e"
24632484
integrity sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==
@@ -6139,6 +6160,15 @@ wrap-ansi@^6.2.0:
61396160
string-width "^4.1.0"
61406161
strip-ansi "^6.0.0"
61416162

6163+
wrap-ansi@^7.0.0:
6164+
version "7.0.0"
6165+
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
6166+
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
6167+
dependencies:
6168+
ansi-styles "^4.0.0"
6169+
string-width "^4.1.0"
6170+
strip-ansi "^6.0.0"
6171+
61426172
wrappy@1:
61436173
version "1.0.2"
61446174
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"
@@ -6186,6 +6216,11 @@ y18n@^4.0.0:
61866216
resolved "https://registry.yarnpkg.com/y18n/-/y18n-4.0.0.tgz#95ef94f85ecc81d007c264e190a120f0a3c8566b"
61876217
integrity sha512-r9S/ZyXu/Xu9q1tYlpsLIsa3EeLXXk0VwlxqTcFRfg9EhMW+17kbt9G0NrgCmhGb5vT2hyhJZLfDGx+7+5Uj/w==
61886218

6219+
y18n@^5.0.5:
6220+
version "5.0.5"
6221+
resolved "https://registry.yarnpkg.com/y18n/-/y18n-5.0.5.tgz#8769ec08d03b1ea2df2500acef561743bbb9ab18"
6222+
integrity sha512-hsRUr4FFrvhhRH12wOdfs38Gy7k2FFzB9qgN9v3aLykRq0dRcdcpz5C9FxdS2NuhOrI/628b/KSTJ3rwHysYSg==
6223+
61896224
yallist@^4.0.0:
61906225
version "4.0.0"
61916226
resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72"
@@ -6196,7 +6231,7 @@ yaml@^1.10.0:
61966231
resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.10.0.tgz#3b593add944876077d4d683fee01081bd9fff31e"
61976232
integrity sha512-yr2icI4glYaNG+KWONODapy2/jDdMSDnrONSjblABjD9B4Z5LgiircSt8m8sRZFNi08kG9Sm0uSHtEmP3zaEGg==
61986233

6199-
[email protected], yargs-parser@^20.2.3:
6234+
[email protected], yargs-parser@^20.2.2, yargs-parser@^20.2.3:
62006235
version "20.2.4"
62016236
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-20.2.4.tgz#b42890f14566796f85ae8e3a25290d205f154a54"
62026237
integrity sha512-WOkpgNhPTlE73h4VFAFsOnomJVaovO8VqLDzy5saChRBFQFBoMYirowyW+Q9HB4HFF4Z7VZTiG3iSzJJA29yRA==
@@ -6226,6 +6261,19 @@ yargs@^15.4.1:
62266261
y18n "^4.0.0"
62276262
yargs-parser "^18.1.2"
62286263

6264+
yargs@^16.2.0:
6265+
version "16.2.0"
6266+
resolved "https://registry.yarnpkg.com/yargs/-/yargs-16.2.0.tgz#1c82bf0f6b6a66eafce7ef30e376f49a12477f66"
6267+
integrity sha512-D1mvvtDG0L5ft/jGWkLpG1+m0eQxOfaBvTNELraWj22wSVUMWxZUvYgJYcKh6jGGIkJFhH4IZPQhR4TKpc8mBw==
6268+
dependencies:
6269+
cliui "^7.0.2"
6270+
escalade "^3.1.1"
6271+
get-caller-file "^2.0.5"
6272+
require-directory "^2.1.1"
6273+
string-width "^4.2.0"
6274+
y18n "^5.0.5"
6275+
yargs-parser "^20.2.2"
6276+
62296277
yocto-queue@^0.1.0:
62306278
version "0.1.0"
62316279
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b"

0 commit comments

Comments
 (0)