generate-version.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/env node
  2. /**
  3. * Generate version.ts file with build information
  4. * This script creates a TypeScript file containing version info from git and build time
  5. */
  6. const { execSync } = require('child_process');
  7. const fs = require('fs');
  8. const path = require('path');
  9. function getGitHash() {
  10. try {
  11. return execSync('git rev-parse --short HEAD', { encoding: 'utf-8' }).trim();
  12. } catch (error) {
  13. console.warn('Warning: Could not get git hash, using "unknown"');
  14. return 'unknown';
  15. }
  16. }
  17. function getGitBranch() {
  18. try {
  19. return execSync('git rev-parse --abbrev-ref HEAD', { encoding: 'utf-8' }).trim();
  20. } catch (error) {
  21. return 'unknown';
  22. }
  23. }
  24. function getBuildTime() {
  25. return new Date().toISOString();
  26. }
  27. function getPackageVersion() {
  28. try {
  29. const packageJson = JSON.parse(
  30. fs.readFileSync(path.join(__dirname, '../package.json'), 'utf-8')
  31. );
  32. return packageJson.version;
  33. } catch (error) {
  34. return '1.0.0';
  35. }
  36. }
  37. const gitHash = getGitHash();
  38. const gitBranch = getGitBranch();
  39. const buildTime = getBuildTime();
  40. const packageVersion = getPackageVersion();
  41. const versionContent = `/**
  42. * Auto-generated version information
  43. * DO NOT EDIT THIS FILE MANUALLY
  44. * Generated at build time by scripts/generate-version.js
  45. */
  46. export const VERSION_INFO = {
  47. version: '${packageVersion}',
  48. gitHash: '${gitHash}',
  49. gitBranch: '${gitBranch}',
  50. buildTime: '${buildTime}',
  51. fullVersion: '${packageVersion}+${gitHash}',
  52. } as const;
  53. export function getVersion(): string {
  54. return VERSION_INFO.fullVersion;
  55. }
  56. export function getUserAgent(): string {
  57. return \`ShopCallCrawler/\${VERSION_INFO.fullVersion} (+https://shopcall.ai/crawler)\`;
  58. }
  59. `;
  60. const outputPath = path.join(__dirname, '../src/version.ts');
  61. try {
  62. fs.writeFileSync(outputPath, versionContent, 'utf-8');
  63. console.log(`✓ Generated version file: ${outputPath}`);
  64. console.log(` Version: ${packageVersion}+${gitHash}`);
  65. console.log(` Branch: ${gitBranch}`);
  66. console.log(` Build time: ${buildTime}`);
  67. } catch (error) {
  68. console.error('Error writing version file:', error);
  69. process.exit(1);
  70. }