/** * Build and sign an adaptation pack from this repository's patches/ (packages/patcher/src/patch-pack/). * * node scripts/build-patch-pack.ts --version [--out ] [--min-app-version ] [--unsigned] * * The private key comes from the environment variable PATCH_SIGNING_KEY (PEM text), as the release * workflow .github/workflows/patch-pack.yml passes it from the GitHub secret of the same name. * --unsigned writes the zip without a signature, for a look at the contents only: the launcher * refuses an unsigned pack. In GitHub Actions --version defaults to the pushed tag * (`patches-`). * * Only committed files go into a pack: the script refuses when patches/ has uncommitted changes or * untracked files, so a version record is released only after it was committed (it is committed * once the build was played in the game, packages/patcher/src/version-record.ts). --min-app-version defaults to * the launcher version in apps/launcher/package.json, the oldest launcher whose patch engine is * known to read this pack. * * Writes /wtoffline-patches-.zip, the .zip.sig and the .manifest.json next to it. */ import { execFileSync } from 'node:child_process'; import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'; import { join } from 'node:path'; import { fileURLToPath } from 'node:url'; import { parseArgs } from 'node:util'; import { checkPatchSet, formatManifest, isPackVersion, packFileNames, packGameVersions, packSigningPublicKey, packVersionOfTag, PATCH_PACK_FORMAT, signPatchPack, verifyPatchPack, writePatchPack, type PatchPackFile, type PatchPackManifest, } from '@wto/patcher'; const REPOSITORY = fileURLToPath(new URL('..', import.meta.url)); const PATCHES = join(REPOSITORY, 'patches'); const PACKED_FOLDERS = ['patches/exe', 'patches/gui', 'patches/native', 'patches/versions']; const USAGE = 'usage: build-patch-pack.ts --version [--out ] [--min-app-version ] [--unsigned]'; function main(): number { const { values } = parseArgs({ options: { version: { type: 'string' }, out: { type: 'string', default: join(REPOSITORY, 'build', 'patch-packs') }, 'min-app-version': { type: 'string' }, unsigned: { type: 'boolean', default: false }, }, }); const version = values.version ?? tagVersion(); if (!version || !isPackVersion(version)) { console.error(version ? `not a pack version: ${version}` : USAGE); return 2; } const privateKey = process.env['PATCH_SIGNING_KEY']; if (!values.unsigned && !privateKey) { console.error('PATCH_SIGNING_KEY is not set; pass --unsigned for a pack to look at only'); return 2; } const uncommitted = git(['status', '--porcelain', '--untracked-files=all', '--', ...PACKED_FOLDERS]).trim(); if (uncommitted) { console.error(`patches/ has uncommitted changes; commit them first:\n${uncommitted}`); return 1; } const summary = checkPatchSet(PATCHES); const files = committedFiles(); const manifest: PatchPackManifest = { format: PATCH_PACK_FORMAT, version, minAppVersion: values['min-app-version'] ?? launcherVersion(), gameVersions: packGameVersions(files), createdAt: new Date().toISOString(), }; const zip = writePatchPack({ manifest, files }); const names = packFileNames(version); mkdirSync(values.out, { recursive: true }); writeFileSync(join(values.out, names.zip), zip); writeFileSync(join(values.out, names.manifest), formatManifest(manifest)); if (privateKey && !values.unsigned) { const signature = signPatchPack(zip, privateKey); const publicKey = packSigningPublicKey(); if (!publicKey) { console.error('no committed public key (packages/patcher/keys/); run scripts/generate-patch-signing-key.ts first'); return 1; } if (!verifyPatchPack(zip, signature, publicKey)) { console.error('the signature does not verify against the committed public key: PATCH_SIGNING_KEY is not its private key'); return 1; } writeFileSync(join(values.out, names.signature), signature); } console.log(`pack ${version} (format ${manifest.format}, launcher ${manifest.minAppVersion} or newer)`); console.log(` ${summary.exePatches} exe patch(es), ${summary.guiPatches} GUI patch(es), ${summary.nativeModules} native module(s)`); console.log(` game versions: ${manifest.gameVersions.join(', ') || 'none'}`); console.log(` ${files.length} file(s), ${zip.length} bytes zipped${values.unsigned ? ', NOT signed' : ', signed'}`); console.log(`wrote ${join(values.out, names.zip)}`); return 0; } /** In GitHub Actions, the version of the pushed `patches-*` tag. */ function tagVersion(): string | undefined { const ref = process.env['GITHUB_REF_NAME']; return ref ? (packVersionOfTag(ref) ?? undefined) : undefined; } function committedFiles(): PatchPackFile[] { return git(['ls-files', '-z', '--', ...PACKED_FOLDERS]) .split('\0') .filter((path) => path.length > 0) .map((path) => ({ path: path.slice('patches/'.length), content: readFileSync(join(REPOSITORY, path)) })); } function launcherVersion(): string { const manifest = JSON.parse(readFileSync(join(REPOSITORY, 'apps', 'launcher', 'package.json'), 'utf8')) as { version?: unknown }; if (typeof manifest.version !== 'string') throw new Error('apps/launcher/package.json has no version'); return manifest.version; } function git(args: readonly string[]): string { return execFileSync('git', args, { cwd: REPOSITORY, encoding: 'utf8' }); } process.exitCode = main();