Skip to content

Commit 4c4bffa

Browse files
committed
Cleanup diff logic and find multiple package nodes
1 parent 1926aa3 commit 4c4bffa

File tree

3 files changed

+178
-164
lines changed

3 files changed

+178
-164
lines changed

src/shadow/arborist/lib/arborist/diff.ts

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
11
import constants from '../../../../constants'
22

33
import type { SafeNode } from '../node'
4-
import type { Diff } from '@npmcli/arborist'
4+
import type { Diff as BaseDiff } from '@npmcli/arborist'
5+
6+
export type SafeDiff = Omit<
7+
BaseDiff,
8+
| 'actual'
9+
| 'children'
10+
| 'filterSet'
11+
| 'ideal'
12+
| 'leaves'
13+
| 'removed'
14+
| 'shrinkwrapInflated'
15+
| 'unchanged'
16+
> & {
17+
actual: SafeNode
18+
children: SafeDiff[]
19+
filterSet: Set<SafeNode>
20+
ideal: SafeNode
21+
leaves: SafeNode[]
22+
parent: SafeDiff | null
23+
removed: SafeNode[]
24+
shrinkwrapInflated: Set<SafeNode>
25+
unchanged: SafeNode[]
26+
}
527

628
const { LOOP_SENTINEL, NPM_REGISTRY_URL, SOCKET_CLI_FIX_PACKAGE_LOCK_FILE } =
729
constants
@@ -14,9 +36,9 @@ function getUrlOrigin(input: string): string {
1436
}
1537

1638
export type PackageDetail = {
17-
pkgid: SafeNode['pkgid']
39+
node: SafeNode
1840
origin: string
19-
existing?: SafeNode['pkgid'] | undefined
41+
existing?: SafeNode | undefined
2042
}
2143

2244
type GetPackagesToQueryFromDiffOptions = {
@@ -25,7 +47,7 @@ type GetPackagesToQueryFromDiffOptions = {
2547
}
2648

2749
export function getPackagesToQueryFromDiff(
28-
diff_: Diff | null,
50+
diff_: SafeDiff | null,
2951
options?: GetPackagesToQueryFromDiffOptions
3052
): PackageDetail[] {
3153
const {
@@ -41,7 +63,7 @@ export function getPackagesToQueryFromDiff(
4163
if (!diff_) {
4264
return details
4365
}
44-
const queue: Diff[] = [...diff_.children]
66+
const queue: SafeDiff[] = [...diff_.children]
4567
let pos = 0
4668
let { length: queueLength } = queue
4769
while (pos < queueLength) {
@@ -56,7 +78,7 @@ export function getPackagesToQueryFromDiff(
5678
// The `oldNode`, i.e. the `actual` node, will be `undefined` if the diff
5779
// action is 'ADD'.
5880
const { actual: oldNode, ideal: pkgNode } = diff
59-
let existing
81+
let existing: SafeNode | undefined
6082
let keep = false
6183
if (action === 'CHANGE') {
6284
if (pkgNode?.package.version !== oldNode?.package.version) {
@@ -65,7 +87,7 @@ export function getPackagesToQueryFromDiff(
6587
oldNode?.package.name &&
6688
oldNode.package.name === pkgNode?.package.name
6789
) {
68-
existing = oldNode.pkgid
90+
existing = oldNode
6991
}
7092
} else {
7193
// TODO: Add proper debug mode.
@@ -78,7 +100,7 @@ export function getPackagesToQueryFromDiff(
78100
const origin = getUrlOrigin(pkgNode.resolved)
79101
if (includeUnknownOrigin || origin === NPM_REGISTRY_URL) {
80102
details.push({
81-
pkgid: pkgNode.pkgid,
103+
node: pkgNode,
82104
origin,
83105
existing
84106
})
@@ -95,11 +117,10 @@ export function getPackagesToQueryFromDiff(
95117
const pkgNode = unchanged[i]!
96118
const origin = getUrlOrigin(pkgNode.resolved!)
97119
if (includeUnknownOrigin || origin === NPM_REGISTRY_URL) {
98-
const { pkgid } = pkgNode
99120
details.push({
100-
pkgid,
121+
node: pkgNode,
101122
origin,
102-
existing: pkgid
123+
existing: pkgNode
103124
})
104125
}
105126
}

src/shadow/arborist/lib/arborist/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { reify } from './reify'
22
import { arboristClassPath } from '../../../npm-paths'
33

4+
import type { SafeDiff } from './diff'
45
import type { SafeNode } from '../node'
56
import type {
67
Options as ArboristOptions,
@@ -16,10 +17,12 @@ export type ArboristClass = ArboristInstance & {
1617

1718
export type ArboristInstance = Omit<
1819
typeof BaseArborist,
19-
'auditReport' | 'idealTree' | 'reify'
20+
'actualTree' | 'auditReport' | 'diff' | 'idealTree' | 'reify'
2021
> & {
21-
auditReport?: AuditReportInstance | null
22-
idealTree: SafeNode | null
22+
auditReport?: AuditReportInstance | null | undefined
23+
actualTree?: SafeNode | null | undefined
24+
diff: SafeDiff | null
25+
idealTree?: SafeNode | null | undefined
2326
reify(options?: ArboristReifyOptions): Promise<SafeNode>
2427
}
2528

@@ -93,8 +96,6 @@ export class SafeArborist extends Arborist {
9396
options.dryRun = true
9497
options.save = false
9598
options.saveBundle = false
96-
// TODO: Make this deal with any refactor to private fields by punching the
97-
// class itself.
9899
await super.reify(...args)
99100
options.dryRun = old.dryRun
100101
options.save = old.save

0 commit comments

Comments
 (0)