|
| 1 | +import { execFile } from 'node:child_process' |
| 2 | +import { access, chmod, rm } from 'node:fs/promises' |
| 3 | +import { dirname, resolve } from 'node:path' |
| 4 | +import { fileURLToPath } from 'node:url' |
| 5 | +import { promisify } from 'node:util' |
| 6 | + |
| 7 | +const execFileAsync = promisify(execFile) |
| 8 | + |
| 9 | +const filePath = fileURLToPath(import.meta.url) |
| 10 | +const repoRoot = resolve(dirname(filePath), '..') |
| 11 | +const nodePackage = resolve(repoRoot, 'packages/node') |
| 12 | +const nodeDistCli = resolve(nodePackage, 'dist/cli.js') |
| 13 | + |
| 14 | +function shouldReusePreparedNodeDist(hasNodeDist: boolean): boolean { |
| 15 | + return process.env.TRANSLOADIT_PUBLISH_PREBUILT_NODE === 'true' && hasNodeDist |
| 16 | +} |
| 17 | + |
| 18 | +const ensureNodePackagePrepared = async (): Promise<void> => { |
| 19 | + const hasNodeDist = await access(nodeDistCli) |
| 20 | + .then(() => true) |
| 21 | + .catch(() => false) |
| 22 | + |
| 23 | + if (shouldReusePreparedNodeDist(hasNodeDist)) { |
| 24 | + await chmod(nodeDistCli, 0o755) |
| 25 | + return |
| 26 | + } |
| 27 | + |
| 28 | + await rm(resolve(nodePackage, 'dist'), { recursive: true, force: true }) |
| 29 | + await rm(resolve(nodePackage, 'tsconfig.tsbuildinfo'), { force: true }) |
| 30 | + await rm(resolve(nodePackage, 'tsconfig.build.tsbuildinfo'), { force: true }) |
| 31 | + await execFileAsync('yarn', ['tsc:node'], { |
| 32 | + cwd: repoRoot, |
| 33 | + }) |
| 34 | + await chmod(nodeDistCli, 0o755) |
| 35 | +} |
| 36 | + |
| 37 | +if (process.argv[1] != null && resolve(process.argv[1]) === filePath) { |
| 38 | + await ensureNodePackagePrepared() |
| 39 | +} |
| 40 | + |
| 41 | +export { ensureNodePackagePrepared, shouldReusePreparedNodeDist } |
0 commit comments