Minor fixes to new async workspace/package methods (#274)

This commit is contained in:
Elizabeth Craig 2023-04-12 20:55:43 -07:00 коммит произвёл GitHub
Родитель bdfbb181a7
Коммит 1dc1125ea5
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 84 добавлений и 82 удалений

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

@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Minor fixes to new async workspace/package methods",
"packageName": "workspace-tools",
"email": "elcraig@microsoft.com",
"dependentChangeType": "patch"
}

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

@ -1,55 +1,50 @@
import path from "path";
import glob from "fast-glob";
import glob, { type Options as GlobOptions } from "fast-glob";
const packagePathsCache: { [workspacesRoot: string]: string[] } = {};
const packagePathsCache: { [root: string]: string[] } = {};
const globOptions: GlobOptions = {
absolute: true,
ignore: ["**/node_modules/**", "**/__fixtures__/**"],
stats: false,
};
export function getPackagePaths(workspacesRoot: string, packages: string[]): string[] {
if (packagePathsCache[workspacesRoot]) {
return packagePathsCache[workspacesRoot];
/**
* Given package folder globs (such as those from package.json `workspaces`) and a workspace root
* directory, get paths to actual package folders.
*/
export function getPackagePaths(root: string, packageGlobs: string[]): string[] {
if (packagePathsCache[root]) {
return packagePathsCache[root];
}
const packagePaths = glob
.sync(
packages.map((glob) => path.join(glob, "package.json").replace(/\\/g, "/")),
{
cwd: workspacesRoot,
absolute: true,
ignore: ["**/node_modules/**", "**/__fixtures__/**"],
stats: false,
}
)
.map((p) => path.dirname(p));
packagePathsCache[root] = glob
.sync(getPackageJsonGlobs(packageGlobs), { cwd: root, ...globOptions })
.map(getResultPackagePath);
if (path.sep === "/") {
packagePathsCache[workspacesRoot] = packagePaths;
} else {
packagePathsCache[workspacesRoot] = packagePaths.map((p) => p.replace(/\//g, path.sep));
}
return packagePathsCache[workspacesRoot];
return packagePathsCache[root];
}
export async function getPackagePathsAsync(workspacesRoot: string, packages: string[]): Promise<string[]> {
if (packagePathsCache[workspacesRoot]) {
return packagePathsCache[workspacesRoot];
/**
* Given package folder globs (such as those from package.json `workspaces`) and a workspace root
* directory, get paths to actual package folders.
*/
export async function getPackagePathsAsync(root: string, packageGlobs: string[]): Promise<string[]> {
if (packagePathsCache[root]) {
return packagePathsCache[root];
}
const packagePaths = (
await glob(
packages.map((glob) => path.join(glob, "package.json").replace(/\\/g, "/")),
{
cwd: workspacesRoot,
ignore: ["**/node_modules/**", "**/__fixtures__/**"],
stats: false,
}
)
).map((p) => path.join(workspacesRoot, path.dirname(p)));
packagePathsCache[root] = (await glob(getPackageJsonGlobs(packageGlobs), { cwd: root, ...globOptions })).map(
getResultPackagePath
);
if (path.sep === "/") {
packagePathsCache[workspacesRoot] = packagePaths;
} else {
packagePathsCache[workspacesRoot] = packagePaths.map((p) => p.replace(/\//g, path.sep));
}
return packagePathsCache[workspacesRoot];
return packagePathsCache[root];
}
function getPackageJsonGlobs(packageGlobs: string[]) {
return packageGlobs.map((glob) => path.join(glob, "package.json").replace(/\\/g, "/"));
}
function getResultPackagePath(packageJsonPath: string) {
const packagePath = path.dirname(packageJsonPath);
return path.sep === "/" ? packagePath : packagePath.replace(/\//g, path.sep);
}

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

@ -1,61 +1,61 @@
import path from "path";
import fs from "fs";
import fsPromises from "fs/promises";
import { WorkspaceInfo } from "../types/WorkspaceInfo";
import { PackageInfo } from "../types/PackageInfo";
import { logVerboseWarning } from "../logging";
export function getWorkspacePackageInfo(workspacePaths: string[]): WorkspaceInfo {
if (!workspacePaths) {
export function getWorkspacePackageInfo(packagePaths: string[]): WorkspaceInfo {
if (!packagePaths) {
return [];
}
return workspacePaths.reduce<WorkspaceInfo>((returnValue, workspacePath) => {
return packagePaths.reduce<WorkspaceInfo>((workspacePkgs, 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;
} catch (err) {
logVerboseWarning(`Error reading or parsing ${packageJsonPath} while getting workspace package info`, err);
return workspacePkgs;
}
return [
...returnValue,
{
workspacePkgs.push({
name: packageJson.name,
path: workspacePath,
packageJson: {
...packageJson,
packageJsonPath,
},
});
return workspacePkgs;
}, []);
}
export async function getWorkspacePackageInfoAsync(packagePaths: string[]): Promise<WorkspaceInfo> {
if (!packagePaths) {
return [];
}
const workspacePkgPromises = packagePaths.map<Promise<WorkspaceInfo[number] | null>>(async (workspacePath) => {
const packageJsonPath = path.join(workspacePath, "package.json");
try {
const packageJson = JSON.parse(await fsPromises.readFile(packageJsonPath, "utf-8")) as PackageInfo;
return {
name: packageJson.name,
path: workspacePath,
packageJson: {
...packageJson,
packageJsonPath,
},
},
];
}, []);
}
export async function getWorkspacePackageInfoAsync(workspacePaths: string[]): Promise<WorkspaceInfo> {
if (!workspacePaths) {
return [];
}
const packageInfoPromises: Promise<{ name: string; path: string; packageJson: PackageInfo } | null>[] =
workspacePaths.map(async (workspacePath) => {
let packageJson: PackageInfo;
const packageJsonPath = path.join(workspacePath, "package.json");
try {
packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8")) as PackageInfo;
return {
name: packageJson.name,
path: workspacePath,
packageJson: {
...packageJson,
packageJsonPath,
},
};
} catch {
return null;
}
});
return (await Promise.all(packageInfoPromises)).flat().filter(Boolean) as WorkspaceInfo;
};
} catch (err) {
logVerboseWarning(`Error reading or parsing ${packageJsonPath} while getting workspace package info`, err);
return null;
}
});
return (await Promise.all(workspacePkgPromises)).flat().filter(Boolean) as WorkspaceInfo;
}