diff --git a/package.json b/package.json index 4c2f53e..3acdd68 100644 --- a/package.json +++ b/package.json @@ -31,8 +31,8 @@ "test": "npx jest", "test:cov": "npx jest --coverage --coverageProvider v8", "lint": "npx eslint src/*.ts --cache", - "lint:fix": "yarn lint:eslint --fix", - "prepublishOnly": "yarn build" + "lint:fix": "npx yarn lint:eslint --fix", + "prepare": "npx yarn build" }, "ultra": { "concurrent": [ diff --git a/src/index.ts b/src/index.ts index bdd626a..d1e31c0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,36 @@ import { posix } from "path" +import { existsSync } from "fs" + +const PROCESS_CWD = process.cwd() +const filePathPrefix = "file:" + +function resolveAbsPath(path: string) { + return posix.resolve(PROCESS_CWD, path.replace(filePathPrefix, "")) +} + +function findLocalDependencyPaths( + cwd: string, + resolvedPaths: Set +): string[] { + const absPath = resolveAbsPath(cwd) + const absPackageJsonPath = posix.resolve(absPath, "package.json") + + if (!existsSync(absPackageJsonPath)) return [] + resolvedPaths.add(absPath) + // eslint-disable-next-line @typescript-eslint/no-var-requires + const { dependencies = {} } = require(absPackageJsonPath) + + return Object.entries(dependencies as { [key: string]: string }) + .map(([, path]) => [path, resolveAbsPath(path)]) + .filter( + ([path, resolvedAbsPath]) => + path.startsWith(filePathPrefix) && !resolvedPaths.has(resolvedAbsPath) + ) + .flatMap(([, resolvedAbsPath]) => { + resolvedPaths.add(resolvedAbsPath) + return findLocalDependencyPaths(resolvedAbsPath, resolvedPaths) + }) +} Object.keys(require.cache) .filter(m => @@ -12,11 +44,18 @@ Object.keys(require.cache) ) => { if (Array.isArray(config._)) config._.push(options.cwd) else if (config._) config._ = [config._, options.cwd] - else config._ = options.cwd + else config._ = [options.cwd] + + const resolvedPaths = new Set() + config._.flatMap(path => + findLocalDependencyPaths(resolveAbsPath(path), resolvedPaths) + ) + config._ = [...config._, ...resolvedPaths] + return parse(config, options) } }) export const tagFormat = `${ - require(posix.resolve(process.cwd(), "package.json")).name + require(posix.resolve(PROCESS_CWD, "package.json")).name }-v\${version}` diff --git a/tsconfig.json b/tsconfig.json index 1ba18b4..8fc0806 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,6 +8,7 @@ "module": "CommonJS", // Should be CommonJS for NodeJS apps "strict": true, "target": "ES2016", // any node version after 2016 is supported + "lib": ["ES2020"], "moduleResolution": "node", // should be node "resolveJsonModule": true, "esModuleInterop": true,