diff --git a/README.md b/README.md index c485a33..5d15584 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,7 @@ To customise your analysis, use the following options, placed in a `codehawk.jso | Option | Description | Default | |----------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------| +| `badgesDirectory` | Directory where the two maintainbility badges will be created (when enabled) | `['/generated']` | | `enableFlow` | Enable Flow support | `false` | | `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']` | | `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']` | diff --git a/codehawk.json b/codehawk.json index ab928d9..210d1d1 100644 --- a/codehawk.json +++ b/codehawk.json @@ -1,4 +1,4 @@ -{ - "skipDirectories": ["/generated", "/samples", "/test/__mocks__"], - "minimumThreshold": 40 -} +{ + "skipDirectories": ["/generated", "/samples", "/test/__mocks__"], + "minimumThreshold": 40 +} diff --git a/generated/avg-maintainability.svg b/generated/avg-maintainability.svg index acb5389..069b2b2 100644 --- a/generated/avg-maintainability.svg +++ b/generated/avg-maintainability.svg @@ -1,5 +1,5 @@ - - maintainability (avg): 54.78 + + maintainability (avg): 54.54 @@ -13,8 +13,8 @@ \ No newline at end of file diff --git a/generated/worst-maintainability.svg b/generated/worst-maintainability.svg index c481733..a415732 100644 --- a/generated/worst-maintainability.svg +++ b/generated/worst-maintainability.svg @@ -1,5 +1,5 @@ - - maintainability (worst): 45.30 + + maintainability (worst): 45.24 @@ -13,8 +13,8 @@ \ No newline at end of file diff --git a/package.json b/package.json index ab60e53..9ec30ae 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,8 @@ "reflect": "node build/index.js src", "test:coverage": "jest --coverage", "test:watch": "jest --watch", - "test": "jest --testTimeout 15000" + "test": "jest --testTimeout 15000", + "verify": "yarn build && yarn test && yarn lint && yarn prettier && yarn reflect && yarn isclean" }, "author": "Sam Brown (https://github.com/sgb-io)", "repository": { diff --git a/src/badge.ts b/src/badge.ts index 405fd18..4a97a68 100644 --- a/src/badge.ts +++ b/src/badge.ts @@ -1,6 +1,7 @@ import { badgen } from 'badgen' import fs from 'fs' -import type { ResultsSummary } from './codehawk' +import slash from 'slash' +import type { Results } from './codehawk' const getBadgeColor = (percent: number): string => { let color = 'e05d44' // 'red'; @@ -14,11 +15,22 @@ const getBadgeColor = (percent: number): string => { return color } -export const generateBadge = (resultsSummary: ResultsSummary): void => { - const { average, worst } = resultsSummary +export const generateBadge = (results: Results): void => { + const { average, worst } = results.summary + const { badgesDirectory } = results.options + const badgesPath = badgesDirectory[0] || '' // Fall back to root + const actualPath = slash(process.cwd()) + badgesPath + + if (badgesPath !== '' && !fs.existsSync(actualPath)) { + // Fire a specific error message, but also let the upstream generic handling kick in + const err = `[codehawk-cli] The directory "${badgesPath}" does not exist, please create it in order to generate badges.` + console.error(err) + throw new Error(err) + } + try { fs.writeFileSync( - 'generated/avg-maintainability.svg', + `${actualPath}/avg-maintainability.svg`, badgen({ label: 'maintainability (avg)', status: `${average.toFixed(2)}`, @@ -32,7 +44,7 @@ export const generateBadge = (resultsSummary: ResultsSummary): void => { try { fs.writeFileSync( - 'generated/worst-maintainability.svg', + `${actualPath}/worst-maintainability.svg`, badgen({ label: 'maintainability (worst)', status: `${worst.toFixed(2)}`, diff --git a/src/codehawk.test.ts b/src/codehawk.test.ts index 3fb4b5b..d0ac17d 100644 --- a/src/codehawk.test.ts +++ b/src/codehawk.test.ts @@ -15,7 +15,7 @@ const outputMatchesResult = (projectPath: string): void => { expect(output.fullResultsTree).toEqual(expected.fullResultsTree) - generateBadge(output.summary) + generateBadge(output) expect('generateBadge did not throw').toBeTruthy() } diff --git a/src/codehawk.ts b/src/codehawk.ts index 88f3083..d05f6ee 100644 --- a/src/codehawk.ts +++ b/src/codehawk.ts @@ -26,6 +26,7 @@ export interface ResultsSummary { } export interface Results { + options: AssembledOptions resultsList: FullyAnalyzedFile[] fullResultsTree: FullyAnalyzedEntity[] summary: ResultsSummary @@ -145,6 +146,7 @@ const analyzeProject = (rawPath: string, isCliContext?: boolean): Results => { } return { + options, summary, resultsList: resultsAsList, fullResultsTree: secondRunResults, diff --git a/src/coverage.ts b/src/coverage.ts index 1e39bd0..6c7146d 100644 --- a/src/coverage.ts +++ b/src/coverage.ts @@ -15,11 +15,12 @@ export const getCoverage = (dirPath: string): CoverageMapping[] => { coverage: coveredFiles[file], })) } catch (e) { - console.warn(` - Coverage not found, please generate it using instanbul/nyc. - We expect the json-summary format (coverage/coverage-summary.json). - If you don't have any tests, you can still continue. - `) + // For now, we choose to do nothing. See https://github.com/sgb-io/codehawk-cli/issues/58 + // console.warn(` + // Coverage not found, please generate it using instanbul/nyc. + // We expect the json-summary format (coverage/coverage-summary.json). + // If you don't have any tests, you can still continue. + // `) } return coverage diff --git a/src/index.ts b/src/index.ts index 82149e2..8fabab8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,7 +20,7 @@ const run = (scanDir: string, createBadge: boolean): void => { try { console.log('[codehawk-cli] Generating maintainability badge...') - generateBadge(output.summary) + generateBadge(output) console.log('[codehawk-cli] Badge was generated') } catch (e) { console.warn('[codehawk-cli] Badge was not generated') diff --git a/src/options.ts b/src/options.ts index 876b4b9..5617f3e 100644 --- a/src/options.ts +++ b/src/options.ts @@ -5,6 +5,11 @@ import { NO_CONFIGURATION_FOUND } from './consts/errors' import type { CodehawkOptions, AllOptionKeys, AssembledOptions } from './types' const baseOptions: CodehawkOptions = { + badgesDirectory: { + type: 'stringArray', + default: ['/generated'], + replaceDefault: true, + }, enableFlow: { type: 'boolean', default: false, @@ -46,11 +51,14 @@ const injectOptionValues = ({ optionKey: AllOptionKeys val: any }): AssembledOptions => { + let err const newOptions = { ...existingOptions } + switch (optionKey) { case 'enableFlow': newOptions[optionKey] = val as boolean break + case 'badgesDirectory': case 'excludeDirectories': case 'excludeFilenames': case 'skipDirectories': @@ -61,9 +69,12 @@ const injectOptionValues = ({ newOptions[optionKey] = parseInt(val, 10) break default: - throw new Error( - `Unknown option "${optionKey as string}" is not supported` - ) + // Print a friendly error but also allow the upstream generic handling to kick in + err = `[codehawk-cli] Unknown option "${ + optionKey as string + }" is not supported` + console.warn(err) + throw new Error(err) } return newOptions diff --git a/src/types/codehawk.ts b/src/types/codehawk.ts index c35187d..30677a9 100644 --- a/src/types/codehawk.ts +++ b/src/types/codehawk.ts @@ -4,6 +4,7 @@ type SupportedStringArrayKeys = | 'excludeDirectories' | 'excludeFilenames' | 'skipDirectories' + | 'badgesDirectory' type SupportedBooleanOptions = 'enableFlow' type SupportedNumberOptions = 'minimumThreshold'