-
Notifications
You must be signed in to change notification settings - Fork 21
refactor(tree-walker): re-abstract tree loading #566
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@nodesecure/tree-walker": minor | ||
| --- | ||
|
|
||
| Re-abstract tree loading with NPM arborist |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* eslint-disable func-style */ | ||
| // Import Third-party Dependencies | ||
| import Arborist from "@npmcli/arborist"; | ||
| import * as iter from "itertools"; | ||
|
|
||
| type EdgeOut = [string, Arborist.Edge]; | ||
|
|
||
| export interface TreeDependenciesOptions { | ||
| includeDevDeps?: boolean; | ||
| } | ||
|
|
||
| export class TreeDependencies { | ||
| dependencies: Map<string, Arborist.Node>; | ||
|
|
||
| static fromArboristNode( | ||
| node: Arborist.Node, | ||
| options: TreeDependenciesOptions = {} | ||
| ): TreeDependencies { | ||
| const { includeDevDeps = false } = options; | ||
|
|
||
| const shouldIncludeEdge = ([packageName, edge]: EdgeOut) => { | ||
| const { to } = edge; | ||
| if (to === null) { | ||
| return []; | ||
| } | ||
|
|
||
| const shouldInclude = includeDevDeps || to.dev === false || to.isWorkspace; | ||
| if (!shouldInclude) { | ||
| return []; | ||
| } | ||
|
|
||
| const targetNode = to.isWorkspace ? to.target : to; | ||
|
|
||
| return [[packageName, targetNode] as const]; | ||
| }; | ||
|
|
||
| const dependencies = new Map( | ||
| iter.flatmap(node.edgesOut.entries(), shouldIncludeEdge) | ||
| ); | ||
|
|
||
| return new TreeDependencies(dependencies); | ||
| } | ||
|
|
||
| constructor( | ||
| dependencies: Map<string, Arborist.Node> | ||
| ) { | ||
| this.dependencies = dependencies; | ||
| } | ||
| } |
This file contains hidden or 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
31 changes: 31 additions & 0 deletions
31
workspaces/tree-walker/test/fixtures/tree-loader-virtual/package-lock.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
workspaces/tree-walker/test/fixtures/tree-loader-virtual/package.json
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "name": "tree-loader-virtual", | ||
| "version": "1.0.0", | ||
| "description": "", | ||
| "main": "index.js", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| }, | ||
| "keywords": [], | ||
| "type": "commonjs", | ||
| "dependencies": { | ||
| "@types/node": "^24.10.1" | ||
| } | ||
| } |
105 changes: 105 additions & 0 deletions
105
workspaces/tree-walker/test/npm/LocalDependencyTreeLoader.spec.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| // Import Node.js Dependencies | ||
| import { afterEach, beforeEach, describe, it } from "node:test"; | ||
| import path from "node:path"; | ||
| import fs from "node:fs"; | ||
| import os from "node:os"; | ||
| import assert from "node:assert"; | ||
| import { spawnSync } from "node:child_process"; | ||
| import { fileURLToPath } from "node:url"; | ||
|
|
||
| // Import Internal Dependencies | ||
| import { | ||
| LocalDependencyTreeLoader | ||
| } from "../../src/npm/LocalDependencyTreeLoader.js"; | ||
|
|
||
| // CONSTANTS | ||
| const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
| const kFixturesDir = path.join(__dirname, "..", "fixtures"); | ||
|
|
||
| describe("LocalDependencyTreeLoader", () => { | ||
| describe("arborist.loadVirtual()", () => { | ||
| it("should load a simple dependency tree using package-lock.json", async() => { | ||
| const treeLoader = new LocalDependencyTreeLoader(); | ||
|
|
||
| const { dependencies } = await treeLoader.load( | ||
| path.join(kFixturesDir, "tree-loader-virtual") | ||
| ); | ||
|
|
||
| const dependenciesName = Array.from(dependencies.keys()); | ||
| assert.deepEqual( | ||
| dependenciesName, | ||
| ["@types/node"] | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe("arborist.loadActual()", () => { | ||
| let npmLocalProjectCloneLocation: string; | ||
|
|
||
| beforeEach(() => { | ||
| npmLocalProjectCloneLocation = fs.mkdtempSync( | ||
| path.join(os.tmpdir(), "local-dep-tree-loader-") | ||
| ); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| fs.rmSync(npmLocalProjectCloneLocation, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| it("should load a simple dependency tree using node_modules", async() => { | ||
| copyAndInstall( | ||
| path.join(kFixturesDir, "tree-loader-virtual"), | ||
| npmLocalProjectCloneLocation | ||
| ); | ||
| const treeLoader = new LocalDependencyTreeLoader(); | ||
|
|
||
| const { dependencies } = await treeLoader.load(npmLocalProjectCloneLocation); | ||
|
|
||
| const dependenciesName = Array.from(dependencies.keys()); | ||
| assert.deepEqual( | ||
| dependenciesName, | ||
| ["@types/node"] | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| interface CopyAndInstallOptions { | ||
| /** | ||
| * @default true | ||
| */ | ||
| removePackageLock?: boolean; | ||
| } | ||
|
|
||
| function copyAndInstall( | ||
| source: string, | ||
| destination: string, | ||
| options: CopyAndInstallOptions = {} | ||
| ) { | ||
| const { removePackageLock = true } = options; | ||
|
|
||
| fs.copyFileSync( | ||
| path.join(source, "package.json"), | ||
| path.join(destination, "package.json") | ||
| ); | ||
|
|
||
| spawnSync( | ||
| [ | ||
| `npm${process.platform === "win32" ? ".cmd" : ""}`, | ||
| "install", | ||
| "--prefer-offline", | ||
| "--no-audit" | ||
| ].join(" "), | ||
| { | ||
| cwd: destination, | ||
| shell: true | ||
| } | ||
| ); | ||
|
|
||
| if (removePackageLock) { | ||
| fs.rmSync( | ||
| path.join(destination, "package-lock.json"), | ||
| { force: true } | ||
| ); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.