2024-02-11 18:28:58 +01:00
|
|
|
import * as child_process from "child_process";
|
|
|
|
import { join as pathJoin, resolve as pathResolve, sep as pathSep } from "path";
|
|
|
|
import { assert } from "tsafe/assert";
|
2024-05-19 23:17:45 +02:00
|
|
|
import * as fs from "fs";
|
2024-02-11 18:28:58 +01:00
|
|
|
|
2024-05-19 23:17:45 +02:00
|
|
|
export function getNpmWorkspaceRootDirPath(params: { reactAppRootDirPath: string; dependencyExpected: string }) {
|
|
|
|
const { reactAppRootDirPath, dependencyExpected } = params;
|
2024-02-11 18:28:58 +01:00
|
|
|
|
|
|
|
const npmWorkspaceRootDirPath = (function callee(depth: number): string {
|
|
|
|
const cwd = pathResolve(pathJoin(...[reactAppRootDirPath, ...Array(depth).fill("..")]));
|
|
|
|
|
|
|
|
try {
|
2024-05-17 05:13:41 +02:00
|
|
|
child_process.execSync("npm config get", { cwd, "stdio": "ignore" });
|
2024-02-11 18:28:58 +01:00
|
|
|
} catch (error) {
|
|
|
|
if (String(error).includes("ENOWORKSPACES")) {
|
|
|
|
assert(cwd !== pathSep, "NPM workspace not found");
|
|
|
|
|
|
|
|
return callee(depth + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2024-05-19 23:17:45 +02:00
|
|
|
const { isExpectedDependencyFound } = (() => {
|
|
|
|
const packageJsonFilePath = pathJoin(cwd, "package.json");
|
|
|
|
|
|
|
|
assert(fs.existsSync(packageJsonFilePath));
|
|
|
|
|
|
|
|
const parsedPackageJson = JSON.parse(fs.readFileSync(packageJsonFilePath).toString("utf8"));
|
|
|
|
|
|
|
|
let isExpectedDependencyFound = false;
|
|
|
|
|
|
|
|
for (const dependenciesOrDevDependencies of ["dependencies", "devDependencies"] as const) {
|
|
|
|
const dependencies = parsedPackageJson[dependenciesOrDevDependencies];
|
|
|
|
|
|
|
|
if (dependencies === undefined) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(dependencies instanceof Object);
|
|
|
|
|
|
|
|
if (dependencies[dependencyExpected] === undefined) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
isExpectedDependencyFound = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return { isExpectedDependencyFound };
|
|
|
|
})();
|
|
|
|
|
|
|
|
if (!isExpectedDependencyFound) {
|
|
|
|
return callee(depth + 1);
|
|
|
|
}
|
|
|
|
|
2024-02-11 18:28:58 +01:00
|
|
|
return cwd;
|
|
|
|
})(0);
|
|
|
|
|
|
|
|
return { npmWorkspaceRootDirPath };
|
|
|
|
}
|