|
1 | | -import path from 'path' |
2 | | -import fs from 'fs' |
| 1 | +import { createHash } from 'node:crypto' |
| 2 | +import fs from 'node:fs' |
| 3 | +import path from 'node:path' |
3 | 4 |
|
| 5 | +import artifact from './ui_builder_artifact.json' with { type: 'json' } |
4 | 6 |
|
5 | | -// Check if we're in node_modules (installed as dependency) |
| 7 | +// Skip when installed as a dependency or outside the root project |
6 | 8 | if (process.cwd().includes('node_modules')) { |
7 | | - console.log('Skipping postinstall - running as dependency'); |
8 | | - process.exit(0); |
| 9 | + console.log('Skipping postinstall - running as dependency') |
| 10 | + process.exit(0) |
9 | 11 | } |
10 | | - |
11 | | -// Check if we're in the root project |
12 | 12 | if (process.env.INIT_CWD && process.env.INIT_CWD !== process.cwd()) { |
13 | | - console.log('Skipping postinstall - not root project'); |
14 | | - process.exit(0); |
| 13 | + console.log('Skipping postinstall - not root project') |
| 14 | + process.exit(0) |
15 | 15 | } |
16 | 16 |
|
17 | | -// Your actual postinstall logic here |
18 | | -console.log('Running postinstall for root project'); |
| 17 | +console.log('Running postinstall for root project') |
19 | 18 |
|
| 19 | +const tarUrl = `${artifact.baseUrl}/ui_builder-${artifact.version}.tar.gz` |
| 20 | +const response = await fetch(tarUrl) |
| 21 | +if (!response.ok) { |
| 22 | + throw new Error(`Failed to download ${tarUrl}: ${response.status} ${response.statusText}`) |
| 23 | +} |
20 | 24 |
|
21 | | -import { x } from 'tar' |
| 25 | +const buffer = Buffer.from(await response.arrayBuffer()) |
| 26 | +const sha256 = createHash('sha256').update(buffer).digest('hex') |
| 27 | +if (sha256 !== artifact.sha256) { |
| 28 | + throw new Error( |
| 29 | + `UI builder artifact checksum mismatch: expected ${artifact.sha256}, got ${sha256}` |
| 30 | + ) |
| 31 | +} |
22 | 32 |
|
23 | | -const tarUrl = 'https://pub-06154ed168a24e73a86ab84db6bf15d8.r2.dev/ui_builder-6715153.tar.gz' |
24 | 33 | const outputTarPath = path.join(process.cwd(), 'ui_builder.tar.gz') |
25 | 34 | const extractTo = path.join(process.cwd(), 'static/ui_builder/') |
26 | 35 |
|
27 | | -import { fileURLToPath } from 'url' |
28 | | -import { dirname } from 'path' |
29 | | - |
30 | | -const __filename = fileURLToPath(import.meta.url) |
31 | | -const __dirname = dirname(__filename) |
| 36 | +await fs.promises.mkdir(extractTo, { recursive: true }) |
| 37 | +await fs.promises.writeFile(outputTarPath, buffer) |
32 | 38 |
|
33 | | -// Download the tar file |
34 | | -const response = await fetch(tarUrl) |
35 | | -const buffer = await response.arrayBuffer() |
36 | | -await fs.promises.writeFile(outputTarPath, Buffer.from(buffer)) |
37 | | - |
38 | | - |
39 | | -// Create extract directory if it doesn't exist |
| 39 | +const { x } = await import('tar') |
40 | 40 | try { |
41 | | - await fs.promises.mkdir(extractTo, { recursive: true }) |
42 | | -} catch (err) { |
43 | | - if (err.code !== 'EEXIST') { |
44 | | - throw err |
45 | | - } |
| 41 | + await x({ |
| 42 | + file: outputTarPath, |
| 43 | + cwd: extractTo, |
| 44 | + sync: false, |
| 45 | + gzip: true, |
| 46 | + preservePaths: false |
| 47 | + }) |
| 48 | +} finally { |
| 49 | + await fs.promises.rm(outputTarPath, { force: true }) |
46 | 50 | } |
47 | | - |
48 | | -await x({ |
49 | | - file: outputTarPath, |
50 | | - cwd: extractTo, |
51 | | - sync: false, |
52 | | - gzip: true |
53 | | -}) |
54 | | - |
55 | | -await fs.promises.unlink(outputTarPath) |
0 commit comments