|
| 1 | +const fs = require('fs'); |
| 2 | +const { execSync } = require('child_process'); |
| 3 | +const os = require('os'); |
| 4 | +const { spawn, sync } = require('cross-spawn'); |
| 5 | + |
| 6 | +const d = new Date(); |
| 7 | + |
| 8 | +const buildDate = d.toISOString().split('T')[0]; |
| 9 | +const buildTime = d.toLocaleTimeString('en-US', { hour12: false, hour: '2-digit', minute: '2-digit', second: '2-digit', timeZoneName: 'short' }); |
| 10 | + |
| 11 | +const tzLongName = new Intl.DateTimeFormat('en-US', { timeZoneName: 'long' }) |
| 12 | + .formatToParts(new Date()) |
| 13 | + .find(part => part.type === 'timeZoneName').value; |
| 14 | +const tzName = new Intl.DateTimeFormat('en-US', { timeZoneName: 'short' }) |
| 15 | + .formatToParts(new Date()) |
| 16 | + .find(part => part.type === 'timeZoneName').value; |
| 17 | + |
| 18 | +const t = d.getTime(); |
| 19 | + |
| 20 | +// use cross-spawn to execute the command in a way that works across platforms |
| 21 | +var shell = os.platform() === 'win32' ? 'C:\\windows\\SysWOW64\\WindowsPowerShell\\v1.0\\powershell.exe' : '/bin/bash'; |
| 22 | +var output = spawn.sync('yarn info next --name-only', { shell: shell, encoding: 'utf8' }).output; |
| 23 | + |
| 24 | +const versionRegex = new RegExp(":(?<version>[\\d+|\\.]+)"); |
| 25 | +const result = versionRegex.exec(output); |
| 26 | +const nextJsVersion = result ? result.groups.version : "Unknown"; |
| 27 | + |
| 28 | +const envContent = ` |
| 29 | +NEXT_PUBLIC_BUILD_DATETIME="${buildDate} ${buildTime}" |
| 30 | +NEXT_PUBLIC_BUILD_DATE="${buildDate}" |
| 31 | +NEXT_PUBLIC_BUILD_DATE_LOCAL="${d.toLocaleDateString('en-US')}" |
| 32 | +NEXT_PUBLIC_BUILD_TIME="${buildTime}" |
| 33 | +NEXT_PUBLIC_BUILD_TIME_LOCAL="${d.toLocaleTimeString('en-US', { hour12: true })}" |
| 34 | +NEXT_PUBLIC_TZ_LONG="${tzLongName}" |
| 35 | +NEXT_PUBLIC_TZ_SHORT="${tzName}" |
| 36 | +NEXT_PUBLIC_NODE_VERSION="${process.version}" |
| 37 | +NEXT_PUBLIC_NEXTJS_VERSION="${nextJsVersion}" |
| 38 | +`; |
| 39 | + |
| 40 | +console.log('.env file contents:'); |
| 41 | +console.log(envContent); |
| 42 | + |
| 43 | +fs.writeFileSync('.env', envContent.trim()); |
| 44 | + |
| 45 | +console.log('Created .env file successfully'); |
0 commit comments