Skip to content
Open
Changes from all 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
29 changes: 23 additions & 6 deletions packages/payload/bin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env node

import { spawnSync } from 'node:child_process'
import { createRequire } from 'node:module'
import path from 'node:path'
import { fileURLToPath, pathToFileURL } from 'node:url'

Expand All @@ -17,20 +19,35 @@ if (disableTranspile) {

void start()
} else {
const require = createRequire(import.meta.url)
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
const url = pathToFileURL(dirname).toString() + '/'

if (!useSwc) {
const start = async () => {
// Use tsx
let tsImport = (await import('tsx/esm/api')).tsImport
const binPath = path.resolve(dirname, 'dist/bin/index.js')
const binURL = pathToFileURL(binPath).href
const tsxLoader = require.resolve('tsx')
const script = `
process.argv = [process.argv[0], ${JSON.stringify(binPath)}, ...process.argv.slice(1)]
import(${JSON.stringify(binURL)})
.then(({ bin }) => bin())
.catch((error) => {
console.error(error)
process.exit(1)
})
`
const result = spawnSync(process.execPath, ['--import', tsxLoader, '--eval', script, ...process.argv.slice(2)], {
cwd: process.cwd(),
env: process.env,
stdio: 'inherit',
})

const { bin } = await tsImport('./dist/bin/index.js', url)
await bin()
if (result.error) {
throw result.error
}

void start()
process.exit(result.status ?? 1)
} else if (useSwc) {
const { register } = await import('node:module')
// Remove --use-swc from arguments
Expand Down
Loading