#!/usr/bin/env node /** * Generate version.ts file with build information * This script creates a TypeScript file containing version info from git and build time */ const { execSync } = require('child_process'); const fs = require('fs'); const path = require('path'); function getGitHash() { try { return execSync('git rev-parse --short HEAD', { encoding: 'utf-8' }).trim(); } catch (error) { console.warn('Warning: Could not get git hash, using "unknown"'); return 'unknown'; } } function getGitBranch() { try { return execSync('git rev-parse --abbrev-ref HEAD', { encoding: 'utf-8' }).trim(); } catch (error) { return 'unknown'; } } function getBuildTime() { return new Date().toISOString(); } function getPackageVersion() { try { const packageJson = JSON.parse( fs.readFileSync(path.join(__dirname, '../package.json'), 'utf-8') ); return packageJson.version; } catch (error) { return '1.0.0'; } } const gitHash = getGitHash(); const gitBranch = getGitBranch(); const buildTime = getBuildTime(); const packageVersion = getPackageVersion(); const versionContent = `/** * Auto-generated version information * DO NOT EDIT THIS FILE MANUALLY * Generated at build time by scripts/generate-version.js */ export const VERSION_INFO = { version: '${packageVersion}', gitHash: '${gitHash}', gitBranch: '${gitBranch}', buildTime: '${buildTime}', fullVersion: '${packageVersion}+${gitHash}', } as const; export function getVersion(): string { return VERSION_INFO.fullVersion; } export function getUserAgent(): string { return \`ShopCallCrawler/\${VERSION_INFO.fullVersion} (+https://shopcall.ai/crawler)\`; } `; const outputPath = path.join(__dirname, '../src/version.ts'); try { fs.writeFileSync(outputPath, versionContent, 'utf-8'); console.log(`✓ Generated version file: ${outputPath}`); console.log(` Version: ${packageVersion}+${gitHash}`); console.log(` Branch: ${gitBranch}`); console.log(` Build time: ${buildTime}`); } catch (error) { console.error('Error writing version file:', error); process.exit(1); }