-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
cli.ts
97 lines (82 loc) · 2.16 KB
/
cli.ts
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
import minimist from 'minimist'
import { dim, red } from 'kolorist'
import { createServer } from 'vite'
import { ViteNodeServer } from './server'
import { ViteNodeRunner } from './client'
const argv = minimist(process.argv.slice(2), {
'alias': {
r: 'root',
c: 'config',
h: 'help',
w: 'watch',
s: 'silent',
},
'--': true,
'string': ['root', 'config'],
'boolean': ['help', 'watch', 'silent'],
unknown(name: string) {
if (name[0] === '-') {
console.error(red(`Unknown argument: ${name}`))
help()
process.exit(1)
}
return true
},
})
if (argv.help) {
help()
process.exit(0)
}
if (!argv._.length) {
console.error(red('No files specified.'))
help()
process.exit(1)
}
// forward argv
process.argv = [...process.argv.slice(0, 2), ...(argv['--'] || [])]
run(argv)
function help() {
// eslint-disable-next-line no-console
console.log(`
Usage:
$ vite-node [options] [files]
Options:
-r, --root <path> ${dim('[string]')} use specified root directory
-c, --config <file> ${dim('[string]')} use specified config file
-w, --watch ${dim('[boolean]')} restart on file changes, similar to "nodemon"
-s, --silent ${dim('[boolean]')} do not emit errors and logs
--vue ${dim('[boolean]')} support for importing Vue component
`)
}
export interface CliOptions {
files?: string[]
_?: string[]
root?: string
config?: string
}
async function run(options: CliOptions = {}) {
const files = options.files || options._ || []
const server = await createServer({
logLevel: 'error',
clearScreen: false,
configFile: options.config,
root: options.root,
})
await server.pluginContainer.buildStart({})
const node = new ViteNodeServer(server)
const runner = new ViteNodeRunner({
root: server.config.root,
base: server.config.base,
fetchModule(id) {
return node.fetchModule(id)
},
resolveId(id, importer) {
return node.resolveId(id, importer)
},
})
// provide the vite define variable in this context
await runner.executeId('/@vite/env')
for (const file of files)
await runner.executeFile(file)
await server.close()
}