|
| 1 | +import {readFileSync, writeFileSync} from 'fs'; |
| 2 | +import {platform, release} from 'os'; |
| 3 | + |
| 4 | +console.log('⏳ Updating Deployment Info...'); |
| 5 | + |
| 6 | +const packageJsonPath = './package.json'; |
| 7 | +const packageJson = readFileSync(packageJsonPath, 'utf8'); |
| 8 | +const packageJsonObj = JSON.parse(packageJson); |
| 9 | +const version = packageJsonObj.version.split('.'); |
| 10 | +const newVersion = `${version[0]}.${version[1]}.${parseInt(version[2]) + 1}`; |
| 11 | +packageJsonObj.version = newVersion; |
| 12 | +writeFileSync(packageJsonPath, JSON.stringify(packageJsonObj, null, 2)); |
| 13 | + |
| 14 | +const publicIp = await getPublicIpAddress(); |
| 15 | +const deploymentInfo = { |
| 16 | + dateUTC: new Date().toISOString(), |
| 17 | + deviceIp: publicIp, |
| 18 | + deviceOS: formatOS(platform()), |
| 19 | + deviceOSVersion: release(), |
| 20 | + version: newVersion, |
| 21 | +}; |
| 22 | + |
| 23 | +const newDeploymentScript = ` |
| 24 | +const deployment = { |
| 25 | + dateUTC: '${deploymentInfo.dateUTC}', |
| 26 | + deviceIp: '${deploymentInfo.deviceIp}', |
| 27 | + deviceOS: '${deploymentInfo.deviceOS}', |
| 28 | + deviceOSVersion: '${deploymentInfo.deviceOSVersion}', |
| 29 | + version: '${deploymentInfo.version}', |
| 30 | +}; |
| 31 | +window.deployment = deployment; |
| 32 | +`; |
| 33 | +const deploymentScriptPattern = /const deployment = \{[\s\S]*?\};[\s\S]*?window\.deployment = deployment;/; |
| 34 | + |
| 35 | +// Read and update index.html |
| 36 | +const indexPath = './src/index.html'; |
| 37 | +let indexHtml = readFileSync(indexPath, 'utf8'); |
| 38 | +indexHtml = indexHtml.replace(deploymentScriptPattern, newDeploymentScript.trim()); |
| 39 | + |
| 40 | +writeFileSync(indexPath, indexHtml); |
| 41 | + |
| 42 | +console.log('✅ Updated Deployment Info!'); |
| 43 | + |
| 44 | +async function getPublicIpAddress() { |
| 45 | + const response = await fetch('https://api.ipify.org/'); |
| 46 | + return await response.text(); |
| 47 | +} |
| 48 | + |
| 49 | +function formatOS(platform) { |
| 50 | + const osMap = { |
| 51 | + 'darwin': 'macOS', |
| 52 | + 'win32': 'Windows', |
| 53 | + 'linux': 'Linux', |
| 54 | + }; |
| 55 | + return osMap[platform] || platform; |
| 56 | +} |
0 commit comments