import assert from 'node:assert/strict'; import { mkdir, readFile } from 'node:fs/promises'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { runFile, sha256, writeAtomic, writeJSON } from './runtime-assets.mjs'; import { installationInstructions } from './install-onboarding.mjs'; import { checkReleaseVersion, releaseTemplateEnvironment } from './release-version.mjs'; export function validateGuideSpec(name, version) { assert.match(name || '', /^(?:@[a-z0-9][a-z0-9._-]*\/)?[a-z0-9][a-z0-9._-]*$/, 'Require an npm package name'); assert(name.length <= 214, 'npm package name is too long'); checkReleaseVersion(version); return version.includes('-test.') ? 'test' : 'stable'; } export function validateGuideIdentity(pkg, manifest) { validateGuideSpec(pkg.name, pkg.version); assert.equal(pkg.private, false, 'Only public release candidates may have a public installation guide'); assert.equal(pkg.version, manifest.version, 'Package and embedded manifest versions differ'); assert.equal(manifest.schemaVersion, 1, 'Unsupported release manifest'); assert.equal(manifest.templateEnvironment, releaseTemplateEnvironment(pkg.version), 'Template defaults must match the release channel'); assert.equal(pkg.bin?.openmotion, 'scripts/npm-installer.mjs', 'Missing OpenMotion entry point'); assert.match(manifest.commit || '', /^[a-f0-9]{40}$/, 'Require the built source SHA'); for (const hash of [manifest.application?.sha256, manifest.locks?.npm, manifest.locks?.native]) assert.match(hash || '', /^[a-f0-9]{64}$/, 'Require release checksums'); const targets = Object.values(manifest.runtime?.targets || {}); assert(targets.length > 0 && targets.every(t => t.platform && t.arch && (t.platform === 'darwin' || t.minimumOS)), 'Require native target declarations (minimum system version required outside macOS)'); return pkg.version.includes('-test.') ? 'test' : 'stable'; } export async function generateInstallGuide(tgz, output, candidate = false) { const readEntry = async entry => JSON.parse(await runFile('tar', ['-xOf', path.resolve(tgz), entry])); const pkg = await readEntry('package/package.json'), manifest = await readEntry('package/payload/release-npm.json'); const channel = validateGuideIdentity(pkg, manifest); const digest = await sha256(tgz), packageSpec = `${pkg.name}@${pkg.version}`; await installationInstructions(fileURLToPath(new URL('../', import.meta.url)), 'docs/templates/legacy-npm-install.md'); const values = { VERSION: pkg.version, PACKAGE_SPEC: packageSpec, CHANNEL: channel, COMMIT: manifest.commit, TEMPLATE_ENVIRONMENT: manifest.templateEnvironment, TGZ_SHA256: digest, TARGETS: Object.values(manifest.runtime.targets).map(t => `${t.platform}/${t.arch}${t.minimumOS ? ` (${t.minimumOS}+)` : ''}`).join(', '), DETAILS_URL: `https://github.com/nexu-io/openmotion/blob/${manifest.commit}/docs/npm-installation.md` }; const guide = await readFile(new URL('../docs/templates/legacy-npm-install.md', import.meta.url), 'utf8'); // Source checkout commands never belong to a published npm installation. const template = guide.replace(/[\s\S]*?\s*/g, '') .replace(/^# Install OpenMotion$/m, '# Install OpenMotion {{VERSION}}') .replace('skills@1.9.0 add nexu-io/openmotion ', `skills@1.9.0 add https://github.com/nexu-io/openmotion/tree/${manifest.commit} `); assert(!template.includes('