Skip to content

Commit

Permalink
feat(create-fabrice-ai): downloading latest release (#116)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
pkarw and grabbou authored Dec 13, 2024
1 parent 85abe0b commit 2a3e7cc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
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',
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,
}),
])

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

0 comments on commit 2a3e7cc

Please sign in to comment.