mirror of
https://github.com/sinanyuntu/trade-message-center.git
synced 2026-09-17 13:22:11 +08:00
40 lines
1.5 KiB
JavaScript
40 lines
1.5 KiB
JavaScript
// 同步 workspace 子包的根版本镜像
|
|
|
|
import { readFileSync, writeFileSync } from "node:fs";
|
|
import { resolve } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
import { listWorkspacePackagePaths, readRootPackageVersion } from "./package-version.mjs";
|
|
|
|
const workspaceDir = resolve(fileURLToPath(new URL(".", import.meta.url)), "..");
|
|
|
|
const readPackageManifest = (packagePath) => {
|
|
try {
|
|
return JSON.parse(readFileSync(packagePath, "utf8"));
|
|
} catch (error) {
|
|
throw new Error(`Unable to read workspace package ${packagePath}`, { cause: error });
|
|
}
|
|
};
|
|
|
|
/** 将根版本写入所有 workspace package metadata 镜像。 */
|
|
export const syncPackageVersions = (rootDir = workspaceDir) => {
|
|
const rootVersion = readRootPackageVersion(rootDir);
|
|
const changedPackages = [];
|
|
|
|
for (const packagePath of listWorkspacePackagePaths(rootDir)) {
|
|
const manifest = readPackageManifest(packagePath);
|
|
if (manifest.version === rootVersion) continue;
|
|
manifest.version = rootVersion;
|
|
writeFileSync(packagePath, `${JSON.stringify(manifest, null, 4)}\n`);
|
|
changedPackages.push(packagePath);
|
|
}
|
|
|
|
console.log(
|
|
changedPackages.length === 0
|
|
? `Workspace package versions already match ${rootVersion}`
|
|
: `Synchronized ${changedPackages.length} workspace package version mirror(s) to ${rootVersion}`,
|
|
);
|
|
};
|
|
|
|
if (process.argv[1] === fileURLToPath(import.meta.url)) syncPackageVersions();
|