Skip to content

Commit

Permalink
chore(scripts): adds script to tag a target version-group as latest (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe authored Mar 29, 2024
1 parent 7210e7d commit 6dd2f7c
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
74 changes: 74 additions & 0 deletions scripts/npm-registry/mark-version-as-latest.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env node

/**
* This script will output commands to set a target version as "latest" on the NPM public registry.
*
* The canonical list of packages will be generated from this repository's current state (lib/packages/clients),
* so anything missing from this workspace will not be considered.
*/
//

import readline from "readline";
import fs from "fs";
import path, { dirname } from "path";
import { fileURLToPath } from "node:url";
import { spawnProcessReturnValue } from "../utils/spawn-process.js";

const __dirname = dirname(fileURLToPath(import.meta.url));

async function ask(question) {
return new Promise((resolve) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question(question + "\n", (answer) => {
resolve(answer);
rl.close();
});
});
}

const root = path.join(__dirname, "..", "..");

const packages = [];

function recordPackages(dir) {
const packageFolders = fs.readdirSync(dir);
for (const packageFolder of packageFolders) {
const pkgJson = JSON.parse(fs.readFileSync(path.join(dir, packageFolder, "package.json"), "utf-8"));
packages.push(pkgJson.name);
}
}

recordPackages(path.join(root, "lib"));
recordPackages(path.join(root, "packages"));
recordPackages(path.join(root, "clients"));

const targetVersion = await ask("What is the version that should become [latest] (e.g. 3.512.0 no v-prefix)?");

for (const pkg of packages) {
try {
const viewVersion = await spawnProcessReturnValue("npm", ["view", `${pkg}@<=${targetVersion}`, "version"], {
returnOutput: true,
});
if (viewVersion) {
const lines = viewVersion.split("\n").filter((s) => !!s.trim());
const versionMatch = lines[lines.length - 1].match(/'(.*?)'/);
if (versionMatch?.[1]) {
// this indicates multiple versions matching.
const latestBeforeTargetVersion = versionMatch[1];
console.log(`npm dist-tag add ${pkg}@${latestBeforeTargetVersion} latest`);
} else {
// this indicates a single version matching.
console.log(`npm dist-tag add ${pkg}@${lines} latest`);
}
}
} catch (e) {
if (e.toString().includes("is not in this registry.")) {
console.log(`# ${pkg} does not exist at or below ${targetVersion}.`);
} else {
throw e;
}
}
}
20 changes: 19 additions & 1 deletion scripts/utils/spawn-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,22 @@ const spawnProcess = async (command, args = [], options = {}) => {
});
};

module.exports = { spawnProcess };
const spawnProcessReturnValue = async (command, args = [], options = {}) => {
const childProcess = spawn(command, args, options);

let buffer = "";
let errBuffer = "";
childProcess.stdout.on("data", (data) => {
buffer += data.toString();
});
childProcess.stderr.on("data", (data) => {
errBuffer += data.toString();
});

return new Promise((resolve, reject) => {
childProcess.on("error", reject);
childProcess.on("exit", (code, signal) => (code === 0 ? resolve(buffer) : reject(errBuffer)));
});
};

module.exports = { spawnProcess, spawnProcessReturnValue };

0 comments on commit 6dd2f7c

Please sign in to comment.