Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(vite-node): Replace minimist with cac #1249

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/vite-node/package.json
Expand Up @@ -64,13 +64,12 @@
},
"dependencies": {
"kolorist": "^1.5.1",
"minimist": "^1.2.6",
"mlly": "^0.5.2",
"pathe": "^0.2.0",
"vite": "^2.9.7"
},
"devDependencies": {
"@types/minimist": "^1.2.2",
"cac": "^6.7.12",
"rollup": "^2.71.1"
},
"engines": {
Expand Down
77 changes: 24 additions & 53 deletions packages/vite-node/src/cli.ts
@@ -1,71 +1,42 @@
import minimist from 'minimist'
import cac from 'cac'
import { dim, red } from 'kolorist'
import { createServer } from 'vite'
import { version } from '../package.json'
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)
}
const cli = cac('vite-node')

// forward argv
process.argv = [...process.argv.slice(0, 2), ...(argv['--'] || [])]
cli
.version(version)
.option('-r, --root <path>', 'Use specified root directory')
.option('-c, --config <path>', 'Use specified config file')
.option('-w, --watch', 'Restart on file changes, similar to "nodemon"')
.help()

run(argv)
cli
.command('[...files]')
.action(run)

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
`)
}
cli.parse()

export interface CliOptions {
files?: string[]
_?: string[]
root?: string
config?: string
watch?: boolean
'--'?: string[]
}

async function run(options: CliOptions = {}) {
const files = options.files || options._ || []
async function run(files: string[], options: CliOptions = {}) {
if (!files.length) {
console.error(red('No files specified.'))
cli.outputHelp()
process.exit(1)
}

// forward argv
process.argv = [...process.argv.slice(0, 2), ...(options['--'] || [])]

const server = await createServer({
logLevel: 'error',
configFile: options.config,
Expand Down
7 changes: 3 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.