From cb306fa91191ea64aa1f7ebc3757a72662c5a6bc Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Sun, 27 Dec 2020 01:55:02 +0800 Subject: [PATCH] refactor: use ts for cli (#191) --- bin/vitepress.js | 44 +------------------------------------------- src/node/cli.ts | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 43 deletions(-) create mode 100644 src/node/cli.ts diff --git a/bin/vitepress.js b/bin/vitepress.js index a5b135eb..0aafe1a3 100755 --- a/bin/vitepress.js +++ b/bin/vitepress.js @@ -1,44 +1,2 @@ #!/usr/bin/env node -const chalk = require('chalk') -const argv = require('minimist')(process.argv.slice(2)) - -console.log(chalk.cyan(`vitepress v${require('../package.json').version}`)) -console.log(chalk.cyan(`vite v${require('vite/package.json').version}`)) - -const command = argv._[0] -const root = argv._[command ? 1 : 0] -if (root) { - argv.root = root -} - -if (!command || command === 'dev') { - const port = argv.port || 3000 - require('../dist/node') - .createServer(argv) - .then((server) => { - server.listen(port, () => { - console.log(`listening at http://localhost:${port}`) - }) - }) - .catch((err) => { - console.error(chalk.red(`failed to start server. error:\n`), err) - process.exit(1) - }) -} else if (command === 'build') { - require('../dist/node') - .build(argv) - .catch((err) => { - console.error(chalk.red(`build error:\n`), err) - process.exit(1) - }) -} else if (command === 'serve') { - require('../dist/node') - .serve(argv) - .catch((err) => { - console.error(chalk.red(`failed to start server. error:\n`), err) - process.exit(1) - }) -} else { - console.log(chalk.red(`unknown command "${command}".`)) - process.exit(1) -} +require('../dist/node/cli') diff --git a/src/node/cli.ts b/src/node/cli.ts new file mode 100644 index 00000000..0f806083 --- /dev/null +++ b/src/node/cli.ts @@ -0,0 +1,41 @@ +import chalk from 'chalk' +import minimist from 'minimist' +import { createServer, build, serve } from '.' + +const argv: any = minimist(process.argv.slice(2)) + +console.log(chalk.cyan(`vitepress v${require('../../package.json').version}`)) +console.log(chalk.cyan(`vite v${require('vite/package.json').version}`)) + +const command = argv._[0] +const root = argv._[command ? 1 : 0] +if (root) { + argv.root = root +} + +if (!command || command === 'dev') { + const port = argv.port || 3000 + createServer(argv) + .then((server) => { + server.listen(port, () => { + console.log(`listening at http://localhost:${port}`) + }) + }) + .catch((err) => { + console.error(chalk.red(`failed to start server. error:\n`), err) + process.exit(1) + }) +} else if (command === 'build') { + build(argv).catch((err) => { + console.error(chalk.red(`build error:\n`), err) + process.exit(1) + }) +} else if (command === 'serve') { + serve(argv).catch((err) => { + console.error(chalk.red(`failed to start server. error:\n`), err) + process.exit(1) + }) +} else { + console.log(chalk.red(`unknown command "${command}".`)) + process.exit(1) +}