Files
sencho/backend/scripts/generate-version.js
T
Anso 670a429168 fix(fleet): resolve getSenchoVersion crash in Docker containers (#396)
* fix(fleet): resolve getSenchoVersion crash in Docker containers

Replace the runtime __dirname walk (which fails in Docker because the
root package.json is absent from the final image) with a build-time
version injection. A prebuild script reads the root package.json and
generates src/generated/version.ts with the version baked in as a
constant. The compiled output carries the correct version regardless
of the container's filesystem layout.

Retains a filesystem walk fallback for dev environments where the
prebuild hook may not have run.

* fix(fleet): skip postinstall version generation when script is absent

The Docker prod-deps stage copies only package.json (no source files)
before running npm ci --omit=dev. The postinstall hook now checks for
the script file before executing, so it no-ops in stages where
scripts/generate-version.js has not been copied.
2026-04-06 00:44:36 -04:00

28 lines
1.0 KiB
JavaScript

#!/usr/bin/env node
// Reads the version from the root package.json and writes it into
// src/generated/version.ts so it's available as a compiled constant.
// Invoked automatically by prebuild/predev/postinstall npm hooks.
const fs = require('fs');
const path = require('path');
const rootPkg = path.resolve(__dirname, '..', '..', 'package.json');
const outFile = path.resolve(__dirname, '..', 'src', 'generated', 'version.ts');
let version = '0.0.0-dev';
try {
const pkg = JSON.parse(fs.readFileSync(rootPkg, 'utf8'));
if (pkg.version) version = pkg.version;
} catch {
console.warn('[generate-version] Could not read root package.json, using fallback version');
}
const content = `// Auto-generated by prebuild script. Do not edit manually.
// Regenerated on every \`npm run build\` from the root package.json.
export const SENCHO_VERSION: string = ${JSON.stringify(version)};
`;
fs.mkdirSync(path.dirname(outFile), { recursive: true });
fs.writeFileSync(outFile, content, 'utf8');
console.log(`[generate-version] Wrote ${version} to src/generated/version.ts`);