diff --git a/package-lock.json b/package-lock.json index cd038d7..5e3995f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -31,6 +31,7 @@ "lodash": "^4.18.1", "open": "^11.0.4", "rollup": "^4.59.0", + "tslib": "^2.8.1", "winston": "^3.17.0" }, "bin": { @@ -58,7 +59,6 @@ "sinon": "^19.0.2", "ts-jest": "^29.3.1", "ts-node": "^10.9.2", - "tslib": "^2.8.1", "typescript": "^5.8.3" }, "engines": { diff --git a/package.json b/package.json index b0ee06a..065cc99 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "lodash": "^4.18.1", "open": "^11.0.4", "rollup": "^4.59.0", + "tslib": "^2.8.1", "winston": "^3.17.0" }, "devDependencies": { @@ -66,15 +67,13 @@ "sinon": "^19.0.2", "ts-jest": "^29.3.1", "ts-node": "^10.9.2", - "tslib": "^2.8.1", "typescript": "^5.8.3" }, "oclif": { "bin": "csdx", "commands": "./dist/commands", "plugins": [ - "@oclif/plugin-help", - "@oclif/plugin-plugins" + "@oclif/plugin-help" ], "topicSeparator": ":", "additionalHelpFlags": [ diff --git a/test/integration/specs/package-dependencies.test.ts b/test/integration/specs/package-dependencies.test.ts new file mode 100644 index 00000000..8d447f0 --- /dev/null +++ b/test/integration/specs/package-dependencies.test.ts @@ -0,0 +1,95 @@ +import { readdirSync, readFileSync } from 'node:fs'; +import { builtinModules } from 'node:module'; +import { join, relative, resolve as resolvePath } from 'node:path'; + +import * as ts from 'typescript'; + +const projectRoot = resolvePath(__dirname, '..', '..', '..'); +const sourceRoot = join(projectRoot, 'src'); + +interface PackageJson { + dependencies?: Record; + oclif?: { plugins?: string[] }; +} + +function readPackage(): PackageJson { + return JSON.parse(readFileSync(join(projectRoot, 'package.json'), 'utf8')) as PackageJson; +} + +function compilerOptions(): ts.CompilerOptions { + const tsconfigPath = join(projectRoot, 'tsconfig.json'); + const tsconfig = ts.parseConfigFileTextToJson(tsconfigPath, readFileSync(tsconfigPath, 'utf8')); + + return ts.convertCompilerOptionsFromJson(tsconfig.config.compilerOptions, projectRoot).options; +} + +function runtimeSources(dir: string): string[] { + return readdirSync(dir, { withFileTypes: true }).flatMap((entry) => { + const path = join(dir, entry.name); + + if (entry.isDirectory()) { + return runtimeSources(path); + } + + return entry.name.endsWith('.ts') && !entry.name.endsWith('.test.ts') ? [path] : []; + }); +} + +function packageOf(specifier: string): string { + const parts = specifier.split('/'); + + return specifier.startsWith('@') ? parts.slice(0, 2).join('/') : parts[0]; +} + +function isBuiltin(specifier: string): boolean { + return specifier.startsWith('node:') || builtinModules.includes(packageOf(specifier)); +} + +function requiredPackages(): Map { + const options = compilerOptions(); + const found = new Map(); + + for (const path of runtimeSources(sourceRoot)) { + const emitted = ts.transpileModule(readFileSync(path, 'utf8'), { compilerOptions: options, fileName: path }); + + for (const match of emitted.outputText.matchAll(/\b(?:require|import)\((["'])([^"']+)\1\)/g)) { + const specifier = match[2]; + + if (specifier.startsWith('.') || isBuiltin(specifier)) { + continue; + } + + const name = packageOf(specifier); + found.set(name, [...(found.get(name) ?? []), relative(projectRoot, path)]); + } + } + + return found; +} + +describe('package dependencies', () => { + it('declares every package the compiled runtime code requires as a production dependency', () => { + const declared = Object.keys(readPackage().dependencies ?? {}); + + const required = requiredPackages(); + const undeclared = [...required.entries()] + .filter(([name]) => !declared.includes(name)) + .map(([name, files]) => `${name} (required by ${files.join(', ')})`); + + expect(required.has('tslib')).toBe(true); + expect(required.has('@oclif/core')).toBe(true); + expect(required.has('open')).toBe(true); + expect(undeclared).toEqual([]); + }); + + it('declares every oclif plugin as a production dependency', () => { + const pkg = readPackage(); + const declared = Object.keys(pkg.dependencies ?? {}); + + const plugins = pkg.oclif?.plugins ?? []; + const undeclared = plugins.filter((plugin) => !declared.includes(plugin)); + + expect(plugins).toContain('@oclif/plugin-help'); + expect(undeclared).toEqual([]); + }); +});