rsnext/packages/create-next-app/helpers/copy.ts
Sukka 127e30ed42
refactor(cna): make create-next-app even smaller (#53241)
The PR follows #53146 and #53115.

The PR does 3 things:

- Replaces a [very heavy dependency `cpy`](https://github.com/vercel/next.js/pull/53146#issuecomment-1649193789) with a more lightweight copy helper.
  - The `fs.cp(src, dest, {recursive: true})` API is not used, as it is still experimental:
  <img width="1630" alt="image" src="https://github.com/vercel/next.js/assets/40715044/c61a454a-3a96-4658-a389-fbb68c241f18">
- Update `cross-spawn` to the latest version `7.0.3`
  - The only breaking change introduced in `cross-spawn@7.x` is dropping Node.js 8 supports, which allows `cross-spawn` to drop a dependency. Since `create-next-app` requires Node.js 16.8.0, I assume bumping `cross-spawn` would be safe.
- Update `fast-glob` to the latest version `3.3.1` to remove more KiBs (pointed out by @imranbarbhuiya)
  - The breaking change introduced in `fast-glob@3.x` is dropping Node.js 8 supports and some options changes.

Together the PR removes another 202 KiB from the `create-next-app/dist/index.js`. The size of `create-next-app/dist/index.js` is now 616 KiB.

<img width="583" alt="image" src="https://github.com/vercel/next.js/assets/40715044/4deb5e36-a63b-4501-b67c-29ea06e30578">
2023-07-28 17:21:58 +00:00

50 lines
1.2 KiB
TypeScript

/* 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('`src` and `dest` are required')
}
const sourceFiles = await glob(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)
// Ensure the destination directory exists
await fs.promises.mkdir(path.dirname(to), { recursive: true })
return fs.promises.copyFile(from, to)
})
)
}