BREAKING CHANGE: Make WorkspaceInfo type a single object rather than an array, for consistency with other types

This commit is contained in:
Elizabeth Craig 2022-07-06 21:24:13 -07:00
Родитель 0897e76930
Коммит 18ad5b6697
13 изменённых файлов: 41 добавлений и 55 удалений

3
.vscode/settings.json поставляемый Normal file
Просмотреть файл

@ -0,0 +1,3 @@
{
"editor.formatOnSave": true
}

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

@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "BREAKING CHANGE: Make WorkspaceInfo type a single object rather than an array, for consistency with other types",
"packageName": "workspace-tools",
"email": "elcraig@microsoft.com",
"dependentChangeType": "patch"
}

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

@ -4,4 +4,4 @@ export type WorkspaceInfo = {
name: string;
path: string;
packageJson: PackageInfo;
}[];
};

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

@ -1,9 +1,6 @@
import { WorkspaceInfo } from "../types/WorkspaceInfo";
export function findWorkspacePath(
workspaces: WorkspaceInfo,
packageName: string
): string | undefined {
export function findWorkspacePath(workspaces: WorkspaceInfo[], packageName: string): string | undefined {
const workspace = workspaces.find(({ name }) => name === packageName);
if (workspace) {

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

@ -3,35 +3,30 @@ import fs from "fs";
import { WorkspaceInfo } from "../types/WorkspaceInfo";
import { PackageInfo } from "../types/PackageInfo";
export function getWorkspacePackageInfo(
workspacePaths: string[]
): WorkspaceInfo {
export function getWorkspacePackageInfo(workspacePaths: string[]): WorkspaceInfo[] {
if (!workspacePaths) {
return [];
}
return workspacePaths.reduce<WorkspaceInfo>((returnValue, workspacePath) => {
let packageJson: PackageInfo;
const packageJsonPath = path.join(workspacePath, "package.json");
return workspacePaths
.map<WorkspaceInfo | undefined>((workspacePath) => {
let packageJson: PackageInfo;
const packageJsonPath = path.join(workspacePath, "package.json");
try {
packageJson = JSON.parse(
fs.readFileSync(packageJsonPath, "utf-8")
) as PackageInfo;
} catch {
return returnValue;
}
try {
packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8")) as PackageInfo;
} catch {
return;
}
return [
...returnValue,
{
return {
name: packageJson.name,
path: workspacePath,
packageJson: {
...packageJson,
packageJsonPath,
},
},
];
}, []);
};
})
.filter((w): w is WorkspaceInfo => !!w);
}

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

@ -5,7 +5,7 @@ import { WorkspaceManager } from "./WorkspaceManager";
const preferred = process.env.PREFERRED_WORKSPACE_MANAGER as WorkspaceManager | null;
export function getWorkspaces(cwd: string): WorkspaceInfo {
export function getWorkspaces(cwd: string): WorkspaceInfo[] {
const workspaceImplementation = preferred || getWorkspaceImplementation(cwd);
if (!workspaceImplementation) {

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

@ -6,7 +6,6 @@ import { getPackagePaths } from "../../getPackagePaths";
import { WorkspaceInfo } from "../../types/WorkspaceInfo";
import { getWorkspacePackageInfo } from "../getWorkspacePackageInfo";
export function getLernaWorkspaceRoot(cwd: string): string {
const lernaJsonPath = findUp.sync("lerna.json", { cwd });
@ -17,19 +16,15 @@ export function getLernaWorkspaceRoot(cwd: string): string {
return path.dirname(lernaJsonPath);
}
export function getLernaWorkspaces(cwd: string): WorkspaceInfo {
export function getLernaWorkspaces(cwd: string): WorkspaceInfo[] {
try {
const lernaWorkspaceRoot = getLernaWorkspaceRoot(cwd);
const lernaJsonPath = path.join(lernaWorkspaceRoot, "lerna.json");
const lernaConfig = jju.parse(fs.readFileSync(lernaJsonPath, "utf-8"));
const packagePaths = getPackagePaths(
lernaWorkspaceRoot,
lernaConfig.packages
);
const workspaceInfo = getWorkspacePackageInfo(packagePaths);
return workspaceInfo;
const packagePaths = getPackagePaths(lernaWorkspaceRoot, lernaConfig.packages);
return getWorkspacePackageInfo(packagePaths);
} catch {
return [];
}

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

@ -11,7 +11,7 @@ export function getNpmWorkspaceRoot(cwd: string): string {
return npmWorkspacesRoot;
}
export function getNpmWorkspaces(cwd: string): WorkspaceInfo {
export function getNpmWorkspaces(cwd: string): WorkspaceInfo[] {
const npmWorkspacesRoot = getNpmWorkspaceRoot(cwd);
return getWorkspaceInfoFromWorkspaceRoot(npmWorkspacesRoot);
}

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

@ -2,6 +2,7 @@ import fs from "fs";
import path from "path";
import { getWorkspaceImplementationAndLockFile } from ".";
import { getPackagePaths } from "../../getPackagePaths";
import { WorkspaceInfo } from "../../types/WorkspaceInfo";
import { getWorkspacePackageInfo } from "../getWorkspacePackageInfo";
type PackageJsonWorkspaces = {
@ -44,13 +45,12 @@ function getPackages(packageJson: PackageJsonWorkspaces): string[] {
return workspaces.packages;
}
export function getWorkspaceInfoFromWorkspaceRoot(packageJsonWorkspacesRoot: string) {
export function getWorkspaceInfoFromWorkspaceRoot(packageJsonWorkspacesRoot: string): WorkspaceInfo[] {
try {
const rootPackageJson = getRootPackageJson(packageJsonWorkspacesRoot);
const packages = getPackages(rootPackageJson);
const packagePaths = getPackagePaths(packageJsonWorkspacesRoot, packages);
const workspaceInfo = getWorkspacePackageInfo(packagePaths);
return workspaceInfo;
return getWorkspacePackageInfo(packagePaths);
} catch {
return [];
}

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

@ -1,7 +1,6 @@
import path from "path";
import findUp from "find-up";
import { getPackagePaths } from "../../getPackagePaths";
import { WorkspaceInfo } from "../../types/WorkspaceInfo";
import { getWorkspacePackageInfo } from "../getWorkspacePackageInfo";
@ -20,24 +19,16 @@ export function getPnpmWorkspaceRoot(cwd: string): string {
return path.dirname(pnpmWorkspacesFile);
}
export function getPnpmWorkspaces(cwd: string): WorkspaceInfo {
export function getPnpmWorkspaces(cwd: string): WorkspaceInfo[] {
try {
const pnpmWorkspacesRoot = getPnpmWorkspaceRoot(cwd);
const pnpmWorkspacesFile = path.join(
pnpmWorkspacesRoot,
"pnpm-workspace.yaml"
);
const pnpmWorkspacesFile = path.join(pnpmWorkspacesRoot, "pnpm-workspace.yaml");
const readYaml = require("read-yaml-file").sync;
const pnpmWorkspaces = readYaml(pnpmWorkspacesFile) as PnpmWorkspaces;
const packagePaths = getPackagePaths(
pnpmWorkspacesRoot,
pnpmWorkspaces.packages
);
const workspaceInfo = getWorkspacePackageInfo(packagePaths);
return workspaceInfo;
const packagePaths = getPackagePaths(pnpmWorkspacesRoot, pnpmWorkspaces.packages);
return getWorkspacePackageInfo(packagePaths);
} catch {
return [];
}

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

@ -16,7 +16,7 @@ export function getRushWorkspaceRoot(cwd: string): string {
return path.dirname(rushJsonPath);
}
export function getRushWorkspaces(cwd: string): WorkspaceInfo {
export function getRushWorkspaces(cwd: string): WorkspaceInfo[] {
try {
const rushWorkspaceRoot = getRushWorkspaceRoot(cwd);
const rushJsonPath = path.join(rushWorkspaceRoot, "rush.json");

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

@ -11,7 +11,7 @@ export function getYarnWorkspaceRoot(cwd: string): string {
return yarnWorkspacesRoot;
}
export function getYarnWorkspaces(cwd: string): WorkspaceInfo {
export function getYarnWorkspaces(cwd: string): WorkspaceInfo[] {
const yarnWorkspacesRoot = getYarnWorkspaceRoot(cwd);
return getWorkspaceInfoFromWorkspaceRoot(yarnWorkspacesRoot);
}

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

@ -1,7 +1,5 @@
import { WorkspaceInfo } from "../types/WorkspaceInfo";
export function listOfWorkspacePackageNames(
workspaces: WorkspaceInfo
): string[] {
export function listOfWorkspacePackageNames(workspaces: WorkspaceInfo[]): string[] {
return workspaces.map(({ name }) => name);
}