#!/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`);
