Files
next.js/scripts/patch-next.cjs
T
hrmnyandGitHub 114c29513e chore: port more nextpack scripts (#68586)
### What?

Ports a few more useful scripts from nextpack, namely:

- `patch-next`
- `sweep`
- `build-native` (to clean up incremental artifacts on compiler panics)
- the macOS compression agent
2024-08-07 18:55:20 +00:00

80 lines
1.7 KiB
JavaScript

#!/usr/bin/env node
const {
NEXT_DIR,
exec,
logCommand,
booleanArg,
packageFiles,
} = require('./pack-util.cjs')
const fs = require('fs')
const path = require('path')
/** @type {string[]} */
const argv = process.argv
const args = argv.slice(2)
const NEXT_PACKAGES = `${NEXT_DIR}/packages`
const PROJECT_DIR = path.resolve(args[0])
const noBuild = booleanArg(args, '--no-build')
const noNativeBuild = booleanArg(args, '--no-native-build')
async function execFn(title, fn) {
logCommand(title, fn.toString())
return fn()
}
function realPathIfAny(path) {
try {
return fs.realpathSync(path)
} catch {
return null
}
}
async function copy(src, dst) {
const realDst = realPathIfAny(dst)
if (!realDst) {
return
}
const files = await packageFiles(src)
for (const file of files) {
fs.cpSync(path.join(src, file), path.join(realDst, file), {
recursive: true,
})
}
}
;(async () => {
if (!noBuild) {
exec('Install Next.js build dependencies', 'pnpm i')
exec('Build Next.js', 'pnpm run build')
}
if (!noNativeBuild) {
process.argv = [...process.argv.slice(0, 2), ...args.slice(1)]
await require('./build-native.cjs')
}
await execFn('Patching next', () =>
copy(`${NEXT_PACKAGES}/next`, `${PROJECT_DIR}/node_modules/next`)
)
await execFn('Patching @next/swc', () =>
copy(`${NEXT_PACKAGES}/next-swc`, `${PROJECT_DIR}/node_modules/@next/swc`)
)
await execFn('Patching @next/mdx', () =>
copy(`${NEXT_PACKAGES}/next-mdx`, `${PROJECT_DIR}/node_modules/@next/mdx`)
)
await execFn('Patching @next/bundle-analyzer', () =>
copy(
`${NEXT_PACKAGES}/next-bundle-analyzer`,
`${PROJECT_DIR}/node_modules/@next/bundle-anlyzer`
)
)
})()