setup: improve publish script

This commit is contained in:
Abdullah Atta
2024-09-23 15:17:50 +05:00
parent fc1015b3fb
commit bcc739aa57

View File

@@ -30,6 +30,7 @@ import { readFile, writeFile, cp, rm } from "fs/promises";
import glob from "fast-glob"; import glob from "fast-glob";
import parser from "yargs-parser"; import parser from "yargs-parser";
import { Listr } from "listr2"; import { Listr } from "listr2";
import { existsSync } from "fs";
const args = parser(process.argv, { alias: { scope: ["s"], version: ["v"] } }); const args = parser(process.argv, { alias: { scope: ["s"], version: ["v"] } });
const allPackages = await glob("packages/*", { const allPackages = await glob("packages/*", {
@@ -52,25 +53,28 @@ const packages = Array.from(
await publishPackages(packages); await publishPackages(packages);
async function publishPackages(dependencies) { async function publishPackages(dependencies) {
console.log("> Found", dependencies.length, "dependencies to bootstrap."); console.log("> Found", dependencies.length, "dependencies to publish.");
const outputs = { stdout: [], stderr: [] }; const outputs = { stdout: [], stderr: [] };
await performTasks("bump", dependencies, (dep) => bumpVersion(dep, outputs));
try { try {
await performTasks("resolve local packages", dependencies, (dep) => await performTasks("dry run", { dependencies, concurrency: 1 }, (dep) =>
resolveLocalPackages(dep)
);
await performTasks("dry run", dependencies, (dep) =>
publishPackage(dep, true) publishPackage(dep, true)
); );
await performTasks("publish", dependencies, (dep) => await performTasks("bump", { dependencies }, (dep) =>
bumpVersion(dep, outputs)
);
await performTasks("resolve local packages", { dependencies }, (dep) =>
resolveLocalPackages(dep)
);
await performTasks("publish", { dependencies, concurrency: 1 }, (dep) =>
publishPackage(dep, false, outputs) publishPackage(dep, false, outputs)
); );
} finally { } finally {
await performTasks("unresolve", dependencies, (dep) => await performTasks("unresolve", { dependencies }, (dep) =>
unresolveLocalPackages(dep) unresolveLocalPackages(dep)
); );
} }
@@ -79,14 +83,14 @@ async function publishPackages(dependencies) {
process.stderr.write(outputs.stderr.join("")); process.stderr.write(outputs.stderr.join(""));
} }
async function performTasks(title, dependencies, action) { async function performTasks(title, { dependencies, concurrency }, action) {
const tasks = new Listr( const tasks = new Listr(
dependencies.map((dep) => ({ dependencies.map((dep) => ({
task: (_, task) => action(dep, task), task: (_, task) => action(dep, task),
title: title + " " + dep title: title + " " + dep
})), })),
{ {
concurrent: 8, concurrent: concurrency || 8,
exitOnError: true exitOnError: true
} }
); );
@@ -127,10 +131,12 @@ async function resolveLocalPackages(cwd) {
async function unresolveLocalPackages(cwd) { async function unresolveLocalPackages(cwd) {
const packageJsonPath = path.join(cwd, "package.json"); const packageJsonPath = path.join(cwd, "package.json");
await cp(packageJsonPath + ".old", packageJsonPath, { if (existsSync(packageJsonPath + ".old")) {
force: true await cp(packageJsonPath + ".old", packageJsonPath, {
}); force: true
await rm(packageJsonPath + ".old", { force: true }); });
await rm(packageJsonPath + ".old", { force: true });
}
} }
async function publishPackage(cwd, dryRun, outputs) { async function publishPackage(cwd, dryRun, outputs) {