|
1 | | -import type { TemplateProvider } from "giget"; |
2 | | -import { type DownloadTemplateResult, downloadTemplate } from "giget"; |
| 1 | +import { ensureDir } from "fs-extra"; |
| 2 | +import { unpackTar } from "modern-tar/fs"; |
| 3 | +import path from "node:path"; |
| 4 | +import { pipeline } from "node:stream/promises"; |
| 5 | +import { createGunzip } from "node:zlib"; |
| 6 | + |
| 7 | +export interface DownloadTemplateResult { |
| 8 | + dir: string; |
| 9 | + source: string; |
| 10 | +} |
3 | 11 |
|
4 | 12 | export const download = async ( |
5 | 13 | template: string, |
6 | 14 | branch: string, |
7 | 15 | out: string, |
8 | 16 | ): Promise<DownloadTemplateResult> => { |
9 | | - const cartesiProvider: TemplateProvider = async (input) => { |
10 | | - return { |
11 | | - name: "cartesi", |
12 | | - subdir: input, |
13 | | - url: "https://github.com/cartesi/application-templates", |
14 | | - tar: `https://codeload.github.com/cartesi/application-templates/tar.gz/refs/heads/${branch}`, |
15 | | - }; |
16 | | - }; |
| 17 | + const repoUrl = "https://github.com/cartesi/application-templates"; |
| 18 | + const tarballUrl = `https://codeload.github.com/cartesi/application-templates/tar.gz/refs/heads/${branch}`; |
17 | 19 |
|
18 | | - const input = `cartesi:${template}`; |
19 | | - return downloadTemplate(input, { |
20 | | - dir: out, |
21 | | - providers: { cartesi: cartesiProvider }, |
| 20 | + // Ensure output directory exists |
| 21 | + await ensureDir(out); |
| 22 | + |
| 23 | + // Download the tarball |
| 24 | + const response = await fetch(tarballUrl); |
| 25 | + if (!response.ok) { |
| 26 | + throw new Error(`Failed to download template: ${response.statusText}`); |
| 27 | + } |
| 28 | + |
| 29 | + // Stream download → gunzip → extract with filtering |
| 30 | + // GitHub tarball structure: application-templates-{branch}/{template}/... |
| 31 | + // We need to extract only files within the template subdirectory |
| 32 | + const extractStream = unpackTar(out, { |
| 33 | + filter: (header) => { |
| 34 | + // remove first path segment |
| 35 | + const pathname = header.name.split("/").splice(1).join("/"); |
| 36 | + return pathname.startsWith(template); |
| 37 | + }, |
| 38 | + map: (header) => { |
| 39 | + // remove first path segment |
| 40 | + const pathname = header.name.split("/").splice(1).join("/"); |
| 41 | + header.name = path.relative(template, pathname); |
| 42 | + return header; |
| 43 | + }, |
22 | 44 | }); |
| 45 | + |
| 46 | + await pipeline( |
| 47 | + response.body as ReadableStream, |
| 48 | + createGunzip(), |
| 49 | + extractStream, |
| 50 | + ); |
| 51 | + |
| 52 | + return { |
| 53 | + dir: out, |
| 54 | + source: repoUrl, |
| 55 | + }; |
23 | 56 | }; |
0 commit comments