Something went wrong. Try again.
All-purpose web tiles library
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154#!/usr/bin/env node// Release every workspace package that has changed since its last release.//// "Last release" is tracked by a per-package git tag of the form// `${name}@${version}` (created by release-it — see .release-it.json). A package// with no such tag is treated as never released and will be released.//// A package is considered changed when its own directory differs between its// last release tag and HEAD. Changes to shared/root files do not trigger a// release on their own.//// Any extra CLI args are passed through to release-it, e.g.:// npm run release -- patch release all changed packages as a patch// npm run release -- minor --ci non-interactive minor release// npm run release -- --dry-run preview without publishing//// --only <name> (repeatable) releases just the named package(s), changed or// not, so a set of releases can be reviewed and shipped one at a time:// npm run release -- minor --only @dasl/tile-lexicon//// release-it cannot parse the DID form of the Tangled remote (the colons in// did:plc:… defeat its URL parser), so .release-it.json names the repository// by handle in git.pushRepo. release-it then pushes to that URL rather than// to origin, which leaves origin/main behind, so each release is followed by a// fetch.//// release-it bumps, commits, tags and pushes; the publish itself is done here,// with the terminal attached, because npm's two-factor login (auth-type web)// opens a browser and needs a TTY, which release-it's captured child process// does not give it. So run this from a terminal. If the publish fails after// the push (the login link expires after a few minutes), the tag is already// there, so running this again does not bump again: a package whose current// version is tagged but not on npm is published as it is.
import { execFileSync } from 'node:child_process';import { readFileSync, writeFileSync } from 'node:fs';import { resolve, dirname } from 'node:path';import { fileURLToPath } from 'node:url';
const root = resolve(dirname(fileURLToPath(import.meta.url)), '..');const configPath = resolve(root, '.release-it.json');const releaseItBin = resolve(root, 'node_modules', '.bin', 'release-it');const only = [];const passthrough = [];for (let i = 2; i < process.argv.length; i++) { if (process.argv[i] === '--only') only.push(process.argv[++i]); else if (process.argv[i].startsWith('--only=')) only.push(process.argv[i].slice(7)); else passthrough.push(process.argv[i]);}
const rootPkg = readJSON(resolve(root, 'package.json'));const dirs = rootPkg.workspaces ?? [];
let released = 0;for (const dir of dirs) { const pkg = readJSON(resolve(root, dir, 'package.json')); if (!pkg.name || pkg.private) { log(`· ${dir}: private or unnamed, skipping`); continue; } if (only.length && !only.includes(pkg.name)) { log(`· ${pkg.name}: not asked for, skipping`); continue; } const tag = lastReleaseTag(pkg.name); const changed = only.length || !tag || dirChangedSince(tag, dir); if (!changed) { log(`· ${pkg.name}: no changes since ${tag}, skipping`); continue; } const pending = tag === `${pkg.name}@${pkg.version}` && !onNpm(pkg.name, pkg.version); if (pending) { log(`\n▸ ${pkg.name}@${pkg.version}: tagged but not on npm, publishing as is`); } else { log(`\n▸ ${pkg.name}: releasing (${only.length ? 'asked for' : tag ? `changed since ${tag}` : 'never released'})`); execFileSync(releaseItBin, ['--config', configPath, ...passthrough], { cwd: resolve(root, dir), stdio: 'inherit', }); } released++; if (!passthrough.includes('--dry-run')) { git(['fetch', '--quiet', 'origin']); const version = readJSON(resolve(root, dir, 'package.json')).version; log(`\n▸ ${pkg.name}@${version}: publishing`); try { execFileSync('npm', ['publish'], { cwd: resolve(root, dir), stdio: 'inherit' }); } catch { log(`\n✗ ${pkg.name}@${version} is tagged and pushed but not published. Fix the cause, then: cd ${dir} && npm publish`); process.exit(1); } pointDependentsAt(pkg.name, version); }}
log(released ? `\nReleased ${released} package(s).` : '\nNothing to release — every package is up to date.');
// Packages are released leaves first, so once one is out every workspace that// depends on it is pointed at the new version before its own turn comes.// Otherwise a loader that needs today's lexicon would ship asking for last// year's.function pointDependentsAt (name, version) { for (const dir of dirs) { const file = resolve(root, dir, 'package.json'); const raw = readFileSync(file, 'utf8'); const pkg = JSON.parse(raw); let changed = false; for (const field of ['dependencies', 'devDependencies', 'peerDependencies']) { if (pkg[field]?.[name] && pkg[field][name] !== `^${version}`) { pkg[field][name] = `^${version}`; changed = true; } } if (changed) { writeFileSync(file, JSON.stringify(pkg, null, 2) + (raw.endsWith('\n') ? '\n' : '')); log(` ${dir}: ${name} -> ^${version}`); } }}
function readJSON (p) { return JSON.parse(readFileSync(p, 'utf8'));}function log (m) { console.log(m);}function onNpm (name, version) { try { return execFileSync('npm', ['view', `${name}@${version}`, 'version'], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim() === version; } catch { return false; }}function lastReleaseTag (name) { const out = git(['tag', '--list', `${name}@*`, '--sort=-creatordate']); return out.split('\n').filter(Boolean)[0] || null;}function dirChangedSince (tag, dir) { // `git diff --quiet` exits 1 (throws) when there are differences. try { git(['diff', '--quiet', tag, 'HEAD', '--', dir]); return false; } catch { return true; }}function git (args) { return execFileSync('git', args, { cwd: root, encoding: 'utf8' }).trim();}