From 91f283f587c3e005aa884dc528c82c3f82bca6c8 Mon Sep 17 00:00:00 2001 From: Dan Ionescu Date: Fri, 7 Jan 2022 20:44:37 +0200 Subject: [PATCH 1/2] fix: make sure all the dependencies are checked when flattening the dependencies tree (#103) --- src/helper.ts | 46 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/src/helper.ts b/src/helper.ts index 4abd5b74..4f07908e 100644 --- a/src/helper.ts +++ b/src/helper.ts @@ -66,22 +66,56 @@ export function extractFileNames( }); } +/** + * Creates a "registry" of all the dependencies of the packages found in the tree described by deps. + * @param deps + */ +const buildAllDeps = (deps?: JSONObject) => { + if (!deps) { + return {}; + } + + const dependenciesLeft = Object.entries(deps); + const allDeps = {}; + + while (dependenciesLeft.length > 0) { + const [depName, details] = dependenciesLeft.shift(); + + if ( + // we already have the dependencies for this package + depName in allDeps && + 'dependencies' in allDeps[depName] + ) { + continue; + } + + const dependencies = (details as JSONObject).dependencies; + if (dependencies) { + Object.entries(dependencies).forEach((dep) => dependenciesLeft.push(dep)); + } + + allDeps[depName] = details; + } + + return allDeps; +}; + /** * Takes a dependency graph and returns a flat list of required production dependencies for all or the filtered deps * @param deps A nested object as given by the `npm list --json` command * @param filter an array of top dependencies to whitelist (takes all dependencies if omitted) + * @param allDeps an object containing the dependencies for each package, the keys being the package names */ -export const flatDep = (deps: JSONObject, filter?: string[], originalObject?: JSONObject) => { +export const flatDep = (deps: JSONObject, filter?: string[], allDeps?: JSONObject) => { if (!deps) return []; - // keep tracks of the original list when nested - if (!originalObject) originalObject = deps; + // keep tracks of all the dependencies for all the packages + if (!allDeps) allDeps = buildAllDeps(allDeps); return Object.entries(deps).reduce((acc, [depName, details]) => { if (filter && !filter.includes(depName)) return acc; - const detailsDeps = - originalObject[depName]?.dependencies || (details as JSONObject).dependencies; - return uniq([...acc, depName, ...flatDep(detailsDeps, undefined, originalObject)]); + const detailsDeps = allDeps[depName]?.dependencies || (details as JSONObject).dependencies; + return uniq([...acc, depName, ...flatDep(detailsDeps, undefined, allDeps)]); }, []); }; From 0716acff2e1a581ccd85f5aeae4867498116d337 Mon Sep 17 00:00:00 2001 From: Dan Ionescu Date: Sat, 8 Jan 2022 21:40:33 +0200 Subject: [PATCH 2/2] fix: take the version into account (#103) --- src/helper.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/helper.ts b/src/helper.ts index 4f07908e..3bfcc67a 100644 --- a/src/helper.ts +++ b/src/helper.ts @@ -94,7 +94,8 @@ const buildAllDeps = (deps?: JSONObject) => { Object.entries(dependencies).forEach((dep) => dependenciesLeft.push(dep)); } - allDeps[depName] = details; + allDeps[depName] = allDeps[depName] || {}; + allDeps[depName][(details as JSONObject).version] = details; } return allDeps; @@ -110,11 +111,13 @@ export const flatDep = (deps: JSONObject, filter?: string[], allDeps?: JSONObjec if (!deps) return []; // keep tracks of all the dependencies for all the packages - if (!allDeps) allDeps = buildAllDeps(allDeps); + if (!allDeps) allDeps = buildAllDeps(deps); return Object.entries(deps).reduce((acc, [depName, details]) => { if (filter && !filter.includes(depName)) return acc; - const detailsDeps = allDeps[depName]?.dependencies || (details as JSONObject).dependencies; + const detailsDeps = + allDeps[depName]?.[(details as JSONObject).version]?.dependencies || + (details as JSONObject).dependencies; return uniq([...acc, depName, ...flatDep(detailsDeps, undefined, allDeps)]); }, []); };