Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(create-fabrice-ai): downloading latest release #116

Merged
merged 3 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions packages/create-fabrice-ai/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
copyAdditionalTemplateFiles,
downloadAndExtractTemplate,
formatTargetDir,
getLatestReleaseInfo,
isNodeError,
} from './utils.js'

Expand Down Expand Up @@ -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',
grabbou marked this conversation as resolved.
Show resolved Hide resolved
template.files
)
s.start(`Downloading template (${lastReleaseInfo.tag_name})...`)

await downloadAndExtractTemplate(root, lastReleaseInfo.tarball_url, template.files)

copyAdditionalTemplateFiles(root)

Expand Down
59 changes: 52 additions & 7 deletions packages/create-fabrice-ai/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<GithubReleaseInfo> {
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()
pkarw marked this conversation as resolved.
Show resolved Hide resolved
}

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) {
Expand All @@ -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,
}),
pkarw marked this conversation as resolved.
Show resolved Hide resolved
])

const filesToCopy = [...files, 'package.json']
Expand Down