From c91a17c451f099d2cc850766d588bf7dfa26259b Mon Sep 17 00:00:00 2001 From: SoonIter Date: Tue, 25 Jun 2024 15:12:59 +0800 Subject: [PATCH] chore: update --- scripts/debug/overrides-rspack.mjs | 52 ++++++++++++++++++++++++++++++ scripts/debug/update-rspack.mjs | 43 ++++++++++++++---------- scripts/release/version.mjs | 9 +++++- x.mjs | 8 +++++ 4 files changed, 94 insertions(+), 18 deletions(-) create mode 100644 scripts/debug/overrides-rspack.mjs diff --git a/scripts/debug/overrides-rspack.mjs b/scripts/debug/overrides-rspack.mjs new file mode 100644 index 00000000000..5babe71b4d5 --- /dev/null +++ b/scripts/debug/overrides-rspack.mjs @@ -0,0 +1,52 @@ +import { findWorkspacePackagesNoCheck } from "@pnpm/find-workspace-packages"; +import fs from "node:fs"; +import { getNextName } from "../release/version.mjs"; + +export async function overrides_rspack_handler(version, options) { + const root = process.cwd(); + const { path: pathOpts } = options; + + let pkgPath = pathOpts; + if (typeof pkgPath !== "string") { + pkgPath = path.resolve(process.cwd(), "package.json"); + } + + if (!path.isAbsolute(pkgPath)) { + pkgPath = path.resolve(root, pkgPath); + } + + if (path.basename(pkgPath) !== "package.json") { + pkgPath = path.resolve(pkgPath, "package.json"); + } + + const pkgJson = ( + await import(pkgPath, { + assert: { + type: "json" + } + }) + )["default"]; + + if (!pkgJson["pnpm"]) { + pkgJson["pnpm"] = {}; + } + + if (!pkgJson["pnpm"]["overrides"]) { + pkgJson["pnpm"]["overrides"] = {}; + } + + const workspaces = await findWorkspacePackagesNoCheck(root); + const workspaceNames = workspaces.map(workspace => workspace.manifest.name); + const isSnapshot = version.includes("-canary"); + + for (const name of workspaceNames) { + if (name.startsWith("@rspack/")) { + pkgJson["pnpm"]["overrides"][isSnapshot ? getNextName(name) : name] = + version; + } + } + + fs.writeFileSync(pkgPath, JSON.stringify(pkgJson, null, 2), "utf8"); + + console.log(`Added pnpm.overrides to ${pkgPath}`); +} diff --git a/scripts/debug/update-rspack.mjs b/scripts/debug/update-rspack.mjs index 418c692b958..76923692c07 100644 --- a/scripts/debug/update-rspack.mjs +++ b/scripts/debug/update-rspack.mjs @@ -9,6 +9,26 @@ const depFields = [ "optionalDependencies" ]; +export function getNextPkgJson(pkgJson, version, isSnapshotVersion) { + const newPkgJson = { ...pkgJson }; + for (let field of depFields) { + if (!newPkgJson[field]) { + continue; + } + for (let [depName, _v] of Object.entries(newPkgJson[field])) { + if (depName.startsWith("@rspack/")) { + if (isSnapshotVersion) { + delete newPkgJson[field][depName]; + newPkgJson[field][getNextName(depName)] = version; + } else { + newPkgJson[field][depName] = version; + } + } + } + } + return newPkgJson; +} + export async function update_rspack_handler(version, options) { const root = process.cwd(); const { path: pathOpts } = options; @@ -34,23 +54,12 @@ export async function update_rspack_handler(version, options) { }) )["default"]; - for (let field of depFields) { - if (!pkgJson[field]) { - continue; - } - for (let [depName, _v] of Object.entries(pkgJson[field])) { - if (depName.startsWith("@rspack/")) { - if (version.includes("-canary")) { - delete pkgJson[field][depName]; - pkgJson[field][getNextName(depName)] = version; - } else { - pkgJson[field][depName] = version; - } - } - } - } - - fs.writeFileSync(pkgPath, JSON.stringify(pkgJson, null, 2), "utf8"); + const newPkgJson = await getNextPkgJson( + pkgJson, + version, + version.includes("-canary") + ); + fs.writeFileSync(pkgPath, JSON.stringify(newPkgJson, null, 2), "utf8"); console.log(`Updated rspack related package to ${version} in ${pkgPath}`); } diff --git a/scripts/release/version.mjs b/scripts/release/version.mjs index 502b1890880..e15e2bf0fe6 100644 --- a/scripts/release/version.mjs +++ b/scripts/release/version.mjs @@ -1,6 +1,7 @@ import path from "path"; import { findWorkspacePackagesNoCheck } from "@pnpm/find-workspace-packages"; import semver from "semver"; +import { getNextPkgJson } from "../debug/update-rspack.mjs"; async function getCommitId() { const result = await $`git rev-parse --short HEAD`; @@ -62,8 +63,13 @@ export async function version_handler(version) { if (version === "snapshot") { const nextName = getNextName(workspace.manifest.name); + const nextPkgJson = getNextPkgJson( + workspace.manifest, + "workspace:*", + true + ); newManifest = { - ...workspace.manifest, + ...nextPkgJson, name: nextName, version: nextVersion }; @@ -73,6 +79,7 @@ export async function version_handler(version) { version: nextVersion }; } + console.log(newManifest.name); workspace.writeProjectManifest(newManifest); } } diff --git a/x.mjs b/x.mjs index 3552fa4d190..0f35417a572 100755 --- a/x.mjs +++ b/x.mjs @@ -9,6 +9,7 @@ import { launchRspackCli } from "./scripts/debug/launch.mjs"; import { update_rspack_handler } from "./scripts/debug/update-rspack.mjs"; +import { overrides_rspack_handler } from "./scripts/debug/overrides-rspack.mjs"; import { publish_handler } from "./scripts/release/publish.mjs"; import { version_handler } from "./scripts/release/version.mjs"; @@ -251,6 +252,13 @@ program .description("update rspack related packages in package.json") .action(update_rspack_handler); +program + .command("overrides-rspack") + .argument("", "version field") + .option("--path ", "path to package.json") + .description("add pnpm.overrides of rspack to package.json") + .action(overrides_rspack_handler); + program .command("version") .argument("", "bump version to (major|minor|patch|snapshot)")