#!/usr/bin/env node 'use strict'; /** * Single source for GSD's published-package coordinates (issue #498). * * `deriveIdentity(pkg)` is the pure core: it turns a parsed package.json into * the coordinate record every consumer needs. The generated runtime module * `get-shit-done/bin/lib/package-identity.cjs` bakes those values at build * time, because the installed tree carries only a synthetic * `{"type":"commonjs"}` package.json (no `.name`) — so a runtime * `require('package.json').name` resolves to `undefined` (the #378 bug this * seam retires). Baking from package.json reconciles #378 (renames survive) * with #2992 (the value is never an LLM runtime choice). */ /** * Parse `owner/name` out of a package.json `repository.url`, stripping the * `git+` prefix and `.git` suffix npm conventionally adds. */ function parseRepoSlug(repository) { const url = typeof repository === 'string' ? repository : (repository && repository.url) || ''; const m = url.replace(/^git\+/, '').replace(/\.git$/, '').match(/github\.com[/:]([^/]+\/[^/]+)$/); return m ? m[1] : ''; } /** * Pure: package.json object -> the package identity coordinates. */ function deriveIdentity(pkg = {}) { const packageName = pkg.name || ''; const binName = pkg.bin ? Object.keys(pkg.bin)[0] || '' : ''; const repoSlug = parseRepoSlug(pkg.repository); const repoUrl = repoSlug ? `https://github.com/${repoSlug}` : ''; const changelogRawUrl = repoSlug ? `https://raw.githubusercontent.com/${repoSlug}/main/CHANGELOG.md` : ''; return { packageName, binName, repoSlug, repoUrl, changelogRawUrl }; } /** * Pure: format the `npx` fallback install command. Shape matches the literal * the update workflow embeds: `npx -y --package=@latest -- * [--] --`. The runtime flag is omitted when not supplied. * * This function is the canonical source — `render()` serializes it verbatim * into the generated module, so the runtime copy can never drift from it. */ function formatManualInstall({ packageName, binName, scope, runtime } = {}) { const runtimeFlag = runtime ? ` --${runtime}` : ''; return `npx -y --package=${packageName}@latest -- ${binName}${runtimeFlag} --${scope}`; } const GENERATED_HEADER = '// @generated by scripts/generate-package-identity.cjs from package.json — DO NOT EDIT.\n' + '// Single source for GSD package coordinates (issue #498). Regenerate with:\n' + '// node scripts/generate-package-identity.cjs\n'; /** * Render the generated runtime module text for a derived identity. Values are * baked as literals; `formatManualInstall` is embedded by `.toString()` so the * runtime command builder is byte-identical to the tested source above. */ function render(identity) { const { packageName, binName, repoSlug, repoUrl, changelogRawUrl } = identity; const j = (v) => JSON.stringify(v); return ( GENERATED_HEADER + "'use strict';\n\n" + `const packageName = ${j(packageName)};\n` + `const binName = ${j(binName)};\n` + `const repoSlug = ${j(repoSlug)};\n` + `const repoUrl = ${j(repoUrl)};\n` + `const changelogRawUrl = ${j(changelogRawUrl)};\n\n` + `${formatManualInstall.toString()}\n\n` + 'function manualInstallCommand(opts = {}) {\n' + ' return formatManualInstall({ packageName, binName, scope: opts.scope, runtime: opts.runtime });\n' + '}\n\n' + 'module.exports = Object.freeze({\n' + ' packageName,\n' + ' // PACKAGE_NAME: back-compat alias for #516-era consumers. Baked here, so it\n' + ' // survives the installed tree’s synthetic package.json (fixes the #378 undefined).\n' + ' PACKAGE_NAME: packageName,\n' + ' binName,\n' + ' repoSlug,\n' + ' repoUrl,\n' + ' changelogRawUrl,\n' + ' manualInstallCommand,\n' + '});\n' ); } function main() { const fs = require('node:fs'); const path = require('node:path'); const pkg = require(path.join(__dirname, '..', 'package.json')); const out = path.join(__dirname, '..', 'get-shit-done', 'bin', 'lib', 'package-identity.cjs'); fs.writeFileSync(out, render(deriveIdentity(pkg))); process.stdout.write(`wrote ${path.relative(path.join(__dirname, '..'), out)}\n`); } if (require.main === module) main(); module.exports = { deriveIdentity, parseRepoSlug, formatManualInstall, render, main };