Skip to content

Commit 5490a39

Browse files
committed
refactor(cli): fetch application template without giget
1 parent c54d179 commit 5490a39

File tree

4 files changed

+62
-86
lines changed

4 files changed

+62
-86
lines changed

.changeset/sour-states-dig.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cartesi/cli": patch
3+
---
4+
5+
fetch application template without giget dependency

apps/cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
"execa": "^9.6.0",
3131
"fs-extra": "^11.3.2",
3232
"get-port": "^7.1.0",
33-
"giget": "^2.0.0",
3433
"listr2": "^9.0.4",
3534
"lookpath": "^1.2.3",
35+
"modern-tar": "^0.7.3",
3636
"ora": "^9.0.0",
3737
"p-retry": "^7.0.0",
3838
"progress-stream": "^2.0.0",

apps/cli/src/template.ts

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,56 @@
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+
}
311

412
export const download = async (
513
template: string,
614
branch: string,
715
out: string,
816
): 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}`;
1719

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+
},
2244
});
45+
46+
await pipeline(
47+
response.body as ReadableStream,
48+
createGunzip(),
49+
extractStream,
50+
);
51+
52+
return {
53+
dir: out,
54+
source: repoUrl,
55+
};
2356
};

pnpm-lock.yaml

Lines changed: 9 additions & 71 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)