Skip to content

Commit

Permalink
refactor(vite-node): Replace minimist with cac
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinerey committed May 6, 2022
1 parent 70bc30b commit a9f58f4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 59 deletions.
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
81 changes: 28 additions & 53 deletions packages/vite-node/src/cli.ts
@@ -1,71 +1,46 @@
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"')
.option('-s, --silent', 'Do not emit errors and logs')
.option('--vue', 'Support for importing Vue component')
.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
// silent?: boolean
// vue?: 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.

0 comments on commit a9f58f4

Please sign in to comment.