|
| 1 | +import { readFile, writeFile } from 'node:fs/promises' |
| 2 | +import { fileURLToPath } from 'node:url' |
| 3 | + |
| 4 | +import { $ } from 'execa' |
| 5 | + |
| 6 | +const versionFile = './src/lib/version.ts' |
| 7 | + |
| 8 | +const main = async (): Promise<void> => { |
| 9 | + const version = await injectVersion( |
| 10 | + fileURLToPath(new URL(versionFile, import.meta.url)), |
| 11 | + ) |
| 12 | + // eslint-disable-next-line no-console |
| 13 | + console.log(`✓ Version ${version} injected into ${versionFile}`) |
| 14 | + |
| 15 | + const { command } = await $`tsc --project tsconfig.version.json` |
| 16 | + // eslint-disable-next-line no-console |
| 17 | + console.log(`✓ Rebuilt with '${command}'`) |
| 18 | +} |
| 19 | + |
| 20 | +const injectVersion = async (path: string): Promise<string> => { |
| 21 | + const { version } = await readPackageJson() |
| 22 | + |
| 23 | + if (version == null) { |
| 24 | + throw new Error('Missing version in package.json') |
| 25 | + } |
| 26 | + |
| 27 | + const buff = await readFile(path) |
| 28 | + |
| 29 | + const data = buff |
| 30 | + .toString() |
| 31 | + .replace( |
| 32 | + 'const seamapiJavascriptHttpVersion = null', |
| 33 | + `const seamapiJavascriptHttpVersion = '${version}'`, |
| 34 | + ) |
| 35 | + |
| 36 | + await writeFile(path, data) |
| 37 | + |
| 38 | + return version |
| 39 | +} |
| 40 | + |
| 41 | +const readPackageJson = async (): Promise<{ version?: string }> => { |
| 42 | + const pkgBuff = await readFile( |
| 43 | + fileURLToPath(new URL('package.json', import.meta.url)), |
| 44 | + ) |
| 45 | + return JSON.parse(pkgBuff.toString()) |
| 46 | +} |
| 47 | + |
| 48 | +await main() |
0 commit comments