Skip to content

Commit 2116651

Browse files
committed
fix: harden installed package lifecycle
1 parent ac58f57 commit 2116651

3 files changed

Lines changed: 27 additions & 27 deletions

File tree

src/getPackageExportSizes.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ async function analyzeAllPackageExports(
2222
signal?: AbortSignal,
2323
) {
2424
throwIfAborted(signal)
25-
const packagePath = installation.sourcePath || installation.packagePath
2625
return getAllExports(
2726
installation.packageString,
28-
packagePath,
27+
installation.packagePath,
2928
installation.packageName,
3029
installation.installPath,
3130
signal,
@@ -34,9 +33,9 @@ async function analyzeAllPackageExports(
3433

3534
async function getExportsForInstalledPackage(
3635
installedPackage: InstalledPackage,
37-
options: InstallPackageOptions = {},
36+
options: InstallPackageOptions,
37+
startTime: number,
3838
) {
39-
const startTime = performance.now()
4039
const packageString = installedPackage.packageString
4140

4241
try {
@@ -57,13 +56,14 @@ export async function getPackageExports(
5756
packageInput: string | InstalledPackage,
5857
options: InstallPackageOptions = {},
5958
) {
59+
const startTime = performance.now()
6060
if (typeof packageInput === 'string') {
6161
return withInstalledPackage(packageInput, options, installedPackage =>
62-
getExportsForInstalledPackage(installedPackage, options),
62+
getExportsForInstalledPackage(installedPackage, options, startTime),
6363
)
6464
}
6565

66-
return getExportsForInstalledPackage(packageInput, options)
66+
return getExportsForInstalledPackage(packageInput, options, startTime)
6767
}
6868

6969
async function analyzePackageExportSizes(
@@ -166,9 +166,9 @@ async function analyzePackageExportSizes(
166166

167167
async function getExportSizesForInstalledPackage(
168168
installedPackage: InstalledPackage,
169-
options: GetPackageStatsOptions = {},
169+
options: GetPackageStatsOptions,
170+
startTime: number,
170171
) {
171-
const startTime = performance.now()
172172
const packageString = installedPackage.packageString
173173

174174
try {
@@ -192,11 +192,12 @@ export async function getPackageExportSizes(
192192
packageInput: string | InstalledPackage,
193193
options: GetPackageStatsOptions = {},
194194
) {
195+
const startTime = performance.now()
195196
if (typeof packageInput === 'string') {
196197
return withInstalledPackage(packageInput, options, installedPackage =>
197-
getExportSizesForInstalledPackage(installedPackage, options),
198+
getExportSizesForInstalledPackage(installedPackage, options, startTime),
198199
)
199200
}
200201

201-
return getExportSizesForInstalledPackage(packageInput, options)
202+
return getExportSizesForInstalledPackage(packageInput, options, startTime)
202203
}

src/getPackageStats.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ async function analyzePackageStats(
110110

111111
async function getStatsForInstalledPackage(
112112
installedPackage: InstalledPackage,
113-
options: GetPackageStatsOptions = {},
113+
options: GetPackageStatsOptions,
114+
startTime: number,
114115
) {
115-
const startTime = performance.now()
116116
const packageString = installedPackage.packageString
117117

118118
try {
@@ -141,11 +141,12 @@ export default async function getPackageStats(
141141
packageInput: string | InstalledPackage,
142142
options: GetPackageStatsOptions = {},
143143
) {
144+
const startTime = performance.now()
144145
if (typeof packageInput === 'string') {
145146
return withInstalledPackage(packageInput, options, installedPackage =>
146-
getStatsForInstalledPackage(installedPackage, options),
147+
getStatsForInstalledPackage(installedPackage, options, startTime),
147148
)
148149
}
149150

150-
return getStatsForInstalledPackage(packageInput, options)
151+
return getStatsForInstalledPackage(packageInput, options, startTime)
151152
}

src/installedPackage.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from 'node:path'
2+
import fs from 'node:fs/promises'
23
import type { InstallPackageOptions } from './common.types.js'
34
import { parsePackageString, throwIfAborted } from './utils/common.utils.js'
45
import InstallationUtils from './utils/installation.utils.js'
@@ -11,19 +12,14 @@ export type InstalledPackage = Readonly<{
1112
packageName: string
1213
installPath: string
1314
packagePath: string
14-
sourcePath?: string
1515
}>
1616

1717
/** Installs a package once and returns a handle that analyses can reuse. */
1818
export async function installPackage(
1919
packageString: string,
2020
options: InstallPackageOptions = {},
2121
): Promise<InstalledPackage> {
22-
const {
23-
name: packageName,
24-
isLocal,
25-
normalPath: sourcePath,
26-
} = parsePackageString(packageString)
22+
const { name: packageName, isLocal } = parsePackageString(packageString)
2723
const installPath = await InstallationUtils.preparePath(
2824
packageName,
2925
options.client,
@@ -44,19 +40,23 @@ export async function installPackage(
4440
packageName,
4541
installPath,
4642
packagePath: path.join(installPath, 'node_modules', packageName),
47-
sourcePath,
4843
})
4944
} catch (error) {
50-
if (!options.debug) {
51-
await InstallationUtils.cleanupPath(installPath)
52-
}
45+
await InstallationUtils.cleanupPath(installPath)
5346
throw error
5447
}
5548
}
5649

5750
/** Removes the workspace owned by a package installation handle. */
5851
export async function disposePackage(installedPackage: InstalledPackage) {
5952
await InstallationUtils.cleanupPath(installedPackage.installPath)
53+
try {
54+
await fs.access(installedPackage.installPath)
55+
} catch (error) {
56+
if ((error as NodeJS.ErrnoException).code === 'ENOENT') return
57+
throw error
58+
}
59+
throw new Error(`Failed to remove ${installedPackage.installPath}`)
6060
}
6161

6262
/** Runs one analysis with a temporary package installation. */
@@ -70,8 +70,6 @@ export async function withInstalledPackage<TResult>(
7070
try {
7171
return await analyze(installedPackage)
7272
} finally {
73-
if (!options.debug) {
74-
await disposePackage(installedPackage)
75-
}
73+
await disposePackage(installedPackage)
7674
}
7775
}

0 commit comments

Comments
 (0)