-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bdf15f8
commit e453420
Showing
2 changed files
with
7 additions
and
85 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -1,59 +1,19 @@ | ||
"use strict"; | ||
|
||
import log from "npmlog"; | ||
import path from "node:path"; | ||
import fs from "node:fs"; | ||
import pathExists from "path-exists"; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const childProcess = require("@lerna/child-process"); | ||
|
||
let rimrafBinPath: string | undefined; | ||
|
||
async function useRimrafBinPath(): Promise<string> { | ||
if (typeof rimrafBinPath === "string") { | ||
return rimrafBinPath; | ||
} | ||
|
||
const filePath = require.resolve("rimraf"); | ||
const directoryPath = path.basename(filePath); | ||
|
||
try { | ||
const rawFile = await fs.promises.readFile(path.join(directoryPath, "package.json"), { | ||
encoding: "utf-8", | ||
}); | ||
const file = JSON.parse(rawFile); | ||
|
||
rimrafBinPath = file.bin || require.resolve("rimraf/bin"); | ||
} catch (e) { | ||
rimrafBinPath = require.resolve("rimraf/bin"); | ||
} | ||
|
||
return rimrafBinPath as string; | ||
} | ||
import rimraf from "rimraf"; | ||
|
||
export async function rimrafDir(dirPath: string) { | ||
log.silly("rimrafDir", dirPath); | ||
// Shelling out to a child process for a noop is expensive. | ||
// Checking if `dirPath` exists to be removed is cheap. | ||
// This lets us short-circuit if we don't have anything to do. | ||
|
||
// Short-circuit if we don't have anything to do. | ||
const fileExists = await pathExists(dirPath); | ||
if (!fileExists) { | ||
return; | ||
} | ||
|
||
const cliPath = await useRimrafBinPath(); | ||
|
||
// globs only return directories with a trailing slash | ||
const slashed = path.normalize(`${dirPath}/`); | ||
const args = [cliPath, "--no-glob", slashed]; | ||
|
||
// We call this resolved CLI path in the "path/to/node path/to/cli <..args>" | ||
// pattern to avoid Windows hangups with shebangs (e.g., WSH can't handle it) | ||
return childProcess.spawn(process.execPath, args).then(() => { | ||
log.verbose("rimrafDir", "removed", dirPath); | ||
|
||
return dirPath; | ||
}); | ||
const isSuccessful = await rimraf(dirPath); | ||
if (!isSuccessful) { | ||
throw new Error(`Failed to fully remove ${dirPath}`); | ||
} | ||
log.verbose("rimrafDir", "removed", dirPath); | ||
} |