Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(scripts): adds script to tag a target version-group as latest #5947

Merged
merged 1 commit into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 };
Loading