rsnext/scripts/setup-wasm.mjs
Sukka 27dcd26c43
chore: replace fs-extra usage in scripts/ (#57215)
The PR is the continuation of #56917 and #57030.

The PR replaces `fs-extra#copy` with Node.js built-in `fs.cp` API (which is almost identical to `fs-extra#copy`, and has been available since Node.js 16). The PR also provides a workaround for flaky Windows `fs.rename` operation (#57030).

cc @styfle @wbinnssmith
2023-11-15 13:34:18 +00:00

32 lines
869 B
JavaScript

import path from 'path'
import fs from 'fs'
;(async function () {
try {
let wasmDir = path.join(process.cwd(), 'packages/next-swc/crates/wasm')
let wasmTarget = 'nodejs'
// CI restores artifact at pkg-${wasmTarget}
// This only runs locally
let folderName = fs.existsSync(path.join(wasmDir, 'pkg'))
? 'pkg'
: `pkg-${wasmTarget}`
let wasmPkg = JSON.parse(
fs.readFileSync(path.join(wasmDir, `${folderName}/package.json`))
)
wasmPkg.name = `@next/swc-wasm-${wasmTarget}`
fs.writeFileSync(
path.join(wasmDir, `${folderName}/package.json`),
JSON.stringify(wasmPkg, null, 2)
)
fs.cpSync(
path.join(wasmDir, folderName),
path.join(process.cwd(), `node_modules/@next/swc-wasm-${wasmTarget}`),
{ force: true, recursive: true }
)
} catch (e) {
console.error(e)
}
})()