Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
SoonIter committed Jun 25, 2024
1 parent 6249ea4 commit c91a17c
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 18 deletions.
52 changes: 52 additions & 0 deletions scripts/debug/overrides-rspack.mjs
Original file line number Diff line number Diff line change
@@ -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}`);
}
43 changes: 26 additions & 17 deletions scripts/debug/update-rspack.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}`);
}
9 changes: 8 additions & 1 deletion scripts/release/version.mjs
Original file line number Diff line number Diff line change
@@ -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`;
Expand Down Expand Up @@ -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
};
Expand All @@ -73,6 +79,7 @@ export async function version_handler(version) {
version: nextVersion
};
}
console.log(newManifest.name);
workspace.writeProjectManifest(newManifest);
}
}
8 changes: 8 additions & 0 deletions x.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -251,6 +252,13 @@ program
.description("update rspack related packages in package.json")
.action(update_rspack_handler);

program
.command("overrides-rspack")
.argument("<version>", "version field")
.option("--path <char>", "path to package.json")
.description("add pnpm.overrides of rspack to package.json")
.action(overrides_rspack_handler);

program
.command("version")
.argument("<bump_version>", "bump version to (major|minor|patch|snapshot)")
Expand Down

0 comments on commit c91a17c

Please sign in to comment.