Skip to content

Commit

Permalink
add release script
Browse files Browse the repository at this point in the history
  • Loading branch information
MrWangJustToDo committed Nov 21, 2023
1 parent 881214d commit c9e4287
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"gen:gql": "graphql-codegen --config site/graphql/codegen.yaml",
"dev": "ts-node ./scripts/rollupWatch.ts",
"build": "ts-node ./scripts/rollupBuild.ts",
"release": "ts-node ./scripts/release.ts",
"lint": "eslint --cache --ext ts,tsx .",
"lint:fix": "pnpm run lint --fix",
"prettier": "prettier --ignore-path .prettierignore --write .",
Expand Down
69 changes: 69 additions & 0 deletions scripts/release.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { readFile } from "fs/promises";
import { spawn } from "node:child_process";
import { resolve } from "path";

const pkgNameAlias = {
"@my-react/react": "myreact",
"@my-react/react-dom": "myreact-dom",
"@my-react/react-jsx": "myreact-jsx",
"@my-react/react-reactive": "myreact-reactivity",
"@my-react/react-reconciler": "myreact-reconciler",
"@my-react/react-refresh": "myreact-refresh",
"@my-react/react-shared": "myreact-shared",
};

const getVersion = (pkgName: string) =>
new Promise((a, b) => {
const ls = spawn(`pnpm view ${pkgName} version --json`, { shell: true, stdio: "pipe" });
ls.stdout.on("data", (d) => {
const res = Buffer.from(d).toString("utf-8");
a(JSON.parse(res));
});
ls.on("error", (e) => b(e));
});

const publish = (pnkName: string, cwd: string) => {
return new Promise((a, b) => {
const ls = spawn(`pnpm publish --access public`, { shell: true, stdio: "inherit", cwd });
ls.on("close", () => {
a(true);
});
ls.on("error", (e) => b(e));
});
};

const release = async (pkgName: keyof typeof pkgNameAlias) => {
if (!pkgNameAlias[pkgName]) return;
const path = "packages/" + pkgNameAlias[pkgName];

const packagesFile = resolve(process.cwd(), path, "package.json");

const data = await readFile(packagesFile, { encoding: "utf-8" });

const pkgObj = JSON.parse(data);

const version = pkgObj.version;

try {
const cVersion = await getVersion(pkgName);

if (cVersion === version) {
console.log(`no need release ${pkgName} @${version}`);
return;
}

await publish(pkgName, resolve(process.cwd(), path));

console.log(`success release ${pkgName} @${version}`);
} catch (e) {
console.log(e);
}
};

release("@my-react/react");
release("@my-react/react-dom");
release("@my-react/react-jsx");
release("@my-react/react-reactive");
release("@my-react/react-reconciler");
release("@my-react/react-refresh");
release("@my-react/react-shared");

0 comments on commit c9e4287

Please sign in to comment.