Skip to content

Commit 2a3e7cc

Browse files
pkarwgrabbou
andauthored
feat(create-fabrice-ai): downloading latest release (#116)
Before, we downloaded the `main` snapshot. It was fine. However, it didn't work well when the current `main` source code was incompatible with the released `npm` modules. This situation happened with the recent #105. Now, we're getting the latest official release and taking the examples out of it. Because the tarball starts with a directory of an unknown name (in fact, it's the `org-repo-lastcommitid` kind of) - we're about to extract the full release and not only the example folder (as it was before) because the leading directory name is unknown. --------- Co-authored-by: Mike Grabowski <[email protected]>
1 parent 85abe0b commit 2a3e7cc

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

packages/create-fabrice-ai/src/index.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
downloadAndExtractTemplate,
1414
formatTargetDir,
1515
isNodeError,
16+
latestReleaseDownloadLink,
1617
} from './utils.js'
1718

1819
console.log(
@@ -133,13 +134,11 @@ if (typeof template !== 'object') {
133134

134135
const s = spinner()
135136

136-
s.start('Downloading template...')
137+
const releaseTarballUrl = await latestReleaseDownloadLink('callstackincubator', 'fabrice-ai')
137138

138-
await downloadAndExtractTemplate(
139-
root,
140-
'https://github.com/callstackincubator/fabrice-ai/archive/refs/heads/main.tar.gz',
141-
template.files
142-
)
139+
s.start(`Downloading template...`)
140+
141+
await downloadAndExtractTemplate(root, releaseTarballUrl, template.files)
143142

144143
copyAdditionalTemplateFiles(root)
145144

packages/create-fabrice-ai/src/utils.ts

+24-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,26 @@ export function formatTargetDir(targetDir: string) {
99
return targetDir.trim().replace(/\/+$/g, '')
1010
}
1111

12+
export async function latestReleaseDownloadLink(
13+
organization: string,
14+
repo: string
15+
): Promise<string> {
16+
const response = await fetch(
17+
`https://api.github.com/repos/${organization}/${repo}/releases/latest`
18+
)
19+
20+
if (!response.ok) {
21+
throw new Error(`Failed to fetch release info from ${response.url}.`)
22+
}
23+
24+
const body = await response.json()
25+
if (!('tarball_url' in body) || typeof body.tarball_url !== 'string') {
26+
throw new Error(`Failed to get tarball url from ${response.url}.`)
27+
}
28+
29+
return body.tarball_url
30+
}
31+
1232
export async function downloadAndExtractTemplate(root: string, tarball: string, files: string[]) {
1333
const response = await fetch(tarball)
1434
if (!response.ok || !response.body) {
@@ -18,13 +38,10 @@ export async function downloadAndExtractTemplate(root: string, tarball: string,
1838
await Stream.pipeline([
1939
// @ts-ignore
2040
Readable.fromWeb(response.body),
21-
extract(
22-
{
23-
cwd: tmpdir(),
24-
strip: 1,
25-
},
26-
['fabrice-ai-main/example']
27-
),
41+
extract({
42+
cwd: path.join(import.meta.dirname),
43+
strip: 1,
44+
}),
2845
])
2946

3047
const filesToCopy = [...files, 'package.json']

0 commit comments

Comments
 (0)