Skip to content

Commit 4692b7d

Browse files
committed
Adds isNodeError
1 parent 321fdf0 commit 4692b7d

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

sources/corepackUtils.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export async function findInstalledVersion(installTarget: string, descriptor: De
8484
try {
8585
cacheDirectory = await fs.promises.opendir(installFolder);
8686
} catch (error) {
87-
if ((error as nodeUtils.NodeError).code === `ENOENT`) {
87+
if (nodeUtils.isNodeError(error) && error.code === `ENOENT`) {
8888
return null;
8989
} else {
9090
throw error;
@@ -181,11 +181,11 @@ async function download(installTarget: string, url: string, algo: string, binPat
181181
try {
182182
await renameSafe(downloadedBin, outputFile);
183183
} catch (err) {
184-
if ((err as nodeUtils.NodeError)?.code === `ENOENT`)
184+
if (nodeUtils.isNodeError(err) && err.code === `ENOENT`)
185185
throw new Error(`Cannot locate '${binPath}' in downloaded tarball`, {cause: err});
186186

187187
// It's alright if another process downloaded the same binary in parallel
188-
if (nodeUtils.isExistError(err as nodeUtils.NodeError)) {
188+
if (nodeUtils.isNodeError(err) && nodeUtils.isExistError(err)) {
189189
await fs.promises.rm(downloadedBin);
190190
} else {
191191
throw err;
@@ -226,7 +226,7 @@ export async function installVersion(installTarget: string, locator: Locator, {s
226226
bin: corepackData.bin,
227227
};
228228
} catch (err) {
229-
if ((err as nodeUtils.NodeError)?.code !== `ENOENT`) {
229+
if (nodeUtils.isNodeError(err) && err.code !== `ENOENT`) {
230230
throw err;
231231
}
232232
}
@@ -321,9 +321,10 @@ export async function installVersion(installTarget: string, locator: Locator, {s
321321
await renameSafe(tmpFolder, installFolder);
322322
} catch (err) {
323323
if (
324-
nodeUtils.isExistError(err as nodeUtils.NodeError) ||
324+
nodeUtils.isNodeError(err) && (
325+
nodeUtils.isExistError(err) ||
325326
// On Windows the error code is EPERM so we check if it is a directory
326-
((err as nodeUtils.NodeError).code === `EPERM` && (await fs.promises.stat(installFolder)).isDirectory())
327+
(err.code === `EPERM` && (await fs.promises.stat(installFolder)).isDirectory()))
327328
) {
328329
debugUtils.log(`Another instance of corepack installed ${locator.name}@${locator.reference}`);
329330
await fs.promises.rm(tmpFolder, {recursive: true, force: true});
@@ -370,10 +371,7 @@ async function renameUnderWindows(oldPath: fs.PathLike, newPath: fs.PathLike) {
370371
break;
371372
} catch (err) {
372373
if (
373-
(
374-
(err as nodeUtils.NodeError).code === `ENOENT` ||
375-
(err as nodeUtils.NodeError).code === `EPERM`
376-
) &&
374+
nodeUtils.isNodeError(err) && (err.code === `ENOENT` || err.code === `EPERM`) &&
377375
i < (retries - 1)
378376
) {
379377
await setTimeoutPromise(100 * 2 ** i);

sources/nodeUtils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ export interface NodeError extends Error {
44
code: string;
55
}
66

7+
export function isNodeError(err: any): err is NodeError {
8+
return `code` in err;
9+
}
10+
711
export function isExistError(err: NodeError) {
812
return err.code === `EEXIST` || err.code === `ENOTEMPTY`;
913
}

0 commit comments

Comments
 (0)