workspace-tools
Version:
A collection of utilities that are useful in a git-controlled monorepo managed by one of these tools:
97 lines • 4.12 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCatalogs = exports.getWorkspacesAsync = exports.getWorkspaces = exports.getPnpmCatalogs = exports.getPnpmWorkspacesAsync = exports.getPnpmWorkspaces = exports.getWorkspacePackagePaths = exports.getPnpmWorkspaceRoot = void 0;
const path_1 = __importDefault(require("path"));
const getPackagePaths_1 = require("../../getPackagePaths");
const getWorkspacePackageInfo_1 = require("../getWorkspacePackageInfo");
const readYaml_1 = require("../../lockfile/readYaml");
const logging_1 = require("../../logging");
const getWorkspaceManagerAndRoot_1 = require("./getWorkspaceManagerAndRoot");
function getPnpmWorkspaceRootAndYaml(cwd) {
const root = getPnpmWorkspaceRoot(cwd);
const pnpmWorkspacesFile = path_1.default.join(root, "pnpm-workspace.yaml");
return { root, workspaceYaml: (0, readYaml_1.readYaml)(pnpmWorkspacesFile) };
}
/** @deprecated Use `getWorkspaceManagerRoot` */
function getPnpmWorkspaceRoot(cwd) {
const root = (0, getWorkspaceManagerAndRoot_1.getWorkspaceManagerAndRoot)(cwd, undefined, "pnpm")?.root;
if (!root) {
throw new Error("Could not find pnpm workspace root from " + cwd);
}
return root;
}
exports.getPnpmWorkspaceRoot = getPnpmWorkspaceRoot;
/** Get paths for each package ("workspace") in a pnpm monorepo. */
function getWorkspacePackagePaths(cwd) {
try {
const { root, workspaceYaml } = getPnpmWorkspaceRootAndYaml(cwd);
return (0, getPackagePaths_1.getPackagePaths)(root, workspaceYaml.packages);
}
catch (err) {
(0, logging_1.logVerboseWarning)(`Error getting pnpm workspace package paths for ${cwd}`, err);
return [];
}
}
exports.getWorkspacePackagePaths = getWorkspacePackagePaths;
/**
* Get an array with names, paths, and package.json contents for each package ("workspace")
* in a pnpm monorepo.
*/
function getPnpmWorkspaces(cwd) {
try {
const packagePaths = getWorkspacePackagePaths(cwd);
return (0, getWorkspacePackageInfo_1.getWorkspacePackageInfo)(packagePaths);
}
catch (err) {
(0, logging_1.logVerboseWarning)(`Error getting pnpm workspaces for ${cwd}`, err);
return [];
}
}
exports.getPnpmWorkspaces = getPnpmWorkspaces;
exports.getWorkspaces = getPnpmWorkspaces;
/**
* Get an array with names, paths, and package.json contents for each package ("workspace")
* in a pnpm monorepo.
*/
async function getPnpmWorkspacesAsync(cwd) {
try {
const packagePaths = getWorkspacePackagePaths(cwd);
return (0, getWorkspacePackageInfo_1.getWorkspacePackageInfoAsync)(packagePaths);
}
catch (err) {
(0, logging_1.logVerboseWarning)(`Error getting pnpm workspaces for ${cwd}`, err);
return [];
}
}
exports.getPnpmWorkspacesAsync = getPnpmWorkspacesAsync;
exports.getWorkspacesAsync = getPnpmWorkspacesAsync;
/**
* Get version catalogs if present.
* Returns undefined if there's no catalog, or any issue reading or parsing.
* @see https://pnpm.io/catalogs
*/
function getPnpmCatalogs(cwd) {
try {
const { workspaceYaml } = getPnpmWorkspaceRootAndYaml(cwd);
if (!workspaceYaml.catalog && !workspaceYaml.catalogs) {
return undefined;
}
// pnpm treats catalog: and catalog:default as the same (and errors if both are defined),
// so treat the catalog named "default" as the default if present.
const { default: namedDefaultCatalog, ...namedCatalogs } = workspaceYaml.catalogs || {};
return {
default: workspaceYaml.catalog || namedDefaultCatalog,
named: Object.keys(namedCatalogs).length ? namedCatalogs : undefined,
};
}
catch (err) {
(0, logging_1.logVerboseWarning)(`Error getting pnpm catalogs for ${cwd}`, err);
return undefined;
}
}
exports.getPnpmCatalogs = getPnpmCatalogs;
exports.getCatalogs = getPnpmCatalogs;
//# sourceMappingURL=pnpm.js.map