Skip to content

Commit 3d93119

Browse files
committed
Simplify arborist walk and safe npm install report
1 parent 015ab6b commit 3d93119

File tree

1 file changed

+25
-59
lines changed

1 file changed

+25
-59
lines changed

src/shadow/arborist.ts

Lines changed: 25 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,9 @@ type Explanation = {
7474
} | null
7575

7676
type InstallEffect = {
77-
action: Diff['action']
7877
existing: NodeClass['pkgid'] | null
7978
pkgid: NodeClass['pkgid']
80-
resolved: NodeClass['resolved']
81-
location: NodeClass['location']
82-
oldPackage: PURLParts | null
83-
newPackage: PURLParts
79+
repository_url: string
8480
}
8581

8682
type NodeClass = Omit<
@@ -147,13 +143,6 @@ interface KnownModules {
147143
'proc-log': typeof import('proc-log')
148144
}
149145

150-
type PURLParts = {
151-
type: 'npm'
152-
namespace_and_name: string
153-
version: string
154-
repository_url: URL['href']
155-
}
156-
157146
type RequireTransformer<T extends keyof KnownModules> = (
158147
mod: KnownModules[T]
159148
) => KnownModules[T]
@@ -283,8 +272,8 @@ async function* batchScan(
283272
)
284273
> {
285274
const query = {
286-
packages: pkgIds.map(pkgid => {
287-
const { name, version } = pkgidParts(pkgid)
275+
packages: pkgIds.map(id => {
276+
const { name, version } = pkgidParts(id)
288277
return {
289278
eco: 'npm',
290279
pkg: name,
@@ -390,28 +379,25 @@ async function packagesHaveRiskyIssues(
390379
pkgs: InstallEffect[],
391380
output?: Writable
392381
): Promise<boolean> {
382+
const spinner = yoctoSpinner({
383+
stream: output
384+
})
393385
let result = false
394-
let remaining = pkgs.length
386+
let { length: remaining } = pkgs
395387
if (!remaining) {
396-
yoctoSpinner().success('No changes detected')
388+
spinner.success('No changes detected')
397389
return result
398390
}
399-
400391
const getText = () => `Looking up data for ${remaining} packages`
401-
402-
const spinner = yoctoSpinner({
403-
stream: output
404-
}).start(getText())
392+
spinner.start(getText())
405393

406394
try {
407-
for await (const pkgData of batchScan(pkgs.map(pkg => pkg.pkgid))) {
408-
let failures: { block?: boolean; raw?: any; type?: string }[] = []
409-
let displayWarning = false
410-
411-
const name = pkgData.pkg
412-
const version = pkgData.ver
395+
for await (const pkgData of batchScan(pkgs.map(p => p.pkgid))) {
396+
const { pkg: name, ver: version } = pkgData
413397
const id = `${name}@${version}`
414398

399+
let displayWarning = false
400+
let failures: { block?: boolean; raw?: any; type?: string }[] = []
415401
if (pkgData.type === 'missing') {
416402
result = true
417403
failures.push({
@@ -430,7 +416,7 @@ async function packagesHaveRiskyIssues(
430416
// Before we ask about problematic issues, check to see if they
431417
// already existed in the old version if they did, be quiet.
432418
const pkg = pkgs.find(
433-
pkg => pkg.pkgid === id && pkg.existing?.startsWith(`${name}@`)
419+
p => p.pkgid === id && p.existing?.startsWith(`${name}@`)
434420
)
435421
if (pkg?.existing) {
436422
// eslint-disable-next-line no-await-in-loop
@@ -455,7 +441,7 @@ async function packagesHaveRiskyIssues(
455441
}
456442
}
457443
if (!blocked) {
458-
const pkg = pkgs.find(pkg => pkg.pkgid === id)
444+
const pkg = pkgs.find(p => p.pkgid === id)
459445
if (pkg) {
460446
await tarball.stream(
461447
id,
@@ -469,9 +455,8 @@ async function packagesHaveRiskyIssues(
469455
}
470456
}
471457
if (displayWarning) {
472-
spinner.stop()
473-
output?.write(
474-
`(socket) ${formatter.hyperlink(id, `https://socket.dev/npm/package/${name}/overview/${version}`)} contains risks:\n`
458+
spinner.stop(
459+
`(socket) ${formatter.hyperlink(id, `https://socket.dev/npm/package/${name}/overview/${version}`)} contains risks:`
475460
)
476461
failures.sort((a, b) => (a.raw.type < b.raw.type ? -1 : 1))
477462
const lines = new Set()
@@ -495,9 +480,7 @@ async function packagesHaveRiskyIssues(
495480
}
496481
return result
497482
} finally {
498-
if (spinner.isSpinning) {
499-
spinner.stop()
500-
}
483+
spinner.stop()
501484
}
502485
}
503486

@@ -508,18 +491,11 @@ function pkgidParts(pkgid: string) {
508491
return { name, version }
509492
}
510493

511-
function toPURL(pkgid: string, resolved: string): PURLParts {
512-
const repo = resolved
513-
.replace(/#[\s\S]*$/u, '')
514-
.replace(/\?[\s\S]*$/u, '')
515-
.replace(/\/[^/]*\/-\/[\s\S]*$/u, '')
516-
const { name, version } = pkgidParts(pkgid)
517-
return {
518-
type: 'npm',
519-
namespace_and_name: name,
520-
version,
521-
repository_url: repo
522-
}
494+
function toRepoUrl(resolved: string): string {
495+
return resolved
496+
.replace(/#[\s\S]*$/, '')
497+
.replace(/\?[\s\S]*$/, '')
498+
.replace(/\/[^/]*\/-\/[\s\S]*$/, '')
523499
}
524500

525501
function walk(
@@ -560,15 +536,8 @@ function walk(
560536
) {
561537
needInfoOn.push({
562538
existing,
563-
action: diff.action,
564-
location: diff.ideal.location,
565539
pkgid: diff.ideal.pkgid,
566-
newPackage: toPURL(diff.ideal.pkgid, diff.ideal.resolved),
567-
oldPackage:
568-
diff.actual && diff.actual.resolved
569-
? toPURL(diff.actual.pkgid, diff.actual.resolved)
570-
: null,
571-
resolved: diff.ideal.resolved
540+
repository_url: toRepoUrl(diff.ideal.resolved)
572541
})
573542
}
574543
}
@@ -1300,10 +1269,7 @@ export class SafeArborist extends Arborist {
13001269
options['save'] = old.save
13011270
options['saveBundle'] = old.saveBundle
13021271
// Nothing to check, mmm already installed or all private?
1303-
if (
1304-
diff.findIndex(c => c.newPackage.repository_url === NPM_REGISTRY_URL) ===
1305-
-1
1306-
) {
1272+
if (diff.findIndex(c => c.repository_url === NPM_REGISTRY_URL) === -1) {
13071273
return await this[kRiskyReify](...args)
13081274
}
13091275
let proceed = ENV.UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE

0 commit comments

Comments
 (0)