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

refactor(cna): make create-next-app even smaller #53241

Merged
merged 7 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
50 changes: 50 additions & 0 deletions packages/create-next-app/helpers/copy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* eslint-disable import/no-extraneous-dependencies */
import { async as glob } from 'fast-glob'
import path from 'path'
import fs from 'fs'

interface CopyOption {
cwd?: string
rename?: (basename: string) => string
parents?: boolean
}

const identity = (x: string) => x

export const copy = async (
src: string | string[],
dest: string,
{ cwd, rename = identity, parents = true }: CopyOption = {}
) => {
const source = typeof src === 'string' ? [src] : src

if (source.length === 0 || !dest) {
throw new TypeError('`files` and `destination` are required')
SukkaW marked this conversation as resolved.
Show resolved Hide resolved
}

const sourceFiles = await glob<string>(source, {
cwd,
dot: true,
absolute: false,
stats: false,
})

const destRelativeToCwd = cwd ? path.resolve(cwd, dest) : dest

return Promise.all(
sourceFiles.map(async (p) => {
const dirname = path.dirname(p)
const basename = rename(path.basename(p))

const from = cwd ? path.resolve(cwd, p) : p
const to = parents
? path.join(destRelativeToCwd, dirname, basename)
: path.join(destRelativeToCwd, basename)

// This ensure the destination directory exists
SukkaW marked this conversation as resolved.
Show resolved Hide resolved
await fs.promises.mkdir(path.dirname(to), { recursive: true })

return fs.promises.copyFile(from, to)
})
)
}
3 changes: 1 addition & 2 deletions packages/create-next-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@
"ci-info": "watson/ci-info#f43f6a1cefff47fb361c88cf4b943fdbcaafe540",
"commander": "2.20.0",
"conf": "10.2.0",
"cpy": "7.3.0",
"cross-spawn": "6.0.5",
"cross-spawn": "7.0.3",
"fast-glob": "2.2.7",
SukkaW marked this conversation as resolved.
Show resolved Hide resolved
"got": "10.7.0",
"picocolors": "1.0.0",
Expand Down
16 changes: 6 additions & 10 deletions packages/create-next-app/templates/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { install } from '../helpers/install'
import { makeDir } from '../helpers/make-dir'
import { copy } from '../helpers/copy'

import cpy from 'cpy'
import { async as glob } from 'fast-glob'
import os from 'os'
import fs from 'fs'
Expand Down Expand Up @@ -50,14 +50,14 @@ export const installTemplate = async ({
if (!eslint) copySource.push('!eslintrc.json')
if (!tailwind) copySource.push('!tailwind.config.js', '!postcss.config.js')

await cpy(copySource, root, {
await copy(copySource, root, {
parents: true,
cwd: templatePath,
rename: (name) => {
rename(name) {
switch (name) {
case 'gitignore':
case 'eslintrc.json': {
return '.'.concat(name)
return `.${name}`
}
// README.md is ignored by webpack-asset-relocator-loader used by ncc:
// https://github.com/vercel/webpack-asset-relocator-loader/blob/e9308683d47ff507253e37c9bcbb99474603192b/src/asset-relocator.js#L227
Expand Down Expand Up @@ -160,7 +160,7 @@ export const installTemplate = async ({
}

/**
* Create a package.json for the new project.
* Create a package.json for the new project and write it to disk.
*/
const packageJson = {
name: appName,
Expand All @@ -173,11 +173,7 @@ export const installTemplate = async ({
lint: 'next lint',
},
}

/**
* Write it to disk.
*/
fs.writeFileSync(
await fs.promises.writeFile(
path.join(root, 'package.json'),
JSON.stringify(packageJson, null, 2) + os.EOL
)
Expand Down
61 changes: 6 additions & 55 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.