-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathmain.ts
More file actions
109 lines (97 loc) · 3.38 KB
/
Copy pathmain.ts
File metadata and controls
109 lines (97 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import type { CommandDef } from 'citty'
import nodeCrypto from 'node:crypto'
import { builtinModules, createRequire } from 'node:module'
import { resolve } from 'node:path'
import process from 'node:process'
import { runMain as _runMain, defineCommand } from 'citty'
import { colors } from 'consola/utils'
import { provider } from 'std-env'
import { description, name, version } from '../package.json'
import { commands } from './commands'
import { cwdArgs } from './commands/_shared'
import { runCommand } from './run'
import { setupGlobalConsole } from './utils/console'
import { checkEngines } from './utils/engines'
import { debug, logger } from './utils/logger'
// globalThis.crypto support for Node.js 18
if (!globalThis.crypto) {
globalThis.crypto = nodeCrypto.webcrypto as unknown as Crypto
}
// Node.js below v22.3.0, v20.16.0
if (!process.getBuiltinModule) {
const _require = createRequire(import.meta.url)
// @ts-expect-error we are overriding with inferior types
process.getBuiltinModule = (name: string) => {
if (name.startsWith('node:') || builtinModules.includes(name)) {
return _require.resolve(name)
}
}
}
const _main = defineCommand({
meta: {
name: name.endsWith('nightly') ? name : 'nuxi',
version,
description,
},
args: {
...cwdArgs,
command: {
type: 'positional',
required: false,
},
},
subCommands: commands,
async setup(ctx) {
const command = ctx.args._[0]
setupGlobalConsole({ dev: command === 'dev' })
debug(`Running \`nuxt ${command}\` command`)
// Check Node.js version and CLI updates in background
let backgroundTasks: Promise<any> | undefined
if (command !== '_dev' && provider !== 'stackblitz') {
backgroundTasks = Promise.all([
checkEngines(),
]).catch(err => logger.error(String(err)))
}
// Avoid background check to fix prompt issues
if (command === 'init') {
await backgroundTasks
}
if (command === 'add' && ctx.rawArgs[1]) {
logger.warn(`${colors.yellow('Deprecated:')} Using ${colors.cyan('nuxt add <template> <name>')} is deprecated.`)
logger.info(`Please use ${colors.cyan('nuxt add-template <template> <name>')} instead.`)
const addTemplate = await import('./commands/add-template').then(m => m.default || m)
await runCommand(addTemplate, [...ctx.rawArgs.slice(1)]).catch((err) => {
console.error(err.message)
process.exit(1)
})
process.exit(0)
}
// allow running arbitrary commands if there's a locally registered binary with `nuxt-` prefix
if (ctx.args.command && !(ctx.args.command in commands)) {
const cwd = resolve(ctx.args.cwd)
try {
const { x } = await import('tinyexec')
// `tinyexec` will resolve command from local binaries
await x(`nuxt-${ctx.args.command}`, ctx.rawArgs.slice(1), {
nodeOptions: { stdio: 'inherit', cwd },
throwOnError: true,
})
}
catch (err) {
// TODO: use windows err code as well
if (err instanceof Error && 'code' in err && err.code === 'ENOENT') {
return
}
}
process.exit()
}
},
})
export const main = _main as CommandDef<any>
export async function runMain(): Promise<void> {
if (process.argv[2] === 'complete') {
const { initCompletions } = await import('./completions')
await initCompletions(main)
}
return _runMain(main)
}