|
| 1 | +import { createWriteStream } from 'node:fs' |
| 2 | +import { rm, writeFile } from 'node:fs/promises' |
| 3 | +import { dirname, join } from 'node:path' |
| 4 | +import { fileURLToPath } from 'node:url' |
| 5 | +import { Readable } from 'stream' |
| 6 | +import { finished } from 'stream/promises' |
| 7 | + |
| 8 | +import { execaCommand } from 'execa' |
| 9 | + |
| 10 | +const denoToolsDirectory = join(dirname(fileURLToPath(import.meta.url)), 'deno') |
| 11 | + |
| 12 | +try { |
| 13 | + await execaCommand('deno --version') |
| 14 | +} catch { |
| 15 | + throw new Error('Could not check the version of Deno. Is it installed on your system?') |
| 16 | +} |
| 17 | + |
| 18 | +const vendorDest = join(denoToolsDirectory, 'vendor') |
| 19 | + |
| 20 | +console.log(`🧹 Deleting '${vendorDest}'...`) |
| 21 | +await rm(vendorDest, { force: true, recursive: true }) |
| 22 | + |
| 23 | +const denoJsonPath = join(denoToolsDirectory, 'deno.json') |
| 24 | + |
| 25 | +console.log(`🧹 Generating clean '${denoJsonPath}`) |
| 26 | + |
| 27 | +await writeFile(denoJsonPath, '{}') |
| 28 | + |
| 29 | +console.log(`📦 Vendoring Deno modules into '${vendorDest}'...`) |
| 30 | + |
| 31 | +await execaCommand('deno vendor eszip.ts', { |
| 32 | + cwd: denoToolsDirectory, |
| 33 | +}) |
| 34 | + |
| 35 | +// eszip contains wasm files and those don't currently work great with vendoring |
| 36 | +// see https://github.com/denoland/deno/issues/14123 |
| 37 | +// to workaround this we copy the wasm files manually |
| 38 | +const filesToDownload = ['https://deno.land/x/eszip@v0.55.4/eszip_wasm_bg.wasm'] |
| 39 | +await Promise.all( |
| 40 | + filesToDownload.map(async (urlString) => { |
| 41 | + const url = new URL(urlString) |
| 42 | + |
| 43 | + const destination = join(vendorDest, url.hostname, url.pathname) |
| 44 | + |
| 45 | + const res = await fetch(url) |
| 46 | + if (!res.ok) throw new Error('Failed to fetch .wasm file to vendor', { cause: err }) |
| 47 | + const fileStream = createWriteStream(destination, { flags: 'wx' }) |
| 48 | + await finished(Readable.fromWeb(res.body).pipe(fileStream)) |
| 49 | + }), |
| 50 | +) |
| 51 | + |
| 52 | +console.log('📦 Vendored dependencies for eszip helper') |
0 commit comments