Skip to content

Commit 0489d4e

Browse files
authored
Add badgesDirectory option, improved error handling, suppress coverage warnings (#86)
1 parent c57e5b3 commit 0489d4e

File tree

12 files changed

+57
-28
lines changed

12 files changed

+57
-28
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ To customise your analysis, use the following options, placed in a `codehawk.jso
114114

115115
| Option | Description | Default |
116116
|----------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------|
117+
| `badgesDirectory` | Directory where the two maintainbility badges will be created (when enabled) | `['/generated']` |
117118
| `enableFlow` | Enable Flow support | `false` |
118119
| `extensions` | File extensions that should be analyzed. The default is always used, but you can add more extensions. You can use the `exclude[...]` options to exclude specific files. | `['.js', '.jsx', '.ts', '.tsx']` |
119120
| `excludeFilenames` | Filename matches that should be excluded from static analysis (but still show in the data). The default is always used, but you can add more matches to be excluded. Note that the matching is exact. The exclude list is taken into consideration after the extension list. | `['.d.ts', '.min.js', '.bundle.js']` |

codehawk.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{
2-
"skipDirectories": ["/generated", "/samples", "/test/__mocks__"],
3-
"minimumThreshold": 40
4-
}
1+
{
2+
"skipDirectories": ["/generated", "/samples", "/test/__mocks__"],
3+
"minimumThreshold": 40
4+
}

generated/avg-maintainability.svg

Lines changed: 4 additions & 4 deletions
Loading

generated/worst-maintainability.svg

Lines changed: 4 additions & 4 deletions
Loading

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"reflect": "node build/index.js src",
1717
"test:coverage": "jest --coverage",
1818
"test:watch": "jest --watch",
19-
"test": "jest --testTimeout 15000"
19+
"test": "jest --testTimeout 15000",
20+
"verify": "yarn build && yarn test && yarn lint && yarn prettier && yarn reflect && yarn isclean"
2021
},
2122
"author": "Sam Brown (https://github.com/sgb-io)",
2223
"repository": {

src/badge.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { badgen } from 'badgen'
22
import fs from 'fs'
3-
import type { ResultsSummary } from './codehawk'
3+
import slash from 'slash'
4+
import type { Results } from './codehawk'
45

56
const getBadgeColor = (percent: number): string => {
67
let color = 'e05d44' // 'red';
@@ -14,11 +15,22 @@ const getBadgeColor = (percent: number): string => {
1415
return color
1516
}
1617

17-
export const generateBadge = (resultsSummary: ResultsSummary): void => {
18-
const { average, worst } = resultsSummary
18+
export const generateBadge = (results: Results): void => {
19+
const { average, worst } = results.summary
20+
const { badgesDirectory } = results.options
21+
const badgesPath = badgesDirectory[0] || '' // Fall back to root
22+
const actualPath = slash(process.cwd()) + badgesPath
23+
24+
if (badgesPath !== '' && !fs.existsSync(actualPath)) {
25+
// Fire a specific error message, but also let the upstream generic handling kick in
26+
const err = `[codehawk-cli] The directory "${badgesPath}" does not exist, please create it in order to generate badges.`
27+
console.error(err)
28+
throw new Error(err)
29+
}
30+
1931
try {
2032
fs.writeFileSync(
21-
'generated/avg-maintainability.svg',
33+
`${actualPath}/avg-maintainability.svg`,
2234
badgen({
2335
label: 'maintainability (avg)',
2436
status: `${average.toFixed(2)}`,
@@ -32,7 +44,7 @@ export const generateBadge = (resultsSummary: ResultsSummary): void => {
3244

3345
try {
3446
fs.writeFileSync(
35-
'generated/worst-maintainability.svg',
47+
`${actualPath}/worst-maintainability.svg`,
3648
badgen({
3749
label: 'maintainability (worst)',
3850
status: `${worst.toFixed(2)}`,

src/codehawk.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const outputMatchesResult = (projectPath: string): void => {
1515

1616
expect(output.fullResultsTree).toEqual(expected.fullResultsTree)
1717

18-
generateBadge(output.summary)
18+
generateBadge(output)
1919
expect('generateBadge did not throw').toBeTruthy()
2020
}
2121

src/codehawk.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export interface ResultsSummary {
2626
}
2727

2828
export interface Results {
29+
options: AssembledOptions
2930
resultsList: FullyAnalyzedFile[]
3031
fullResultsTree: FullyAnalyzedEntity[]
3132
summary: ResultsSummary
@@ -145,6 +146,7 @@ const analyzeProject = (rawPath: string, isCliContext?: boolean): Results => {
145146
}
146147

147148
return {
149+
options,
148150
summary,
149151
resultsList: resultsAsList,
150152
fullResultsTree: secondRunResults,

src/coverage.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ export const getCoverage = (dirPath: string): CoverageMapping[] => {
1515
coverage: coveredFiles[file],
1616
}))
1717
} catch (e) {
18-
console.warn(`
19-
Coverage not found, please generate it using instanbul/nyc.
20-
We expect the json-summary format (coverage/coverage-summary.json).
21-
If you don't have any tests, you can still continue.
22-
`)
18+
// For now, we choose to do nothing. See https://github.com/sgb-io/codehawk-cli/issues/58
19+
// console.warn(`
20+
// Coverage not found, please generate it using instanbul/nyc.
21+
// We expect the json-summary format (coverage/coverage-summary.json).
22+
// If you don't have any tests, you can still continue.
23+
// `)
2324
}
2425

