Simplify workspace root utilities (#276)

This commit is contained in:
Elizabeth Craig 2023-04-12 22:30:33 -07:00 коммит произвёл GitHub
Родитель 18d27beb3d
Коммит b697288732
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
16 изменённых файлов: 214 добавлений и 236 удалений

Просмотреть файл

@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Simplify workspace root utilities, and deprecate individual manager `get___WorkspaceRoot` utilities (use `getWorkspaceRoot` instead). This includes moving, deleting, or renaming certain private methods, but has no public-facing changes.",
"packageName": "workspace-tools",
"email": "elcraig@microsoft.com",
"dependentChangeType": "patch"
}

Просмотреть файл

@ -152,7 +152,7 @@ export function getPackagesByFiles(workspaceRoot: string, files: string[], ignor
// @public (undocumented)
export function getParentBranch(cwd: string): string | null;
// @public (undocumented)
// @public @deprecated (undocumented)
export function getPnpmWorkspaceRoot(cwd: string): string;
// @public
@ -164,7 +164,7 @@ export function getRecentCommitMessages(branch: string, cwd: string): string[];
// @public (undocumented)
export function getRemoteBranch(branch: string, cwd: string): string | null;
// @public (undocumented)
// @public @deprecated (undocumented)
export function getRushWorkspaceRoot(cwd: string): string;
// @public
@ -202,8 +202,10 @@ export function getUntrackedChanges(cwd: string): string[];
// @public (undocumented)
export function getUserEmail(cwd: string): string | null;
// Warning: (ae-forgotten-export) The symbol "WorkspaceManager" needs to be exported by the entry point index.d.ts
//
// @public
export function getWorkspaceRoot(cwd: string): string | undefined;
export function getWorkspaceRoot(cwd: string, preferredManager?: WorkspaceManager): string | undefined;
// @public
export function getWorkspaces(cwd: string): WorkspaceInfo;
@ -211,7 +213,7 @@ export function getWorkspaces(cwd: string): WorkspaceInfo;
// @public
export function getWorkspacesAsync(cwd: string): Promise<WorkspaceInfo>;
// @public (undocumented)
// @public @deprecated (undocumented)
export function getYarnWorkspaceRoot(cwd: string): string;
// @public
@ -447,6 +449,9 @@ export type WorkspaceInfo = {
packageJson: PackageInfo;
}[];
// @public (undocumented)
type WorkspaceManager = "yarn" | "pnpm" | "rush" | "npm" | "lerna";
// (No @packageDocumentation comment for this package)
```

Просмотреть файл

@ -1,9 +1,5 @@
import { cleanupFixtures, setupFixture } from "workspace-tools-scripts/jest/setupFixture";
import { getYarnWorkspaceRoot } from "../workspaces/implementations/yarn";
import { getPnpmWorkspaceRoot } from "../workspaces/implementations/pnpm";
import { getRushWorkspaceRoot } from "../workspaces/implementations/rush";
import { getNpmWorkspaceRoot } from "../workspaces/implementations/npm";
import { getLernaWorkspaceRoot } from "../workspaces/implementations/lerna";
import { getWorkspaceRoot } from "../workspaces/getWorkspaceRoot";
describe("getWorkspaceRoot", () => {
afterAll(() => {
@ -12,38 +8,26 @@ describe("getWorkspaceRoot", () => {
it("handles yarn workspace", () => {
const repoRoot = setupFixture("monorepo");
const workspaceRoot = getYarnWorkspaceRoot(repoRoot);
expect(workspaceRoot).toBe(repoRoot);
expect(getWorkspaceRoot(repoRoot)).toBe(repoRoot);
});
it("handles pnpm workspace", () => {
const repoRoot = setupFixture("monorepo-pnpm");
const workspaceRoot = getPnpmWorkspaceRoot(repoRoot);
expect(workspaceRoot).toBe(repoRoot);
expect(getWorkspaceRoot(repoRoot)).toBe(repoRoot);
});
it("handles rush workspace", () => {
const repoRoot = setupFixture("monorepo-rush-pnpm");
const workspaceRoot = getRushWorkspaceRoot(repoRoot);
expect(workspaceRoot).toBe(repoRoot);
expect(getWorkspaceRoot(repoRoot)).toBe(repoRoot);
});
it("handles npm workspace", () => {
const repoRoot = setupFixture("monorepo-npm");
const workspaceRoot = getNpmWorkspaceRoot(repoRoot);
expect(getWorkspaceRoot(repoRoot)).toBe(repoRoot);
});
expect(workspaceRoot).toBe(repoRoot);
});
});
describe("getLernaWorkspaceRoot()", () => {
it("gets the root of the workspace", async () => {
const repoRoot = await setupFixture("monorepo-lerna-npm");
const workspaceRoot = getLernaWorkspaceRoot(repoRoot);
expect(workspaceRoot).toBe(repoRoot);
it("handles lerna workspace", () => {
const repoRoot = setupFixture("monorepo-lerna-npm");
expect(getWorkspaceRoot(repoRoot)).toBe(repoRoot);
});
});

Просмотреть файл

@ -1,7 +1,7 @@
import path from "path";
import { cleanupFixtures, setupFixture } from "workspace-tools-scripts/jest/setupFixture";
import { getWorkspaceManager } from "../workspaces/implementations";
import { getWorkspaceManagerAndRoot } from "../workspaces/implementations";
import { getYarnWorkspaces, getYarnWorkspacesAsync } from "../workspaces/implementations/yarn";
import { getPnpmWorkspaces } from "../workspaces/implementations/pnpm";
import { getRushWorkspaces } from "../workspaces/implementations/rush";
@ -22,7 +22,7 @@ describe("getWorkspaces", () => {
it("gets the name and path of the workspaces", async () => {
const packageRoot = setupFixture("monorepo");
expect(getWorkspaceManager(packageRoot, new Map())).toBe("yarn");
expect(getWorkspaceManagerAndRoot(packageRoot, new Map())).toEqual({ manager: "yarn", root: packageRoot });
const workspacesPackageInfo = await getWorkspaces(packageRoot);
@ -38,7 +38,7 @@ describe("getWorkspaces", () => {
it("gets the name and path of the workspaces against a packages spec of an individual package", async () => {
const packageRoot = setupFixture("monorepo-globby");
expect(getWorkspaceManager(packageRoot, new Map())).toBe("yarn");
expect(getWorkspaceManagerAndRoot(packageRoot, new Map())).toEqual({ manager: "yarn", root: packageRoot });
const workspacesPackageInfo = await getWorkspaces(packageRoot);
@ -58,7 +58,7 @@ describe("getWorkspaces", () => {
it("gets the name and path of the workspaces", () => {
const packageRoot = setupFixture("monorepo-pnpm");
expect(getWorkspaceManager(packageRoot, new Map())).toBe("pnpm");
expect(getWorkspaceManagerAndRoot(packageRoot, new Map())).toEqual({ manager: "pnpm", root: packageRoot });
const workspacesPackageInfo = getPnpmWorkspaces(packageRoot);
@ -76,7 +76,7 @@ describe("getWorkspaces", () => {
it("gets the name and path of the workspaces", () => {
const packageRoot = setupFixture("monorepo-rush-pnpm");
expect(getWorkspaceManager(packageRoot, new Map())).toBe("rush");
expect(getWorkspaceManagerAndRoot(packageRoot, new Map())).toEqual({ manager: "rush", root: packageRoot });
const workspacesPackageInfo = getRushWorkspaces(packageRoot);
@ -94,7 +94,7 @@ describe("getWorkspaces", () => {
it("gets the name and path of the workspaces", () => {
const packageRoot = setupFixture("monorepo-rush-yarn");
expect(getWorkspaceManager(packageRoot, new Map())).toBe("rush");
expect(getWorkspaceManagerAndRoot(packageRoot, new Map())).toEqual({ manager: "rush", root: packageRoot });
const workspacesPackageInfo = getRushWorkspaces(packageRoot);
@ -115,7 +115,7 @@ describe("getWorkspaces", () => {
it("gets the name and path of the workspaces", async () => {
const packageRoot = setupFixture("monorepo-npm");
expect(getWorkspaceManager(packageRoot, new Map())).toBe("npm");
expect(getWorkspaceManagerAndRoot(packageRoot, new Map())).toEqual({ manager: "npm", root: packageRoot });
const workspacesPackageInfo = await getWorkspaces(packageRoot);
@ -131,7 +131,7 @@ describe("getWorkspaces", () => {
it("gets the name and path of the workspaces using the shorthand configuration", async () => {
const packageRoot = setupFixture("monorepo-shorthand");
expect(getWorkspaceManager(packageRoot, new Map())).toBe("npm");
expect(getWorkspaceManagerAndRoot(packageRoot, new Map())).toEqual({ manager: "npm", root: packageRoot });
const workspacesPackageInfo = await getWorkspaces(packageRoot);
@ -151,7 +151,7 @@ describe("getWorkspaces", () => {
it("gets the name and path of the workspaces", async () => {
const packageRoot = setupFixture("monorepo-lerna-npm");
expect(getWorkspaceManager(packageRoot, new Map())).toBe("lerna");
expect(getWorkspaceManagerAndRoot(packageRoot, new Map())).toEqual({ manager: "lerna", root: packageRoot });
const workspacesPackageInfo = getLernaWorkspaces(packageRoot);

Просмотреть файл

@ -8,9 +8,5 @@ import { WorkspaceInfo } from "../types/WorkspaceInfo";
* @returns Package path if found, or undefined
*/
export function findWorkspacePath(workspaces: WorkspaceInfo, packageName: string): string | undefined {
const workspace = workspaces.find(({ name }) => name === packageName);
if (workspace) {
return workspace.path;
}
return workspaces.find(({ name }) => name === packageName)?.path;
}

Просмотреть файл

@ -1,10 +1,12 @@
import { getWorkspaceUtilities } from "./implementations";
import { WorkspaceManager } from "./WorkspaceManager";
import { getWorkspaceManagerAndRoot } from "./implementations";
/**
* Get the root directory of a workspace/monorepo, defined as the directory where the workspace
* manager config file is located.
* @param cwd Start searching from here
* @param preferredManager Search for only this manager's config file
*/
export function getWorkspaceRoot(cwd: string): string | undefined {
const impl = getWorkspaceUtilities(cwd);
return impl?.getWorkspaceRoot(cwd);
export function getWorkspaceRoot(cwd: string, preferredManager?: WorkspaceManager): string | undefined {
return getWorkspaceManagerAndRoot(cwd, undefined, preferredManager)?.root;
}

Просмотреть файл

@ -1,4 +1,4 @@
import { getWorkspaceUtilities, getWorkspaceManager } from "./implementations";
import { getWorkspaceUtilities, getWorkspaceManagerAndRoot } from "./implementations";
import { WorkspaceInfo } from "../types/WorkspaceInfo";
/**
@ -30,7 +30,7 @@ export async function getWorkspacesAsync(cwd: string): Promise<WorkspaceInfo> {
}
if (!utils.getWorkspacesAsync) {
const managerName = getWorkspaceManager(cwd);
const managerName = getWorkspaceManagerAndRoot(cwd)?.manager;
throw new Error(`${cwd} is using ${managerName} which has not been converted to async yet`);
}

Просмотреть файл

@ -0,0 +1,76 @@
import path from "path";
import { searchUp } from "../../paths";
import { WorkspaceManager } from "../WorkspaceManager";
export interface WorkspaceManagerAndRoot {
/** Workspace manager name */
manager: WorkspaceManager;
/** Workspace root, where the manager configuration file is located */
root: string;
}
const workspaceCache = new Map<string, WorkspaceManagerAndRoot | undefined>();
/**
* Files indicating the workspace root for each manager.
*
* DO NOT REORDER! The order of keys determines the precedence of the files, which is
* important for cases like lerna where lerna.json and e.g. yarn.lock may both exist.
*/
const managerFiles = {
// DO NOT REORDER! (see above)
lerna: "lerna.json",
rush: "rush.json",
yarn: "yarn.lock",
pnpm: "pnpm-workspace.yaml",
npm: "package-lock.json",
};
/**
* Get the preferred workspace manager based on `process.env.PREFERRED_WORKSPACE_MANAGER`
* (if valid).
*/
export function getPreferredWorkspaceManager(): WorkspaceManager | undefined {
const preferred = process.env.PREFERRED_WORKSPACE_MANAGER as WorkspaceManager | undefined;
return preferred && managerFiles[preferred] ? preferred : undefined;
}
/**
* Get the workspace manager name and workspace root directory for `cwd`, with caching.
* Also respects the `process.env.PREFERRED_WORKSPACE_MANAGER` override, provided the relevant
* manager file exists.
* @param cwd Directory to search up from
* @param cache Optional override cache for testing
* @param preferredManager Optional override manager (if provided, only searches for this manager's file)
* @returns Workspace manager and root, or undefined if it can't be determined
*/
export function getWorkspaceManagerAndRoot(
cwd: string,
cache?: Map<string, WorkspaceManagerAndRoot | undefined>,
preferredManager?: WorkspaceManager
): WorkspaceManagerAndRoot | undefined {
cache = cache || workspaceCache;
if (cache.has(cwd)) {
return cache.get(cwd);
}
preferredManager = preferredManager || getPreferredWorkspaceManager();
const managerFile = searchUp(
(preferredManager && managerFiles[preferredManager]) || Object.values(managerFiles),
cwd
);
if (managerFile) {
const managerFileName = path.basename(managerFile);
cache.set(cwd, {
manager: (Object.keys(managerFiles) as WorkspaceManager[]).find(
(name) => managerFiles[name] === managerFileName
)!,
root: path.dirname(managerFile),
});
} else {
// Avoid searching again if no file was found
cache.set(cwd, undefined);
}
return cache.get(cwd);
}

Просмотреть файл

@ -0,0 +1,46 @@
import { WorkspaceInfo } from "../../types/WorkspaceInfo";
import { getWorkspaceManagerAndRoot } from "./getWorkspaceManagerAndRoot";
// These must be type imports to avoid parsing the additional deps at runtime
import type * as LernaUtilities from "./lerna";
import type * as NpmUtilities from "./npm";
import type * as PnpmUtilities from "./pnpm";
import type * as RushUtilities from "./rush";
import type * as YarnUtilities from "./yarn";
export interface WorkspaceUtilities {
/**
* Get an array with names, paths, and package.json contents for each package in a workspace.
* (See `../getWorkspaces` for why it's named this way.)
*/
getWorkspaces: (cwd: string) => WorkspaceInfo;
/**
* Get an array with names, paths, and package.json contents for each package in a workspace.
* (See `../getWorkspaces` for why it's named this way.)
*/
getWorkspacesAsync?: (cwd: string) => Promise<WorkspaceInfo>;
}
/**
* Get utility implementations for the workspace manager of `cwd`.
* Returns undefined if the manager can't be determined.
*/
export function getWorkspaceUtilities(cwd: string): WorkspaceUtilities | undefined {
const manager = getWorkspaceManagerAndRoot(cwd)?.manager;
switch (manager) {
case "yarn":
return require("./yarn") as typeof YarnUtilities;
case "pnpm":
return require("./pnpm") as typeof PnpmUtilities;
case "rush":
return require("./rush") as typeof RushUtilities;
case "npm":
return require("./npm") as typeof NpmUtilities;
case "lerna":
return require("./lerna") as typeof LernaUtilities;
}
}

Просмотреть файл

@ -1,117 +1,2 @@
import path from "path";
import { searchUp } from "../../paths";
import { WorkspaceInfo } from "../../types/WorkspaceInfo";
import { WorkspaceManager } from "../WorkspaceManager";
// These must be type imports to avoid parsing the additional deps at runtime
import type * as LernaUtilities from "./lerna";
import type * as NpmUtilities from "./npm";
import type * as PnpmUtilities from "./pnpm";
import type * as RushUtilities from "./rush";
import type * as YarnUtilities from "./yarn";
export interface WorkspaceUtilities {
/**
* Get the root directory of a workspace/monorepo, defined as the directory where the workspace
* manager config file is located.
*/
getWorkspaceRoot: (cwd: string) => string;
/**
* Get an array with names, paths, and package.json contents for each package in a workspace.
* (See `../getWorkspaces` for why it's named this way.)
*/
getWorkspaces: (cwd: string) => WorkspaceInfo;
/**
* Get an array with names, paths, and package.json contents for each package in a workspace.
* (See `../getWorkspaces` for why it's named this way.)
*/
getWorkspacesAsync?: (cwd: string) => Promise<WorkspaceInfo>;
}
export interface WorkspaceManagerAndRoot {
/** Workspace manager name */
manager: WorkspaceManager;
/** Workspace root, where the manager configuration file is located */
root: string;
}
const workspaceCache = new Map<string, WorkspaceManagerAndRoot | undefined>();
const preferred = process.env.PREFERRED_WORKSPACE_MANAGER as WorkspaceManager | undefined;
/**
* Get the workspace manager name and workspace root directory for `cwd`.
* Returns undefined if the manager can't be determined.
*/
export function getWorkspaceManagerAndRoot(cwd: string, cache = workspaceCache): WorkspaceManagerAndRoot | undefined {
if (cache.has(cwd)) {
return cache.get(cwd);
}
const lockFile = searchUp(["lerna.json", "rush.json", "yarn.lock", "pnpm-workspace.yaml", "package-lock.json"], cwd);
if (!lockFile) {
return;
}
const root = path.dirname(lockFile);
switch (path.basename(lockFile)) {
case "lerna.json":
cache.set(cwd, { manager: "lerna", root });
break;
case "yarn.lock":
cache.set(cwd, { manager: "yarn", root });
break;
case "pnpm-workspace.yaml":
cache.set(cwd, { manager: "pnpm", root });
break;
case "rush.json":
cache.set(cwd, { manager: "rush", root });
break;
case "package-lock.json":
cache.set(cwd, { manager: "npm", root });
break;
default:
// Avoid searching again if no file was found
cache.set(cwd, undefined);
}
return cache.get(cwd);
}
/**
* Get the workspace manager name for `cwd`.
* Returns undefined if the manager can't be determined.
*/
export function getWorkspaceManager(cwd: string, cache = workspaceCache): WorkspaceManager | undefined {
return preferred || getWorkspaceManagerAndRoot(cwd, cache)?.manager;
}
/**
* Get utility implementations for the workspace manager of `cwd`.
* Returns undefined if the manager can't be determined.
*/
export function getWorkspaceUtilities(cwd: string): WorkspaceUtilities | undefined {
const manager = getWorkspaceManager(cwd);
switch (manager) {
case "yarn":
return require("./yarn") as typeof YarnUtilities;
case "pnpm":
return require("./pnpm") as typeof PnpmUtilities;
case "rush":
return require("./rush") as typeof RushUtilities;
case "npm":
return require("./npm") as typeof NpmUtilities;
case "lerna":
return require("./lerna") as typeof LernaUtilities;
}
}
export { getWorkspaceManagerAndRoot, WorkspaceManagerAndRoot } from "./getWorkspaceManagerAndRoot";
export { getWorkspaceUtilities, WorkspaceUtilities } from "./getWorkspaceUtilities";

Просмотреть файл

@ -2,19 +2,17 @@ import fs from "fs";
import jju from "jju";
import path from "path";
import { getPackagePaths } from "../../getPackagePaths";
import { searchUp } from "../../paths";
import { WorkspaceInfo } from "../../types/WorkspaceInfo";
import { getWorkspacePackageInfo } from "../getWorkspacePackageInfo";
import { logVerboseWarning } from "../../logging";
import { getWorkspaceManagerAndRoot } from "./getWorkspaceManagerAndRoot";
export function getLernaWorkspaceRoot(cwd: string): string {
const lernaJsonPath = searchUp("lerna.json", cwd);
if (!lernaJsonPath) {
throw new Error("Could not find lerna workspace root");
function getLernaWorkspaceRoot(cwd: string): string {
const root = getWorkspaceManagerAndRoot(cwd, undefined, "lerna")?.root;
if (!root) {
throw new Error("Could not find lerna workspace root from " + cwd);
}
return path.dirname(lernaJsonPath);
return root;
}
/**
@ -37,5 +35,4 @@ export function getLernaWorkspaces(cwd: string): WorkspaceInfo {
}
}
export { getLernaWorkspaceRoot as getWorkspaceRoot };
export { getLernaWorkspaces as getWorkspaces };

Просмотреть файл

@ -1,18 +1,13 @@
import { getWorkspaceManagerAndRoot } from ".";
import { WorkspaceInfo } from "../../types/WorkspaceInfo";
import {
getPackageJsonWorkspaceRoot,
getWorkspaceInfoFromWorkspaceRoot,
getWorkspaceInfoFromWorkspaceRootAsync,
} from "./packageJsonWorkspaces";
import { getWorkspaceInfoFromWorkspaceRoot, getWorkspaceInfoFromWorkspaceRootAsync } from "./packageJsonWorkspaces";
export function getNpmWorkspaceRoot(cwd: string): string {
const npmWorkspacesRoot = getPackageJsonWorkspaceRoot(cwd);
if (!npmWorkspacesRoot) {
throw new Error("Could not find NPM workspaces root");
function getNpmWorkspaceRoot(cwd: string): string {
const root = getWorkspaceManagerAndRoot(cwd, undefined, "npm")?.root;
if (!root) {
throw new Error("Could not find npm workspace root from " + cwd);
}
return npmWorkspacesRoot;
return root;
}
/**
@ -28,11 +23,10 @@ export function getNpmWorkspaces(cwd: string): WorkspaceInfo {
* Get an array with names, paths, and package.json contents for each package in an npm workspace.
* (See `../getWorkspaces` for why it's named this way.)
*/
export async function getNpmWorkspacesAsync(cwd: string): Promise<WorkspaceInfo> {
export function getNpmWorkspacesAsync(cwd: string): Promise<WorkspaceInfo> {
const npmWorkspacesRoot = getNpmWorkspaceRoot(cwd);
return await getWorkspaceInfoFromWorkspaceRootAsync(npmWorkspacesRoot);
return getWorkspaceInfoFromWorkspaceRootAsync(npmWorkspacesRoot);
}
export { getNpmWorkspaceRoot as getWorkspaceRoot };
export { getNpmWorkspaces as getWorkspaces };
export { getNpmWorkspacesAsync as getWorkspacesAsync };

Просмотреть файл

@ -1,11 +1,10 @@
import fs from "fs";
import path from "path";
import { getWorkspaceManagerAndRoot } from ".";
import { getPackagePaths, getPackagePathsAsync } from "../../getPackagePaths";
import { getWorkspacePackageInfo, getWorkspacePackageInfoAsync } from "../getWorkspacePackageInfo";
import { logVerboseWarning } from "../../logging";
type PackageJsonWorkspaces = {
type PackageJsonWithWorkspaces = {
workspaces?:
| {
packages?: string[];
@ -14,21 +13,19 @@ type PackageJsonWorkspaces = {
| string[];
};
export function getPackageJsonWorkspaceRoot(cwd: string): string | undefined {
return getWorkspaceManagerAndRoot(cwd)?.root;
}
function getRootPackageJson(packageJsonWorkspacesRoot: string) {
/**
* Read the workspace root package.json and get the list of package globs from its `workspaces` property.
*/
function getPackages(packageJsonWorkspacesRoot: string): string[] {
const packageJsonFile = path.join(packageJsonWorkspacesRoot, "package.json");
let packageJson: PackageJsonWithWorkspaces;
try {
return JSON.parse(fs.readFileSync(packageJsonFile, "utf-8"));
packageJson = JSON.parse(fs.readFileSync(packageJsonFile, "utf-8")) as PackageJsonWithWorkspaces;
} catch (e) {
throw new Error("Could not load package.json from workspaces root");
}
}
function getPackages(packageJson: PackageJsonWorkspaces): string[] {
const { workspaces } = packageJson;
if (Array.isArray(workspaces)) {
@ -48,9 +45,8 @@ function getPackages(packageJson: PackageJsonWorkspaces): string[] {
*/
export function getWorkspaceInfoFromWorkspaceRoot(packageJsonWorkspacesRoot: string) {
try {
const rootPackageJson = getRootPackageJson(packageJsonWorkspacesRoot);
const packages = getPackages(rootPackageJson);
const packagePaths = getPackagePaths(packageJsonWorkspacesRoot, packages);
const packageGlobs = getPackages(packageJsonWorkspacesRoot);
const packagePaths = getPackagePaths(packageJsonWorkspacesRoot, packageGlobs);
return getWorkspacePackageInfo(packagePaths);
} catch (err) {
logVerboseWarning(`Error getting workspace info for ${packageJsonWorkspacesRoot}`, err);
@ -64,9 +60,8 @@ export function getWorkspaceInfoFromWorkspaceRoot(packageJsonWorkspacesRoot: str
*/
export async function getWorkspaceInfoFromWorkspaceRootAsync(packageJsonWorkspacesRoot: string) {
try {
const rootPackageJson = getRootPackageJson(packageJsonWorkspacesRoot);
const packages = getPackages(rootPackageJson);
const packagePaths = await getPackagePathsAsync(packageJsonWorkspacesRoot, packages);
const packageGlobs = getPackages(packageJsonWorkspacesRoot);
const packagePaths = await getPackagePathsAsync(packageJsonWorkspacesRoot, packageGlobs);
return getWorkspacePackageInfoAsync(packagePaths);
} catch (err) {
logVerboseWarning(`Error getting workspace info for ${packageJsonWorkspacesRoot}`, err);

Просмотреть файл

@ -4,21 +4,20 @@ import { getPackagePaths } from "../../getPackagePaths";
import { WorkspaceInfo } from "../../types/WorkspaceInfo";
import { getWorkspacePackageInfo } from "../getWorkspacePackageInfo";
import { readYaml } from "../../lockfile/readYaml";
import { searchUp } from "../../paths";
import { logVerboseWarning } from "../../logging";
import { getWorkspaceManagerAndRoot } from "./getWorkspaceManagerAndRoot";
type PnpmWorkspaceYaml = {
packages: string[];
};
/** @deprecated Use `getWorkspaceRoot` */
export function getPnpmWorkspaceRoot(cwd: string): string {
const pnpmWorkspacesFile = searchUp("pnpm-workspace.yaml", cwd);
if (!pnpmWorkspacesFile) {
throw new Error("Could not find pnpm workspaces root");
const root = getWorkspaceManagerAndRoot(cwd, undefined, "pnpm")?.root;
if (!root) {
throw new Error("Could not find pnpm workspace root from " + cwd);
}
return path.dirname(pnpmWorkspacesFile);
return root;
}
/**
@ -42,5 +41,4 @@ export function getPnpmWorkspaces(cwd: string): WorkspaceInfo {
}
}
export { getPnpmWorkspaceRoot as getWorkspaceRoot };
export { getPnpmWorkspaces as getWorkspaces };

Просмотреть файл

@ -4,17 +4,16 @@ import fs from "fs";
import { WorkspaceInfo } from "../../types/WorkspaceInfo";
import { getWorkspacePackageInfo } from "../getWorkspacePackageInfo";
import { searchUp } from "../../paths";
import { logVerboseWarning } from "../../logging";
import { getWorkspaceManagerAndRoot } from "./getWorkspaceManagerAndRoot";
/** @deprecated Use getWorkspaceRoot */
export function getRushWorkspaceRoot(cwd: string): string {
const rushJsonPath = searchUp("rush.json", cwd);
if (!rushJsonPath) {
throw new Error("Could not find rush workspaces root");
const root = getWorkspaceManagerAndRoot(cwd, undefined, "rush")?.root;
if (!root) {
throw new Error("Could not find rush workspace root from " + cwd);
}
return path.dirname(rushJsonPath);
return root;
}
/**
@ -38,5 +37,4 @@ export function getRushWorkspaces(cwd: string): WorkspaceInfo {
}
}
export { getRushWorkspaceRoot as getWorkspaceRoot };
export { getRushWorkspaces as getWorkspaces };

Просмотреть файл

@ -1,18 +1,14 @@
import { getWorkspaceManagerAndRoot } from ".";
import { WorkspaceInfo } from "../../types/WorkspaceInfo";
import {
getPackageJsonWorkspaceRoot,
getWorkspaceInfoFromWorkspaceRoot,
getWorkspaceInfoFromWorkspaceRootAsync,
} from "./packageJsonWorkspaces";
import { getWorkspaceInfoFromWorkspaceRoot, getWorkspaceInfoFromWorkspaceRootAsync } from "./packageJsonWorkspaces";
/** @deprecated Use `getWorkspaceRoot` */
export function getYarnWorkspaceRoot(cwd: string): string {
const yarnWorkspacesRoot = getPackageJsonWorkspaceRoot(cwd);
if (!yarnWorkspacesRoot) {
throw new Error("Could not find yarn workspaces root");
const root = getWorkspaceManagerAndRoot(cwd, undefined, "yarn")?.root;
if (!root) {
throw new Error("Could not find yarn workspace root from " + cwd);
}
return yarnWorkspacesRoot;
return root;
}
/**
@ -28,11 +24,10 @@ export function getYarnWorkspaces(cwd: string): WorkspaceInfo {
* Get an array with names, paths, and package.json contents for each package in a yarn workspace.
* (See `../getWorkspaces` for why it's named this way.)
*/
export async function getYarnWorkspacesAsync(cwd: string): Promise<WorkspaceInfo> {
export function getYarnWorkspacesAsync(cwd: string): Promise<WorkspaceInfo> {
const yarnWorkspacesRoot = getYarnWorkspaceRoot(cwd);
return await getWorkspaceInfoFromWorkspaceRootAsync(yarnWorkspacesRoot);
return getWorkspaceInfoFromWorkspaceRootAsync(yarnWorkspacesRoot);
}
export { getYarnWorkspaceRoot as getWorkspaceRoot };
export { getYarnWorkspaces as getWorkspaces };
export { getYarnWorkspacesAsync as getWorkspacesAsync };