Skip to content

Commit

Permalink
tweak cli
Browse files Browse the repository at this point in the history
  • Loading branch information
HcySunYang committed Oct 14, 2018
1 parent 9510b42 commit fbf03b7
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 28 deletions.
26 changes: 9 additions & 17 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
const cac = require('cac')
const JoyCon = require('joycon')
const majo = require('majo')
const path = require('path')
const logger = require('log-horizon').create()

const cli = cac()
const joycon = new JoyCon({
Expand All @@ -20,37 +18,31 @@ joycon.addLoader({

async function getConfig (flags) {
const { path, data } = await joycon.load(['vuese.config.js', '.vueserc', 'package.json'])
const config = {}
const config = {
include: '**/*.vue',
exclude: [],
outDir: 'website',
markdownDir: 'components'
}
if (path) Object.assign(config, data, flags)
return config
}

cli.command('*', 'vuese cli', async (input, flags) => {
const config = await getConfig(flags)
require('../lib')(config)
cli.showHelp()
})

cli.command('gen', 'Generate target resources', async (input, flags) => {
const config = await getConfig(flags)
const questions = require('./questions')
const answser = await require('inquirer').prompt(questions)
if (answser.genType === 'docute') require('../lib/genDocute')(config)
if (answser.genType === 'markdown') require('../lib')(config)
})

cli.command('serve', 'Serve generated docute website', async (input, flags) => {
const config = await getConfig(flags)
const servePath = path.resolve(config.outDir)
const http = require('http')
const handler = require('serve-handler')
const server = http.createServer((req, res) => {
return handler(req, res, {
public: path.resolve(config.outDir)
})
})

server.listen('5000', '127.0.0.1',() => {
logger.success(`Server running at http://127.0.0.1:5000/`);
})
require('../lib/server')(config)
})

cli.parse()
6 changes: 1 addition & 5 deletions lib/genDocute.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@ const lib = require('./index')
const logger = require('log-horizon').create()

module.exports = async (config) => {
const {
outDir = 'website'
} = config

try {
const componentNamesPromise = await lib(config)
const componentNames = await Promise.all(componentNamesPromise)
logger.progress('Start generating...')
await sao({
template: path.resolve(__dirname, './templates/docute'),
targetPath: path.resolve(outDir),
targetPath: path.resolve(config.outDir),
configOptions: {
componentNames,
markdownDir: config.markdownDir
Expand Down
14 changes: 8 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ const path = require('path')
const majo = require('majo')
const logger = require('log-horizon').create()

module.exports = async ({
include = '**/*.vue',
exclude = [],
outDir = 'website',
markdownDir = 'components'
} = config) => {
module.exports = async (config) => {
logger.progress('Start creating markdown files...')

let {
include,
exclude,
outDir,
markdownDir
} = config

if (typeof include === 'string') include = [include]
if (typeof exclude === 'string') exclude = [exclude]
exclude = exclude.concat('node_modules/**/*.vue')
Expand Down
19 changes: 19 additions & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const path = require('path')
const getProt = require('get-port')
const logger = require('log-horizon').create()

module.exports = async (config) => {
const servePath = path.resolve(config.outDir)
const http = require('http')
const handler = require('serve-handler')
const server = http.createServer((req, res) => {
return handler(req, res, {
public: path.resolve(config.outDir)
})
})
const port = await getProt({ prot: 5000 })

server.listen(port, '127.0.0.1',() => {
logger.success(`Server running at http://127.0.0.1:${port}/`);
})
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@vue/component-compiler-utils": "^2.2.0",
"cac": "^5.0.13",
"fast-glob": "^2.2.3",
"get-port": "^4.0.0",
"inquirer": "^6.2.0",
"joycon": "^2.1.2",
"log-horizon": "^0.1.2",
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2525,6 +2525,10 @@ get-own-enumerable-property-symbols@^2.0.1:
version "2.0.1"
resolved "https://registry.npmjs.org/get-own-enumerable-property-symbols/-/get-own-enumerable-property-symbols-2.0.1.tgz#5c4ad87f2834c4b9b4e84549dc1e0650fb38c24b"

get-port@^4.0.0:
version "4.0.0"
resolved "https://registry.npmjs.org/get-port/-/get-port-4.0.0.tgz#373c85960138ee20027c070e3cb08019fea29816"

get-proxy@^2.0.0:
version "2.1.0"
resolved "https://registry.npmjs.org/get-proxy/-/get-proxy-2.1.0.tgz#349f2b4d91d44c4d4d4e9cba2ad90143fac5ef93"
Expand Down

0 comments on commit fbf03b7

Please sign in to comment.