checkpoint

This commit is contained in:
Joseph Garrone
2024-11-02 22:39:03 +01:00
parent db37320280
commit af7a45d125
16 changed files with 677 additions and 115 deletions

View File

@ -0,0 +1,53 @@
import { dirname as pathDirname, join as pathJoin } from "path";
import { existsAsync } from "./fs.existsAsync";
import * as child_process from "child_process";
import { assert } from "tsafe/assert";
export async function getInstalledModuleDirPath(params: {
moduleName: string;
packageJsonFilePath: string;
projectDirPath: string;
}) {
const { moduleName, packageJsonFilePath, projectDirPath } = params;
const packageJsonDirPath = pathDirname(packageJsonFilePath);
common_case: {
const dirPath = pathJoin(
...[packageJsonDirPath, "node_modules", ...moduleName.split("/")]
);
if (!(await existsAsync(dirPath))) {
break common_case;
}
return dirPath;
}
node_modules_at_root_case: {
if (projectDirPath === packageJsonDirPath) {
break node_modules_at_root_case;
}
const dirPath = pathJoin(
...[projectDirPath, "node_modules", ...moduleName.split("/")]
);
if (!(await existsAsync(dirPath))) {
break node_modules_at_root_case;
}
return dirPath;
}
const dirPath = child_process
.execSync(`npm list ${moduleName}`, {
cwd: packageJsonDirPath
})
.toString("utf8")
.trim();
assert(dirPath !== "");
return dirPath;
}