|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | + |
| 6 | +/** |
| 7 | + * Updates the VERSION constant in version.ts with the version from task.json |
| 8 | + * This script is run during the build process to ensure the user agent includes the correct version |
| 9 | + */ |
| 10 | + |
| 11 | +const taskJsonPath = path.join(__dirname, '..', 'task.json'); |
| 12 | +const versionPath = path.join(__dirname, '..', 'dist', 'src', 'utils', 'version.js'); |
| 13 | + |
| 14 | +try { |
| 15 | + // Read task.json to get the version |
| 16 | + const taskJsonContent = fs.readFileSync(taskJsonPath, 'utf8'); |
| 17 | + console.log('Task.json file read successfully'); |
| 18 | + |
| 19 | + let taskJson; |
| 20 | + try { |
| 21 | + taskJson = JSON.parse(taskJsonContent); |
| 22 | + } catch (parseError) { |
| 23 | + console.error('Error parsing task.json:'); |
| 24 | + console.error('Parse error:', parseError.message); |
| 25 | + console.error('File content length:', taskJsonContent.length); |
| 26 | + console.error('Content around error position:'); |
| 27 | + if (parseError.message.includes('position')) { |
| 28 | + const position = parseInt(parseError.message.match(/position (\d+)/)?.[1] || '0'); |
| 29 | + const start = Math.max(0, position - 50); |
| 30 | + const end = Math.min(taskJsonContent.length, position + 50); |
| 31 | + console.error(taskJsonContent.substring(start, end)); |
| 32 | + } |
| 33 | + process.exit(1); |
| 34 | + } |
| 35 | + |
| 36 | + const version = `${taskJson.version.Major}.${taskJson.version.Minor}.${taskJson.version.Patch}`; |
| 37 | + |
| 38 | + console.log(`Updating version to: ${version}`); |
| 39 | + |
| 40 | + // Check if the version file exists |
| 41 | + if (!fs.existsSync(versionPath)) { |
| 42 | + console.error(`Version file not found at: ${versionPath}`); |
| 43 | + console.error('Make sure to run TypeScript compilation first'); |
| 44 | + process.exit(1); |
| 45 | + } |
| 46 | + |
| 47 | + // Read the compiled version.js file |
| 48 | + let versionContent = fs.readFileSync(versionPath, 'utf8'); |
| 49 | + |
| 50 | + // Replace the VERSION export |
| 51 | + const versionRegex = /exports\.VERSION = ['"]dev['"];/; |
| 52 | + const replacement = `exports.VERSION = "${version}";`; |
| 53 | + |
| 54 | + if (!versionRegex.test(versionContent)) { |
| 55 | + console.error('Could not find VERSION export in version.js'); |
| 56 | + console.error('Expected pattern: exports.VERSION = "dev";'); |
| 57 | + process.exit(1); |
| 58 | + } |
| 59 | + |
| 60 | + versionContent = versionContent.replace(versionRegex, replacement); |
| 61 | + |
| 62 | + // Write the updated content back |
| 63 | + fs.writeFileSync(versionPath, versionContent, 'utf8'); |
| 64 | + |
| 65 | + console.log('Version updated successfully in version.js'); |
| 66 | + |
| 67 | +} catch (error) { |
| 68 | + console.error('Error updating version:', error.message); |
| 69 | + process.exit(1); |
| 70 | +} |
0 commit comments