diff --git a/packages/create-fabrice-ai/src/index.ts b/packages/create-fabrice-ai/src/index.ts index 9c1e3a2..825ece7 100644 --- a/packages/create-fabrice-ai/src/index.ts +++ b/packages/create-fabrice-ai/src/index.ts @@ -13,6 +13,7 @@ import { downloadAndExtractTemplate, formatTargetDir, isNodeError, + latestReleaseDownloadLink, } from './utils.js' console.log( @@ -133,13 +134,11 @@ if (typeof template !== 'object') { const s = spinner() -s.start('Downloading template...') +const releaseTarballUrl = await latestReleaseDownloadLink('callstackincubator', 'fabrice-ai') -await downloadAndExtractTemplate( - root, - 'https://github.com/callstackincubator/fabrice-ai/archive/refs/heads/main.tar.gz', - template.files -) +s.start(`Downloading template...`) + +await downloadAndExtractTemplate(root, releaseTarballUrl, template.files) copyAdditionalTemplateFiles(root) diff --git a/packages/create-fabrice-ai/src/utils.ts b/packages/create-fabrice-ai/src/utils.ts index 8c41d34..9541411 100644 --- a/packages/create-fabrice-ai/src/utils.ts +++ b/packages/create-fabrice-ai/src/utils.ts @@ -9,6 +9,26 @@ export function formatTargetDir(targetDir: string) { return targetDir.trim().replace(/\/+$/g, '') } +export async function latestReleaseDownloadLink( + organization: string, + repo: string +): Promise { + const response = await fetch( + `https://api.github.com/repos/${organization}/${repo}/releases/latest` + ) + + if (!response.ok) { + throw new Error(`Failed to fetch release info from ${response.url}.`) + } + + const body = await response.json() + if (!('tarball_url' in body) || typeof body.tarball_url !== 'string') { + throw new Error(`Failed to get tarball url from ${response.url}.`) + } + + return body.tarball_url +} + export async function downloadAndExtractTemplate(root: string, tarball: string, files: string[]) { const response = await fetch(tarball) if (!response.ok || !response.body) { @@ -18,13 +38,10 @@ export async function downloadAndExtractTemplate(root: string, tarball: string, await Stream.pipeline([ // @ts-ignore Readable.fromWeb(response.body), - extract( - { - cwd: tmpdir(), - strip: 1, - }, - ['fabrice-ai-main/example'] - ), + extract({ + cwd: path.join(import.meta.dirname), + strip: 1, + }), ]) const filesToCopy = [...files, 'package.json']