-
Notifications
You must be signed in to change notification settings - Fork 582
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(scripts): adds script to tag a target version-group as latest (#…
- Loading branch information
Showing
2 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters