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 all commits
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 @@ -13,6 +13,7 @@ import {
downloadAndExtractTemplate,
formatTargetDir,
isNodeError,
latestReleaseDownloadLink,
} from './utils.js'

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

await downloadAndExtractTemplate(root, releaseTarballUrl, template.files)

copyAdditionalTemplateFiles(root)

Expand Down
31 changes: 24 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,26 @@ export function formatTargetDir(targetDir: string) {
return targetDir.trim().replace(/\/+$/g, '')
}

export async function latestReleaseDownloadLink(
organization: string,
repo: string
): Promise<string> {
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) {
Expand All @@ -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,
}),
pkarw marked this conversation as resolved.
Show resolved Hide resolved
])

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