2526
return coverage

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const run = (scanDir: string, createBadge: boolean): void => {
2020

2121
try {
2222
console.log('[codehawk-cli] Generating maintainability badge...')
23-
generateBadge(output.summary)
23+
generateBadge(output)
2424
console.log('[codehawk-cli] Badge was generated')
2525
} catch (e) {
2626
console.warn('[codehawk-cli] Badge was not generated')

src/options.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import { NO_CONFIGURATION_FOUND } from './consts/errors'
55
import type { CodehawkOptions, AllOptionKeys, AssembledOptions } from './types'
66

77
const baseOptions: CodehawkOptions = {
8+
badgesDirectory: {
9+
type: 'stringArray',
10+
default: ['/generated'],
11+
replaceDefault: true,
12+
},
813
enableFlow: {
914
type: 'boolean',
1015
default: false,
@@ -46,11 +51,14 @@ const injectOptionValues = ({
4651
optionKey: AllOptionKeys
4752
val: any
4853
}): AssembledOptions => {
54+
let err
4955
const newOptions = { ...existingOptions }
56+
5057
switch (optionKey) {
5158
case 'enableFlow':
5259
newOptions[optionKey] = val as boolean
5360
break
61+
case 'badgesDirectory':
5462
case 'excludeDirectories':
5563
case 'excludeFilenames':
5664
case 'skipDirectories':
@@ -61,9 +69,12 @@ const injectOptionValues = ({
6169
newOptions[optionKey] = parseInt(val, 10)
6270
break
6371
default:
64-
throw new Error(
65-
`Unknown option "${optionKey as string}" is not supported`
66-
)
72+
// Print a friendly error but also allow the upstream generic handling to kick in
73+
err = `[codehawk-cli] Unknown option "${
74+
optionKey as string
75+
}" is not supported`
76+
console.warn(err)
77+
throw new Error(err)
6778
}
6879

6980
return newOptions

src/types/codehawk.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ type SupportedStringArrayKeys =
44
| 'excludeDirectories'
55
| 'excludeFilenames'
66
| 'skipDirectories'
7+
| 'badgesDirectory'
78
type SupportedBooleanOptions = 'enableFlow'
89
type SupportedNumberOptions = 'minimumThreshold'
910

0 commit comments

Comments
 (0)