Update the euristic for getting the NPM workspace root.

This commit is contained in:
Joseph Garrone
2024-06-03 22:37:22 +02:00
parent e873eb5123
commit 57ac5badba

View File

@ -14,6 +14,8 @@ export function getNpmWorkspaceRootDirPath(params: {
pathJoin(...[reactAppRootDirPath, ...Array(depth).fill("..")]) pathJoin(...[reactAppRootDirPath, ...Array(depth).fill("..")])
); );
assert(cwd !== pathSep, "NPM workspace not found");
try { try {
child_process.execSync("npm config get", { child_process.execSync("npm config get", {
cwd, cwd,
@ -21,48 +23,46 @@ export function getNpmWorkspaceRootDirPath(params: {
}); });
} catch (error) { } catch (error) {
if (String(error).includes("ENOWORKSPACES")) { if (String(error).includes("ENOWORKSPACES")) {
assert(cwd !== pathSep, "NPM workspace not found");
return callee(depth + 1); return callee(depth + 1);
} }
throw error; throw error;
} }
const { isExpectedDependencyFound } = (() => { const packageJsonFilePath = pathJoin(cwd, "package.json");
const packageJsonFilePath = pathJoin(cwd, "package.json");
assert(fs.existsSync(packageJsonFilePath)); if (!fs.existsSync(packageJsonFilePath)) {
return callee(depth + 1);
}
const parsedPackageJson = JSON.parse( assert(fs.existsSync(packageJsonFilePath));
fs.readFileSync(packageJsonFilePath).toString("utf8")
);
let isExpectedDependencyFound = false; const parsedPackageJson = JSON.parse(
fs.readFileSync(packageJsonFilePath).toString("utf8")
);
for (const dependenciesOrDevDependencies of [ let isExpectedDependencyFound = false;
"dependencies",
"devDependencies"
] as const) {
const dependencies = parsedPackageJson[dependenciesOrDevDependencies];
if (dependencies === undefined) { for (const dependenciesOrDevDependencies of [
continue; "dependencies",
} "devDependencies"
] as const) {
const dependencies = parsedPackageJson[dependenciesOrDevDependencies];
assert(dependencies instanceof Object); if (dependencies === undefined) {
continue;
if (dependencies[dependencyExpected] === undefined) {
continue;
}
isExpectedDependencyFound = true;
} }
return { isExpectedDependencyFound }; assert(dependencies instanceof Object);
})();
if (!isExpectedDependencyFound) { if (dependencies[dependencyExpected] === undefined) {
continue;
}
isExpectedDependencyFound = true;
}
if (!isExpectedDependencyFound && parsedPackageJson.name !== dependencyExpected) {
return callee(depth + 1); return callee(depth + 1);
} }