|
| 1 | +#!/usr/bin/env node |
| 2 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 3 | +/** |
| 4 | + * released-keys-removed.mjs — every translation key a released `@object-ui/i18n` |
| 5 | + * shipped that a later tree no longer carries. |
| 6 | + * |
| 7 | + * Usage (from anywhere inside the repository): |
| 8 | + * |
| 9 | + * node ui/packages/i18n/scripts/released-keys-removed.mjs \ |
| 10 | + * [--tag @object-ui/i18n@18.6.0] [--ref origin/main] [--json] |
| 11 | + * |
| 12 | + * It reads the package's catalogue sources out of git at BOTH refs (never the |
| 13 | + * working tree), so it answers the same on any checkout. Zero third-party |
| 14 | + * dependencies: git for the blobs, Node's own type stripping to evaluate the |
| 15 | + * catalogue modules (they import nothing, so each one evaluates alone). |
| 16 | + * |
| 17 | + * ## Which files are the published catalogues — read from the package, not listed here |
| 18 | + * |
| 19 | + * - `package.json` `exports`: every `./namespaces/NAME` subpath is a published |
| 20 | + * namespace; `./locales` is the all-packs door. |
| 21 | + * - `src/locales/index.ts` (the all-packs door): each DEFAULT import of a |
| 22 | + * `./TAG.js` module is a language pack, and each default import of a |
| 23 | + * `../namespaces/NAME/TAG.js` module is that language's catalogue in a |
| 24 | + * pack-shaped namespace. The door deep-merges them into one catalogue per |
| 25 | + * language, and at runtime they all land in i18next's default |
| 26 | + * `translation` namespace, so they are ONE key space: a key that moved |
| 27 | + * from a pack into a pack-shaped namespace (or back) is not removed. |
| 28 | + * - Every other exported namespace (one whose `index.ts` does not build on |
| 29 | + * `../packNamespace.js`, today `studio`) is a key space of its own; its |
| 30 | + * catalogues are the `./TAG.js` modules its `index.ts` imports, statically |
| 31 | + * or through a literal `import()` loader. |
| 32 | + * |
| 33 | + * A key is the dotted path to a leaf value (strings, plus any non-object |
| 34 | + * value, arrays included), so plural forms (`KEY_one`, `KEY_other`, …) are |
| 35 | + * separate keys. A key counts as removed in a language when that language's |
| 36 | + * catalogue at the tag has it and the same key space at the ref does not. |
| 37 | + */ |
| 38 | +import { execFileSync } from 'node:child_process'; |
| 39 | +import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from 'node:fs'; |
| 40 | +import { tmpdir } from 'node:os'; |
| 41 | +import { dirname, join, posix } from 'node:path'; |
| 42 | +import { fileURLToPath, pathToFileURL } from 'node:url'; |
| 43 | + |
| 44 | +const args = process.argv.slice(2); |
| 45 | +const opt = (name, fallback) => { |
| 46 | + const i = args.indexOf(name); |
| 47 | + if (i === -1) return fallback; |
| 48 | + const v = args[i + 1]; |
| 49 | + if (!v || v.startsWith('--')) { |
| 50 | + console.error(`released-keys-removed: ${name} needs a value`); |
| 51 | + process.exit(2); |
| 52 | + } |
| 53 | + return v; |
| 54 | +}; |
| 55 | +const TAG = opt('--tag', '@object-ui/i18n@18.6.0'); |
| 56 | +const REF = opt('--ref', 'origin/main'); |
| 57 | +const AS_JSON = args.includes('--json'); |
| 58 | + |
| 59 | +// git runs in the package directory and every blob is named in git's own |
| 60 | +// cwd-relative form (`REF:./PATH`), so the script needs no repository root and |
| 61 | +// reads the same in the standalone and the monorepo layout. |
| 62 | +const PKG_DIR = dirname(dirname(fileURLToPath(import.meta.url))); |
| 63 | +const git = (...a) => execFileSync('git', a, { cwd: PKG_DIR, encoding: 'utf8', maxBuffer: 1 << 28 }); |
| 64 | +const PKG = git('rev-parse', '--show-prefix').trim().replace(/\/$/, ''); // for messages only |
| 65 | + |
| 66 | +const show = (ref, path) => git('show', `${ref}:./${path}`); |
| 67 | +const exists = (ref, path) => { |
| 68 | + try { |
| 69 | + git('cat-file', '-e', `${ref}:./${path}`); |
| 70 | + return true; |
| 71 | + } catch { |
| 72 | + return false; |
| 73 | + } |
| 74 | +}; |
| 75 | + |
| 76 | +for (const ref of [TAG, REF]) { |
| 77 | + try { |
| 78 | + git('rev-parse', '--verify', '--quiet', `${ref}^{commit}`); |
| 79 | + } catch { |
| 80 | + console.error(`released-keys-removed: ref '${ref}' does not resolve (fetch it first)`); |
| 81 | + process.exit(2); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +const tmp = mkdtempSync(join(tmpdir(), 'released-keys-removed-')); |
| 86 | +process.on('exit', () => rmSync(tmp, { recursive: true, force: true })); |
| 87 | +let seq = 0; |
| 88 | + |
| 89 | +/** Evaluate one catalogue module's default export from its source at `ref`. */ |
| 90 | +async function loadCatalogue(ref, path) { |
| 91 | + const dir = join(tmp, String(seq++)); |
| 92 | + mkdirSync(dir); |
| 93 | + const file = join(dir, posix.basename(path)); |
| 94 | + writeFileSync(file, show(ref, path)); |
| 95 | + const mod = await import(pathToFileURL(file).href); |
| 96 | + if (!mod.default || typeof mod.default !== 'object') { |
| 97 | + throw new Error(`${ref}:${PKG}/${path} has no object default export`); |
| 98 | + } |
| 99 | + return mod.default; |
| 100 | +} |
| 101 | + |
| 102 | +const isPlain = (v) => v !== null && typeof v === 'object' && !Array.isArray(v); |
| 103 | +function flatten(obj, prefix = '', out = new Set()) { |
| 104 | + for (const [k, v] of Object.entries(obj)) { |
| 105 | + const key = prefix ? `${prefix}.${k}` : k; |
| 106 | + if (isPlain(v)) flatten(v, key, out); |
| 107 | + else out.add(key); |
| 108 | + } |
| 109 | + return out; |
| 110 | +} |
| 111 | + |
| 112 | +/** `./x.js` / `../a/b.js` specifiers of default imports and literal `import()` calls. */ |
| 113 | +function defaultImports(src) { |
| 114 | + return [...src.matchAll(/^\s*import\s+[A-Za-z_$][\w$]*\s+from\s+'([^']+)'/gm)].map((m) => m[1]); |
| 115 | +} |
| 116 | +function dynamicImports(src) { |
| 117 | + return [...src.matchAll(/import\(\s*'([^']+)'\s*\)/g)].map((m) => m[1]); |
| 118 | +} |
| 119 | +const tagOf = (spec) => posix.basename(spec).replace(/\.js$/, ''); |
| 120 | +const toSrc = (fromDir, spec) => posix.normalize(posix.join(fromDir, spec)).replace(/\.js$/, '.ts'); |
| 121 | + |
| 122 | +/** |
| 123 | + * The catalogues one ref publishes: |
| 124 | + * Map<space, Map<lang, Array<{ module, path }>>>, where `module` names the |
| 125 | + * published group the file belongs to (`locales`, or the namespace name). |
| 126 | + */ |
| 127 | +function discover(ref) { |
| 128 | + const pkg = JSON.parse(show(ref, 'package.json')); |
| 129 | + const exp = Object.keys(pkg.exports ?? {}); |
| 130 | + if (!exp.includes('./locales')) throw new Error(`${ref}: package.json exports no ./locales`); |
| 131 | + const namespaces = exp |
| 132 | + .filter((k) => k.startsWith('./namespaces/')) |
| 133 | + .map((k) => k.slice('./namespaces/'.length)); |
| 134 | + |
| 135 | + const spaces = new Map(); |
| 136 | + const add = (space, lang, module, path) => { |
| 137 | + if (!spaces.has(space)) spaces.set(space, new Map()); |
| 138 | + const langs = spaces.get(space); |
| 139 | + if (!langs.has(lang)) langs.set(lang, []); |
| 140 | + langs.get(lang).push({ module, path }); |
| 141 | + }; |
| 142 | + |
| 143 | + // The all-packs door: packs + pack-shaped namespaces, one `translation` space. |
| 144 | + const door = show(ref, 'src/locales/index.ts'); |
| 145 | + const packShaped = new Set(); |
| 146 | + for (const spec of defaultImports(door)) { |
| 147 | + if (!spec.startsWith('.')) continue; |
| 148 | + const path = toSrc('src/locales', spec); |
| 149 | + const m = /^src\/namespaces\/([^/]+)\/[^/]+\.ts$/.exec(path); |
| 150 | + if (m) { |
| 151 | + packShaped.add(m[1]); |
| 152 | + add('translation', tagOf(spec), m[1], path); |
| 153 | + } else if (/^src\/locales\/[^/]+\.ts$/.test(path)) { |
| 154 | + add('translation', tagOf(spec), 'locales', path); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + // Every exported namespace's own catalogue modules. |
| 159 | + for (const ns of namespaces) { |
| 160 | + const index = `src/namespaces/${ns}/index.ts`; |
| 161 | + if (!exists(ref, index)) throw new Error(`${ref}: export ./namespaces/${ns} has no ${index}`); |
| 162 | + const src = show(ref, index); |
| 163 | + const isPackShaped = /from '\.\.\/packNamespace\.js'/.test(src); |
| 164 | + const specs = [...defaultImports(src), ...dynamicImports(src)].filter((s) => /^\.\/[^/]+\.js$/.test(s)); |
| 165 | + for (const spec of new Set(specs)) { |
| 166 | + const path = toSrc(`src/namespaces/${ns}`, spec); |
| 167 | + if (isPackShaped) { |
| 168 | + // Already in the door; a catalogue the door does not merge would be a |
| 169 | + // published file outside every key space this script knows: refuse. |
| 170 | + const listed = spaces.get('translation')?.get(tagOf(spec))?.some((e) => e.path === path); |
| 171 | + if (!listed) throw new Error(`${ref}: ${path} is loaded by ${index} but not merged by src/locales/index.ts`); |
| 172 | + } else { |
| 173 | + add(ns, tagOf(spec), ns, path); |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + return spaces; |
| 178 | +} |
| 179 | + |
| 180 | +/** Map<space, Map<lang, Map<key, module>>> for one ref. */ |
| 181 | +async function keysAt(ref) { |
| 182 | + const out = new Map(); |
| 183 | + for (const [space, langs] of discover(ref)) { |
| 184 | + const perLang = new Map(); |
| 185 | + for (const [lang, entries] of langs) { |
| 186 | + const keys = new Map(); |
| 187 | + for (const { module, path } of entries) { |
| 188 | + for (const k of flatten(await loadCatalogue(ref, path))) keys.set(k, module); |
| 189 | + } |
| 190 | + perLang.set(lang, keys); |
| 191 | + } |
| 192 | + out.set(space, perLang); |
| 193 | + } |
| 194 | + return out; |
| 195 | +} |
| 196 | + |
| 197 | +const before = await keysAt(TAG); |
| 198 | +const after = await keysAt(REF); |
| 199 | + |
| 200 | +// removed: Map<groupName, Map<key, Set<lang>>>; group = space, or the module a |
| 201 | +// `translation` key lived in at the tag. |
| 202 | +const removed = new Map(); |
| 203 | +let moved = 0; |
| 204 | +for (const [space, langs] of before) { |
| 205 | + for (const [lang, keys] of langs) { |
| 206 | + const now = after.get(space)?.get(lang) ?? new Map(); |
| 207 | + for (const [key, module] of keys) { |
| 208 | + if (now.has(key)) { |
| 209 | + if (now.get(key) !== module) moved++; |
| 210 | + continue; |
| 211 | + } |
| 212 | + const group = space === 'translation' ? module : space; |
| 213 | + if (!removed.has(group)) removed.set(group, new Map()); |
| 214 | + const g = removed.get(group); |
| 215 | + if (!g.has(key)) g.set(key, new Set()); |
| 216 | + g.get(key).add(lang); |
| 217 | + } |
| 218 | + } |
| 219 | +} |
| 220 | + |
| 221 | +const tagLangs = (space) => [...(before.get(space)?.keys() ?? [])]; |
| 222 | +const total = [...removed.values()].reduce((n, g) => n + g.size, 0); |
| 223 | +const groups = [...removed.keys()].sort(); |
| 224 | + |
| 225 | +if (AS_JSON) { |
| 226 | + const body = { |
| 227 | + tag: TAG, |
| 228 | + ref: REF, |
| 229 | + refSha: git('rev-parse', REF).trim(), |
| 230 | + total, |
| 231 | + movedBetweenModules: moved, |
| 232 | + groups: Object.fromEntries( |
| 233 | + groups.map((g) => [ |
| 234 | + g, |
| 235 | + Object.fromEntries([...removed.get(g)].sort(([a], [b]) => a.localeCompare(b)).map(([k, l]) => [k, [...l].sort()])), |
| 236 | + ]), |
| 237 | + ), |
| 238 | + }; |
| 239 | + console.log(JSON.stringify(body, null, 2)); |
| 240 | +} else { |
| 241 | + console.log(`Keys in ${TAG} absent at ${REF} (${git('rev-parse', '--short', REF).trim()}): ${total}`); |
| 242 | + for (const g of groups) { |
| 243 | + const space = before.has(g) ? g : 'translation'; |
| 244 | + const all = tagLangs(space); |
| 245 | + console.log(`\n## ${g} (${removed.get(g).size})`); |
| 246 | + for (const [key, langs] of [...removed.get(g)].sort(([a], [b]) => a.localeCompare(b))) { |
| 247 | + const l = [...langs].sort(); |
| 248 | + const which = l.length === all.length ? `all ${all.length} locales (${l.join(', ')})` : l.join(', '); |
| 249 | + console.log(`- ${key} — ${which}`); |
| 250 | + } |
| 251 | + } |
| 252 | + if (moved) console.log(`\n(${moved} key×locale rows moved between modules inside one key space; not removals)`); |
| 253 | +} |
0 commit comments