-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugin-js-packages): implement plugin schema and configuration flow
- Loading branch information
Showing
10 changed files
with
495 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { jsPackagesPlugin } from './lib/js-packages-plugin'; | ||
|
||
export default jsPackagesPlugin; | ||
export type { JSPackagesPluginConfig } from './lib/config'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { z } from 'zod'; | ||
import { IssueSeverity, issueSeveritySchema } from '@code-pushup/models'; | ||
|
||
const packageCommandSchema = z.enum(['audit', 'outdated']); | ||
export type PackageCommand = z.infer<typeof packageCommandSchema>; | ||
|
||
const packageManagerSchema = z.enum(['npm', 'yarn', 'yarn-berry', 'pnpm']); | ||
export type PackageManager = z.infer<typeof packageManagerSchema>; | ||
|
||
const packageAuditLevelSchema = z.enum([ | ||
'info', | ||
'low', | ||
'moderate', | ||
'high', | ||
'critical', | ||
]); | ||
export type PackageAuditLevel = z.infer<typeof packageAuditLevelSchema>; | ||
|
||
const defaultAuditLevelMapping: Record<PackageAuditLevel, IssueSeverity> = { | ||
critical: 'error', | ||
high: 'error', | ||
moderate: 'warning', | ||
low: 'warning', | ||
info: 'info', | ||
}; | ||
|
||
export function fillAuditLevelMapping( | ||
mapping: Partial<Record<PackageAuditLevel, IssueSeverity>>, | ||
): Record<PackageAuditLevel, IssueSeverity> { | ||
return { | ||
critical: mapping.critical ?? defaultAuditLevelMapping.critical, | ||
high: mapping.high ?? defaultAuditLevelMapping.high, | ||
moderate: mapping.moderate ?? defaultAuditLevelMapping.moderate, | ||
low: mapping.low ?? defaultAuditLevelMapping.low, | ||
info: mapping.info ?? defaultAuditLevelMapping.info, | ||
}; | ||
} | ||
|
||
// TODO how? | ||
// export function objectKeys<T extends object>(obj: T): (keyof T)[] { | ||
// return Object.keys(obj) as (keyof T)[]; | ||
// } | ||
|
||
// function newFillAuditLevelMapping( | ||
// mapping: Partial<Record<PackageAuditLevel, IssueSeverity>>, | ||
// ): Record<PackageAuditLevel, IssueSeverity> { | ||
// return Object.fromEntries( | ||
// objectKeys(defaultAuditLevelMapping).map< | ||
// [PackageAuditLevel, IssueSeverity] | ||
// >(auditLevel => [ | ||
// auditLevel, | ||
// mapping[auditLevel] ?? defaultAuditLevelMapping[auditLevel], | ||
// ]), | ||
// ); | ||
// } | ||
|
||
export const jsPackagesPluginConfigSchema = z.object({ | ||
features: z | ||
.array(packageCommandSchema, { | ||
description: | ||
'Package manager commands to be run. Defaults to both audit and outdated.', | ||
}) | ||
.min(1) | ||
.default(['audit', 'outdated']), | ||
packageManager: packageManagerSchema | ||
.describe('Package manager to be used. Defaults to npm') | ||
.default('npm'), | ||
auditLevelMapping: z | ||
.record(packageAuditLevelSchema, issueSeveritySchema, { | ||
description: | ||
'Mapping of audit levels to issue severity. Custom mapping or overrides may be entered manually, otherwise has a default preset.', | ||
}) | ||
.default(defaultAuditLevelMapping) | ||
.transform(fillAuditLevelMapping), | ||
}); | ||
|
||
export type JSPackagesPluginConfig = z.input< | ||
typeof jsPackagesPluginConfigSchema | ||
>; | ||
|
||
export type FinalJSPackagesPluginConfig = z.infer< | ||
typeof jsPackagesPluginConfigSchema | ||
>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { IssueSeverity } from '@code-pushup/models'; | ||
import { | ||
FinalJSPackagesPluginConfig, | ||
JSPackagesPluginConfig, | ||
PackageAuditLevel, | ||
fillAuditLevelMapping, | ||
jsPackagesPluginConfigSchema, | ||
} from './config'; | ||
|
||
describe('jsPackagesPluginConfigSchema', () => { | ||
it('should accept a JS package configuration with all entities', () => { | ||
expect(() => | ||
jsPackagesPluginConfigSchema.parse({ | ||
auditLevelMapping: { moderate: 'error' }, | ||
features: ['audit'], | ||
packageManager: 'yarn', | ||
} satisfies JSPackagesPluginConfig), | ||
).not.toThrow(); | ||
}); | ||
|
||
it('should accept a minimal JS package configuration', () => { | ||
expect(() => jsPackagesPluginConfigSchema.parse({})).not.toThrow(); | ||
}); | ||
|
||
it('should fill in default values', () => { | ||
const config = jsPackagesPluginConfigSchema.parse({}); | ||
expect(config).toEqual<FinalJSPackagesPluginConfig>({ | ||
features: ['audit', 'outdated'], | ||
packageManager: 'npm', | ||
auditLevelMapping: { | ||
critical: 'error', | ||
high: 'error', | ||
moderate: 'warning', | ||
low: 'warning', | ||
info: 'info', | ||
}, | ||
}); | ||
}); | ||
|
||
it('should throw for no features', () => { | ||
expect(() => jsPackagesPluginConfigSchema.parse({ features: [] })).toThrow( | ||
'too_small', | ||
); | ||
}); | ||
}); | ||
|
||
describe('fillAuditLevelMapping', () => { | ||
it('should fill in defaults', () => { | ||
expect(fillAuditLevelMapping({})).toEqual< | ||
Record<PackageAuditLevel, IssueSeverity> | ||
>({ | ||
critical: 'error', | ||
high: 'error', | ||
moderate: 'warning', | ||
low: 'warning', | ||
info: 'info', | ||
}); | ||
}); | ||
|
||
it('should override mapping for given values', () => { | ||
expect(fillAuditLevelMapping({ high: 'warning', low: 'info' })).toEqual< | ||
Record<PackageAuditLevel, IssueSeverity> | ||
>({ | ||
critical: 'error', | ||
high: 'warning', | ||
moderate: 'warning', | ||
low: 'info', | ||
info: 'info', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { dirname, join } from 'node:path'; | ||
import { fileURLToPath } from 'node:url'; | ||
import { Audit, Group, PluginConfig } from '@code-pushup/models'; | ||
import { name, version } from '../../package.json'; | ||
import { | ||
JSPackagesPluginConfig, | ||
PackageCommand, | ||
jsPackagesPluginConfigSchema, | ||
} from './config'; | ||
import { createRunnerConfig } from './runner'; | ||
import { auditDocs, outdatedDocs, pkgManagerDocs } from './utils'; | ||
|
||
/** | ||
* Instantiates Code PushUp JS packages plugin for core config. | ||
* | ||
* @example | ||
* import coveragePlugin from '@code-pushup/js-packages-plugin' | ||
* | ||
* export default { | ||
* // ... core config ... | ||
* plugins: [ | ||
* // ... other plugins ... | ||
* await jsPackagesPlugin() | ||
* ] | ||
* } | ||
* | ||
* @returns Plugin configuration. | ||
*/ | ||
export async function jsPackagesPlugin( | ||
config: JSPackagesPluginConfig = {}, | ||
): Promise<PluginConfig> { | ||
const jsPackagesPluginConfig = jsPackagesPluginConfigSchema.parse(config); | ||
const pkgManager = jsPackagesPluginConfig.packageManager; | ||
const features = [...new Set(jsPackagesPluginConfig.features)]; | ||
|
||
const runnerScriptPath = join( | ||
fileURLToPath(dirname(import.meta.url)), | ||
'bin.js', | ||
); | ||
|
||
const audits: Record<PackageCommand, Audit> = { | ||
audit: { | ||
slug: `${pkgManager}-audit`, | ||
title: `${pkgManager} audit`, | ||
description: `Lists ${pkgManager} audit vulnerabilities.`, | ||
docsUrl: auditDocs[pkgManager], | ||
}, | ||
outdated: { | ||
slug: `${pkgManager}-outdated`, | ||
title: `${pkgManager} outdated dependencies`, | ||
description: `Lists ${pkgManager} outdated dependencies.`, | ||
docsUrl: outdatedDocs[pkgManager], | ||
}, | ||
}; | ||
|
||
const group: Group = { | ||
slug: `${pkgManager}-package-manager`, | ||
title: `${pkgManager} package manager`, | ||
description: `Group containing both audit and dependencies command audits for the ${pkgManager} package manager.`, | ||
docsUrl: pkgManagerDocs[pkgManager], | ||
refs: features.map(feature => ({ | ||
slug: `${pkgManager}-${feature}`, | ||
weight: 1, | ||
})), | ||
}; | ||
|
||
return { | ||
slug: 'js-packages', | ||
title: 'Plugin for JS packages', | ||
icon: | ||
pkgManager === 'npm' ? 'npm' : pkgManager === 'pnpm' ? 'pnpm' : 'yarn', | ||
description: | ||
'This plugin runs audit to uncover vulnerabilities and lists outdated dependencies. It supports npm, yarn classic and berry, pnpm package managers.', | ||
docsUrl: pkgManagerDocs[pkgManager], | ||
packageName: name, | ||
version, | ||
audits: features.map(feature => audits[feature]), | ||
groups: [group], | ||
runner: await createRunnerConfig(runnerScriptPath, jsPackagesPluginConfig), | ||
}; | ||
} |
Oops, something went wrong.