|
| 1 | +import { dirname, join } from 'node:path'; |
| 2 | +import { fileURLToPath } from 'node:url'; |
| 3 | +import { Audit, Group, PluginConfig } from '@code-pushup/models'; |
| 4 | +import { name, version } from '../../package.json'; |
| 5 | +import { |
| 6 | + JSPackagesPluginConfig, |
| 7 | + PackageCommand, |
| 8 | + jsPackagesPluginConfigSchema, |
| 9 | +} from './config'; |
| 10 | +import { createRunnerConfig } from './runner'; |
| 11 | +import { auditDocs, outdatedDocs, pkgManagerDocs } from './utils'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Instantiates Code PushUp JS packages plugin for core config. |
| 15 | + * |
| 16 | + * @example |
| 17 | + * import coveragePlugin from '@code-pushup/js-packages-plugin' |
| 18 | + * |
| 19 | + * export default { |
| 20 | + * // ... core config ... |
| 21 | + * plugins: [ |
| 22 | + * // ... other plugins ... |
| 23 | + * await jsPackagesPlugin() |
| 24 | + * ] |
| 25 | + * } |
| 26 | + * |
| 27 | + * @returns Plugin configuration. |
| 28 | + */ |
| 29 | +export async function jsPackagesPlugin( |
| 30 | + config: JSPackagesPluginConfig = {}, |
| 31 | +): Promise<PluginConfig> { |
| 32 | + const jsPackagesPluginConfig = jsPackagesPluginConfigSchema.parse(config); |
| 33 | + const pkgManager = jsPackagesPluginConfig.packageManager; |
| 34 | + const features = [...new Set(jsPackagesPluginConfig.features)]; |
| 35 | + |
| 36 | + const runnerScriptPath = join( |
| 37 | + fileURLToPath(dirname(import.meta.url)), |
| 38 | + 'bin.js', |
| 39 | + ); |
| 40 | + |
| 41 | + const audits: Record<PackageCommand, Audit> = { |
| 42 | + audit: { |
| 43 | + slug: `${pkgManager}-audit`, |
| 44 | + title: `${pkgManager} audit`, |
| 45 | + description: `Lists ${pkgManager} audit vulnerabilities.`, |
| 46 | + docsUrl: auditDocs[pkgManager], |
| 47 | + }, |
| 48 | + outdated: { |
| 49 | + slug: `${pkgManager}-outdated`, |
| 50 | + title: `${pkgManager} outdated dependencies`, |
| 51 | + description: `Lists ${pkgManager} outdated dependencies.`, |
| 52 | + docsUrl: outdatedDocs[pkgManager], |
| 53 | + }, |
| 54 | + }; |
| 55 | + |
| 56 | + const group: Group = { |
| 57 | + slug: `${pkgManager}-package-manager`, |
| 58 | + title: `${pkgManager} package manager`, |
| 59 | + description: `Group containing both audit and dependencies command audits for the ${pkgManager} package manager.`, |
| 60 | + docsUrl: pkgManagerDocs[pkgManager], |
| 61 | + refs: features.map(feature => ({ |
| 62 | + slug: `${pkgManager}-${feature}`, |
| 63 | + weight: 1, |
| 64 | + })), |
| 65 | + }; |
| 66 | + |
| 67 | + return { |
| 68 | + slug: 'js-packages', |
| 69 | + title: 'Plugin for JS packages', |
| 70 | + icon: |
| 71 | + pkgManager === 'npm' ? 'npm' : pkgManager === 'pnpm' ? 'pnpm' : 'yarn', |
| 72 | + description: |
| 73 | + 'This plugin runs audit to uncover vulnerabilities and lists outdated dependencies. It supports npm, yarn classic and berry, pnpm package managers.', |
| 74 | + docsUrl: pkgManagerDocs[pkgManager], |
| 75 | + packageName: name, |
| 76 | + version, |
| 77 | + audits: features.map(feature => audits[feature]), |
| 78 | + groups: [group], |
| 79 | + runner: await createRunnerConfig(runnerScriptPath, jsPackagesPluginConfig), |
| 80 | + }; |
| 81 | +} |
0 commit comments