waka-pm
Version:
a pnpm supplement for enforcing consistent versions across all workspaces
69 lines • 2.81 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseWorkspaceDir = exports.identifyRootDir = exports.isSubDirOfMonoRepo = exports.isMonoRepoRoot = exports.toDirectoryOnly = exports.cwd = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const package_1 = require("./package");
exports.cwd = process.cwd();
function toDirectoryOnly(p) {
if (fs_1.default.lstatSync(p).isDirectory()) {
return p;
}
return path_1.default.dirname(p);
}
exports.toDirectoryOnly = toDirectoryOnly;
const terminalFiles = [
'pnpm-workspace.yaml',
'pnpm-workspace.yml',
'pnpm-lock.yaml',
'pnpm-lock.yml',
];
const semiTerminalFiles = ['package.json', 'node_modules', '.git'];
function isMonoRepoRoot(dir) {
const containsTerminalValue = terminalFiles.some((file) => fs_1.default.existsSync(path_1.default.join(dir, file)));
return containsTerminalValue;
}
exports.isMonoRepoRoot = isMonoRepoRoot;
function isSubDirOfMonoRepo(dir) {
const containsTerminalValue = semiTerminalFiles.some((file) => fs_1.default.existsSync(path_1.default.join(dir, file)));
return containsTerminalValue;
}
exports.isSubDirOfMonoRepo = isSubDirOfMonoRepo;
function identifyRootDir(currntDir = exports.cwd, lastProjectDir = null) {
if (isMonoRepoRoot(currntDir)) {
return currntDir;
}
if (currntDir === path_1.default.resolve('/')) {
throw new Error('Could not find root directory of repository');
}
if (!isSubDirOfMonoRepo(currntDir) && lastProjectDir) {
return lastProjectDir;
}
return identifyRootDir(path_1.default.join(currntDir, '..'), currntDir);
}
exports.identifyRootDir = identifyRootDir;
async function parseWorkspaceDir(repoRootDir, packageNameOrRelativePath) {
const isSub = isSubDirOfMonoRepo(exports.cwd);
const isRoot = isMonoRepoRoot(exports.cwd);
if ((packageNameOrRelativePath ?? '') === '') {
if (isSub && !isRoot) {
return exports.cwd;
}
return repoRootDir;
}
const fullPath = path_1.default.resolve(path_1.default.join(repoRootDir, packageNameOrRelativePath));
if (fs_1.default.existsSync(fullPath)) {
return fullPath;
}
if (packageNameOrRelativePath ?? '' !== '') {
const dir = await (0, package_1.getNPMPackageDir)(packageNameOrRelativePath, repoRootDir);
return dir;
}
throw new Error(`Could not determine package directory for ${packageNameOrRelativePath} or ${exports.cwd}`);
}
exports.parseWorkspaceDir = parseWorkspaceDir;
// export const rootDir = identifyRootDir();
//# sourceMappingURL=file.js.map