@eljs/utils
Version:
Collection of nodejs utility.
132 lines (130 loc) • 5.08 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/path/root.ts
var root_exports = {};
__export(root_exports, {
getBunWorkspaceRoot: () => getBunWorkspaceRoot,
getLernaWorkspaceRoot: () => getLernaWorkspaceRoot,
getNpmWorkspaceRoot: () => getNpmWorkspaceRoot,
getPnpmWorkspaceRoot: () => getPnpmWorkspaceRoot,
getWorkspaceRoot: () => getWorkspaceRoot,
getWorkspaces: () => getWorkspaces,
getYarnWorkspaceRoot: () => getYarnWorkspaceRoot
});
module.exports = __toCommonJS(root_exports);
var import_file = require("../file");
var import_npm = require("../npm");
var import_find_up = __toESM(require("find-up"));
var import_glob = require("glob");
var import_js_yaml = __toESM(require("js-yaml"));
var import_node_path = __toESM(require("node:path"));
async function getPnpmWorkspaceRoot(cwd) {
const yaml2 = await (0, import_find_up.default)(["pnpm-lock.yaml", "pnpm-workspace.yaml"], {
cwd
});
return yaml2 ? import_node_path.default.dirname(yaml2) : "";
}
async function getYarnWorkspaceRoot(cwd) {
const lock = await (0, import_find_up.default)(["yarn.lock"], {
cwd
});
return lock ? import_node_path.default.dirname(lock) : "";
}
async function getLernaWorkspaceRoot(cwd) {
const json = await (0, import_find_up.default)(["lerna.json"], {
cwd
});
return json ? import_node_path.default.dirname(json) : "";
}
async function getNpmWorkspaceRoot(cwd) {
const lock = await (0, import_find_up.default)(["package-lock.json"], {
cwd
});
return lock ? import_node_path.default.dirname(lock) : "";
}
async function getBunWorkspaceRoot(cwd) {
const lock = await (0, import_find_up.default)(["bun.lockb"], {
cwd
});
return lock ? import_node_path.default.dirname(lock) : "";
}
async function getWorkspaceRoot(cwd) {
return await getPnpmWorkspaceRoot(cwd) || await getYarnWorkspaceRoot(cwd) || await getLernaWorkspaceRoot(cwd) || await getNpmWorkspaceRoot(cwd);
}
var cache = /* @__PURE__ */ new Map();
async function getWorkspaces(cwd, relative = false) {
const cacheKey = `pkg_paths_${cwd}`;
if (cache.has(cacheKey)) {
return cache.get(cacheKey);
}
const packageManager = await (0, import_npm.getPackageManager)(cwd);
const packageRootPath = [];
let workspaces = [];
if (packageManager === "pnpm") {
const workspacePath = import_node_path.default.resolve(cwd, "pnpm-workspace.yaml");
if (await (0, import_file.isPathExists)(workspacePath)) {
workspaces = import_js_yaml.default.load(await (0, import_file.readFile)(workspacePath)).packages;
}
} else {
const pkgJsonPath = import_node_path.default.resolve(cwd, "package.json");
const pkg = await (0, import_file.readJson)(pkgJsonPath);
workspaces = (pkg == null ? void 0 : pkg.workspaces) || [];
}
if (workspaces == null ? void 0 : workspaces.length) {
for (let matcher of workspaces) {
matcher = matcher.replace(/\/\*+$/, "/*");
if (matcher.endsWith("/*")) {
let rootPath = import_glob.glob.sync(matcher, {
cwd,
ignore: "*/*.*"
});
if (!relative) {
rootPath = rootPath.map((pkgPath) => {
return `${cwd}/${pkgPath}`;
});
}
packageRootPath.push(...rootPath);
} else if (await (0, import_file.isPathExists)(import_node_path.default.resolve(cwd, matcher))) {
packageRootPath.push(relative ? matcher : `${cwd}/${matcher}`);
}
}
} else {
packageRootPath.push(cwd);
}
return packageRootPath;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
getBunWorkspaceRoot,
getLernaWorkspaceRoot,
getNpmWorkspaceRoot,
getPnpmWorkspaceRoot,
getWorkspaceRoot,
getWorkspaces,
getYarnWorkspaceRoot
});