-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: changed npm ls to arborist actual, updated tests, added --p…
…roduction snapshot tests
- Loading branch information
1 parent
890df38
commit dbcefd1
Showing
10 changed files
with
230 additions
and
170 deletions.
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
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 |
---|---|---|
@@ -1,73 +1,38 @@ | ||
import type { DependenciesResult } from "@license-auditor/data"; | ||
import { ExecCommandException } from "../exceptions/index.js"; | ||
import { execCommand } from "./exec-command.js"; | ||
import Arborist, { type Node } from "@npmcli/arborist"; | ||
|
||
export const findNpmDepsCommand = "npm ls --all -p"; | ||
export const findNpmProdDepsCommand = "npm ls --all -p --omit=dev"; | ||
const flattenDependenciesTree = ( | ||
tree: Node, | ||
production?: boolean, | ||
): string[] => { | ||
const dependencies: string[] = []; | ||
for (const [, node] of tree.children) { | ||
if (!(production && node.dev)) { | ||
const packagePath = node.path; | ||
if (!packagePath) { | ||
throw new Error("Package found by arborist is missing a path"); | ||
} | ||
dependencies.push(packagePath); | ||
} | ||
dependencies.push(...flattenDependenciesTree(node, production)); | ||
} | ||
|
||
return dependencies; | ||
}; | ||
|
||
export async function findNpmDependencies( | ||
projectRoot: string, | ||
production?: boolean | undefined, | ||
verbose?: boolean | undefined, | ||
): Promise<DependenciesResult> { | ||
const { output, warning } = await (async () => { | ||
try { | ||
return { | ||
output: await execCommand( | ||
production ? findNpmProdDepsCommand : findNpmDepsCommand, | ||
projectRoot, | ||
verbose, | ||
), | ||
}; | ||
} catch (error) { | ||
if (error instanceof ExecCommandException) { | ||
if (/missing:.+required by/.test(error.stderr)) { | ||
return { | ||
output: error.stdout, | ||
warning: `Results incomplete because of an error. This is most likely caused by peer dependencies. Try turning legacy-peer-deps off and resolve peer dependency conflicts. Original error:\n${error.stderr}`, | ||
}; | ||
} | ||
if (/ELSPROBLEMS/.test(error.stderr)) { | ||
throw new ExecCommandException( | ||
[ | ||
"", | ||
"Unable to resolve project dependencies.", | ||
error.message, | ||
"", | ||
"Potential causes:", | ||
" - Incompatible or inconsistent version specifications in package.json.", | ||
" - Conflicts in peer dependencies.", | ||
" - Cached or outdated data in node_modules or npm cache.", | ||
"", | ||
"Suggested actions:", | ||
" 1. Inspect and resolve version conflicts in package.json.", | ||
" 2. Check and address peer dependency conflicts.", | ||
" 3. Clear the node_modules folder and reinstall dependencies with a clean state:", | ||
" remove node_modules and run npm install", | ||
" 4. Clear the npm cache to ensure no outdated or corrupted data is used:", | ||
" npm cache clean --force", | ||
].join("\n"), | ||
{ | ||
stdout: error.stdout, | ||
stderr: error.stderr, | ||
originalError: error.originalError, | ||
}, | ||
); | ||
} | ||
} | ||
throw error; | ||
} | ||
})(); | ||
|
||
// Remove the first line, as npm always prints the project root first | ||
const lines = output.split("\n").slice(1); | ||
try { | ||
const arborist = new Arborist({ path: projectRoot }); | ||
const tree = await arborist.loadActual(); | ||
|
||
const dependencies = lines | ||
.filter((line) => line.trim() !== "") | ||
.map((line) => line.trim()); | ||
const dependencies: string[] = flattenDependenciesTree(tree, production); | ||
|
||
if (warning) { | ||
return { dependencies, warning }; | ||
return { dependencies }; | ||
} catch (error) { | ||
console.log("arborist error", error); | ||
throw error; | ||
} | ||
return { dependencies }; | ||
} |
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
Oops, something went wrong.