From 3bfdb5d16b501fe5ee1112bf53419f7c380e1c84 Mon Sep 17 00:00:00 2001 From: Piotr Karwatka Date: Fri, 13 Dec 2024 13:27:20 +0100 Subject: [PATCH 1/3] feat: `create-fabrice-ai`: downloading latest release --- packages/create-fabrice-ai/src/index.ts | 11 +++-- packages/create-fabrice-ai/src/utils.ts | 59 ++++++++++++++++++++++--- 2 files changed, 57 insertions(+), 13 deletions(-) diff --git a/packages/create-fabrice-ai/src/index.ts b/packages/create-fabrice-ai/src/index.ts index 9c1e3a2..716b277 100644 --- a/packages/create-fabrice-ai/src/index.ts +++ b/packages/create-fabrice-ai/src/index.ts @@ -12,6 +12,7 @@ import { copyAdditionalTemplateFiles, downloadAndExtractTemplate, formatTargetDir, + getLatestReleaseInfo, isNodeError, } from './utils.js' @@ -133,13 +134,11 @@ if (typeof template !== 'object') { const s = spinner() -s.start('Downloading template...') +const lastReleaseInfo = await getLatestReleaseInfo('callstackincubator', 'fabrice-ai') -await downloadAndExtractTemplate( - root, - 'https://github.com/callstackincubator/fabrice-ai/archive/refs/heads/main.tar.gz', - template.files -) +s.start(`Downloading template (${lastReleaseInfo.tag_name})...`) + +await downloadAndExtractTemplate(root, lastReleaseInfo.tarball_url, template.files) copyAdditionalTemplateFiles(root) diff --git a/packages/create-fabrice-ai/src/utils.ts b/packages/create-fabrice-ai/src/utils.ts index 8c41d34..6e1972d 100644 --- a/packages/create-fabrice-ai/src/utils.ts +++ b/packages/create-fabrice-ai/src/utils.ts @@ -9,6 +9,54 @@ export function formatTargetDir(targetDir: string) { return targetDir.trim().replace(/\/+$/g, '') } +export interface GithubReleaseInfo { + url: string + assets_url: string + upload_url: string + html_url: string + id: number + author: { + login: string + id: number + avatar_url: string + gravatar_id: string + url: string + repos_url: string + events_url: string + type: string + } + node_id: string + tag_name: string + target_commitish: string + name: string + draft: boolean + prerelease: boolean + created_at: string + published_at: string + assets: any[] + tarball_url: string + zipball_url: string +} + +export async function getLatestReleaseInfo( + 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}.`) + } + + return await response.json() +} + +export async function latestReleaseDownloadLink(organization: string, repo: string) { + const latestRelease = await getLatestReleaseInfo(organization, repo) + return latestRelease.tarball_url +} + export async function downloadAndExtractTemplate(root: string, tarball: string, files: string[]) { const response = await fetch(tarball) if (!response.ok || !response.body) { @@ -18,13 +66,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: tmpdir(), + strip: 1, + }), ]) const filesToCopy = [...files, 'package.json'] From dd4d44fc7d549b12632cece281b45bb276391384 Mon Sep 17 00:00:00 2001 From: Piotr Karwatka Date: Fri, 13 Dec 2024 16:50:17 +0100 Subject: [PATCH 2/3] fix: cr fixes --- packages/create-fabrice-ai/src/index.ts | 8 ++--- packages/create-fabrice-ai/src/utils.ts | 47 +++++-------------------- 2 files changed, 13 insertions(+), 42 deletions(-) diff --git a/packages/create-fabrice-ai/src/index.ts b/packages/create-fabrice-ai/src/index.ts index 716b277..825ece7 100644 --- a/packages/create-fabrice-ai/src/index.ts +++ b/packages/create-fabrice-ai/src/index.ts @@ -12,8 +12,8 @@ import { copyAdditionalTemplateFiles, downloadAndExtractTemplate, formatTargetDir, - getLatestReleaseInfo, isNodeError, + latestReleaseDownloadLink, } from './utils.js' console.log( @@ -134,11 +134,11 @@ if (typeof template !== 'object') { const s = spinner() -const lastReleaseInfo = await getLatestReleaseInfo('callstackincubator', 'fabrice-ai') +const releaseTarballUrl = await latestReleaseDownloadLink('callstackincubator', 'fabrice-ai') -s.start(`Downloading template (${lastReleaseInfo.tag_name})...`) +s.start(`Downloading template...`) -await downloadAndExtractTemplate(root, lastReleaseInfo.tarball_url, template.files) +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 6e1972d..c45a778 100644 --- a/packages/create-fabrice-ai/src/utils.ts +++ b/packages/create-fabrice-ai/src/utils.ts @@ -9,52 +9,23 @@ export function formatTargetDir(targetDir: string) { return targetDir.trim().replace(/\/+$/g, '') } -export interface GithubReleaseInfo { - url: string - assets_url: string - upload_url: string - html_url: string - id: number - author: { - login: string - id: number - avatar_url: string - gravatar_id: string - url: string - repos_url: string - events_url: string - type: string - } - node_id: string - tag_name: string - target_commitish: string - name: string - draft: boolean - prerelease: boolean - created_at: string - published_at: string - assets: any[] - tarball_url: string - zipball_url: string -} - -export async function getLatestReleaseInfo( +export async function latestReleaseDownloadLink( organization: string, repo: string -): Promise { +): 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}.`) } - - return await response.json() -} - -export async function latestReleaseDownloadLink(organization: string, repo: string) { - const latestRelease = await getLatestReleaseInfo(organization, repo) - return latestRelease.tarball_url + + const body = await response.json() + if (!('tarball_url' in body) || typeof body.tarball_url !== 'string') { + throw new Error() + } + return body.tarball_url } export async function downloadAndExtractTemplate(root: string, tarball: string, files: string[]) { From b7613f231d62b50531165f590cfddd513a15b0f1 Mon Sep 17 00:00:00 2001 From: Mike Grabowski Date: Fri, 13 Dec 2024 22:02:19 +0300 Subject: [PATCH 3/3] chore --- packages/create-fabrice-ai/src/utils.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/create-fabrice-ai/src/utils.ts b/packages/create-fabrice-ai/src/utils.ts index c45a778..9541411 100644 --- a/packages/create-fabrice-ai/src/utils.ts +++ b/packages/create-fabrice-ai/src/utils.ts @@ -16,15 +16,16 @@ export async function latestReleaseDownloadLink( 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() + throw new Error(`Failed to get tarball url from ${response.url}.`) } + return body.tarball_url } @@ -38,7 +39,7 @@ export async function downloadAndExtractTemplate(root: string, tarball: string, // @ts-ignore Readable.fromWeb(response.body), extract({ - cwd: tmpdir(), + cwd: path.join(import.meta.dirname), strip: 1, }), ])