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

fix(cli): do not throw error when extracting examples in Node 18+ #40182

Merged
merged 7 commits into from Sep 5, 2022
50 changes: 33 additions & 17 deletions packages/create-next-app/helpers/examples.ts
Expand Up @@ -3,6 +3,9 @@ import got from 'got'
import tar from 'tar'
import { Stream } from 'stream'
import { promisify } from 'util'
import { join } from 'path'
import { tmpdir } from 'os'
import { createWriteStream } from 'fs'

const pipeline = promisify(Stream.pipeline)

Expand Down Expand Up @@ -79,31 +82,44 @@ export function existsInRepo(nameOrUrl: string): Promise<boolean> {
}
}

export function downloadAndExtractRepo(
async function downloadTar(url: string) {
const tempFile = join(tmpdir(), `next.js-cra-example.temp-${Date.now()}`)
await pipeline(got.stream(url), createWriteStream(tempFile))
return tempFile
}

export async function downloadAndExtractRepo(
root: string,
{ username, name, branch, filePath }: RepoInfo
): Promise<void> {
return pipeline(
got.stream(
`https://codeload.github.com/${username}/${name}/tar.gz/${branch}`
),
tar.extract(
{ cwd: root, strip: filePath ? filePath.split('/').length + 1 : 1 },
[`${name}-${branch.replace(/\//g, '-')}${filePath ? `/${filePath}` : ''}`]
)
) {
const tempFile = await downloadTar(
`https://codeload.github.com/${username}/${name}/tar.gz/${branch}`
)

await tar.x({
file: tempFile,
cwd: root,
strip: filePath ? filePath.split('/').length + 1 : 1,
filter: (p) =>
p.startsWith(
`${name}-${branch.replace(/\//g, '-')}${filePath ? `/${filePath}` : ''}`
),
})
}

export function downloadAndExtractExample(
root: string,
name: string
): Promise<void> {
export async function downloadAndExtractExample(root: string, name: string) {
if (name === '__internal-testing-retry') {
throw new Error('This is an internal example for testing the CLI.')
}

return pipeline(
got.stream('https://codeload.github.com/vercel/next.js/tar.gz/canary'),
tar.extract({ cwd: root, strip: 3 }, [`next.js-canary/examples/${name}`])
const tempFile = await downloadTar(
'https://codeload.github.com/vercel/next.js/tar.gz/canary'
)

await tar.x({
file: tempFile,
cwd: root,
strip: 3,
filter: (p) => p.includes(`next.js-canary/examples/${name}`),
})
balazsorban44 marked this conversation as resolved.
Show resolved Hide resolved
}