workspace-tools
Version:
A collection of utilities that are useful in a git-controlled monorepo managed by one of these tools:
100 lines • 4.4 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getWorkspaceInfoFromWorkspaceRootAsync = exports.getWorkspaceInfoFromWorkspaceRoot = exports.getPackagePathsFromWorkspaceRootAsync = exports.getPackagePathsFromWorkspaceRoot = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const getPackagePaths_1 = require("../../getPackagePaths");
const getWorkspacePackageInfo_1 = require("../getWorkspacePackageInfo");
const logging_1 = require("../../logging");
/**
* Read the monorepo root package.json and get the list of package globs from its `workspaces` property.
*/
function getPackages(root) {
const packageJsonFile = path_1.default.join(root, "package.json");
let packageJson;
try {
packageJson = JSON.parse(fs_1.default.readFileSync(packageJsonFile, "utf-8"));
}
catch (e) {
throw new Error("Could not load package.json from workspaces root");
}
const { workspaces } = packageJson;
if (Array.isArray(workspaces)) {
return workspaces;
}
if (!workspaces?.packages) {
throw new Error("Could not find a workspaces object in package.json (expected if this is not a monorepo)");
}
return workspaces.packages;
}
/**
* Read the `workspaces` property the monorepo root package.json, then process the workspace globs
* into absolute package paths. Returns an empty array if the `workspaces` property is not found or
* there's some other issue.
*/
function getPackagePathsFromWorkspaceRoot(root) {
try {
const packageGlobs = getPackages(root);
return packageGlobs ? (0, getPackagePaths_1.getPackagePaths)(root, packageGlobs) : [];
}
catch (err) {
(0, logging_1.logVerboseWarning)(`Error getting package paths for ${root}`, err);
return [];
}
}
exports.getPackagePathsFromWorkspaceRoot = getPackagePathsFromWorkspaceRoot;
/**
* Read the `workspaces` property the monorepo root package.json, then process the workspace globs
* into absolute package paths. Returns an empty array if the `workspaces` property is not found or
* there's some other issue.
*/
async function getPackagePathsFromWorkspaceRootAsync(root) {
try {
const packageGlobs = getPackages(root);
return packageGlobs ? (0, getPackagePaths_1.getPackagePathsAsync)(root, packageGlobs) : [];
}
catch (err) {
(0, logging_1.logVerboseWarning)(`Error getting package paths for ${root}`, err);
return [];
}
}
exports.getPackagePathsFromWorkspaceRootAsync = getPackagePathsFromWorkspaceRootAsync;
/**
* Read the `workspaces` property the monorepo root package.json, then process the workspace globs
* into an array with names, paths, and package.json contents for each package (each "workspace"
* in npm/yarn/pnpm terms). Returns an empty array if there's any issue.
*/
function getWorkspaceInfoFromWorkspaceRoot(root) {
try {
const packagePaths = getPackagePathsFromWorkspaceRoot(root);
return (0, getWorkspacePackageInfo_1.getWorkspacePackageInfo)(packagePaths);
}
catch (err) {
(0, logging_1.logVerboseWarning)(`Error getting workspace info for ${root}`, err);
return [];
}
}
exports.getWorkspaceInfoFromWorkspaceRoot = getWorkspaceInfoFromWorkspaceRoot;
/**
* Read the `workspaces` property the monorepo root package.json, then process the workspace globs
* into an array with names, paths, and package.json contents for each package (each "workspace"
* in npm/yarn/pnpm terms). Returns an empty array if there's any issue.
*
* NOTE: As of writing, this will start promises to read all package.json files in parallel,
* without direct concurrency control.
*/
async function getWorkspaceInfoFromWorkspaceRootAsync(root) {
try {
const packagePaths = await getPackagePathsFromWorkspaceRootAsync(root);
return (0, getWorkspacePackageInfo_1.getWorkspacePackageInfoAsync)(packagePaths);
}
catch (err) {
(0, logging_1.logVerboseWarning)(`Error getting workspace info for ${root}`, err);
return [];
}
}
exports.getWorkspaceInfoFromWorkspaceRootAsync = getWorkspaceInfoFromWorkspaceRootAsync;
//# sourceMappingURL=packageJsonWorkspaces.js.map