Skip to content

Commit

Permalink
Generate documentation and add loggers
Browse files Browse the repository at this point in the history
  • Loading branch information
HcySunYang committed Oct 14, 2018
1 parent 46afa88 commit 9510b42
Show file tree
Hide file tree
Showing 10 changed files with 1,012 additions and 36 deletions.
3 changes: 2 additions & 1 deletion .vueserc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"include": ["**/*.vue"],
"exclude": [],
"outDir": "test-website"
"outDir": "test-website",
"markdownDir": "components"
}
34 changes: 32 additions & 2 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
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 @@ -16,11 +18,39 @@ joycon.addLoader({
}
})

cli.command('*', 'vuese cli', async (input, flags) => {
async function getConfig (flags) {
const { path, data } = await joycon.load(['vuese.config.js', '.vueserc', 'package.json'])
const config = {}
if (path) Object.assign(config, data, flags)
require('./lib')(config)
return config
}

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

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)
})

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/`);
})
})

cli.parse()
18 changes: 18 additions & 0 deletions bin/questions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = [
{
type: 'list',
name: 'genType',
message: 'Select the target to generate',
default: 'docute',
choices: [
{
name: 'Docute - The fastest way to create a documentation site for your project.',
value: 'docute'
},
{
name: 'Only markdown.',
value: 'markdown'
}
]
}
]
28 changes: 28 additions & 0 deletions lib/genDocute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const sao = require('sao')
const path = require('path')
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),
configOptions: {
componentNames,
markdownDir: config.markdownDir
}
})
logger.success('Generated successfully')
} catch (err) {
console.error(err.name === 'SAOError' ? err.message : err.stack)
process.exit(1)
}
}
16 changes: 11 additions & 5 deletions bin/lib.js → lib/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
const fg = require('fast-glob')
const path = require('path')
const majo = require('majo')
const logger = require('log-horizon').create()

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

if (typeof include === 'string') include = [include]
if (typeof exclude === 'string') exclude = [exclude]
exclude = exclude.concat('node_modules/**/*.vue')
Expand All @@ -17,7 +21,7 @@ module.exports = async ({
const htmlCommentRE = /<!--\s*@vuese:(\w+):(\w+):start\s*-->[^]*<!--\s*@vuese:\1:\2:end\s*-->/

const { parser, Render } = require('../dist/vuese')
files.forEach(async p => {
return files.map(async p => {
const abs = path.resolve(p)
const source = await majo.fs.readFile(abs, 'utf-8')
const parserRes = parser(source)
Expand Down Expand Up @@ -55,9 +59,11 @@ module.exports = async ({
stream = stream.slice(index)
}

const targetDir = path.resolve(outDir)
const target = path.resolve(outDir, compName + '.md')
const targetDir = path.resolve(outDir, markdownDir)
const target = path.resolve(targetDir, compName + '.md')
await majo.fs.ensureDir(targetDir)
majo.fs.writeFile(target, str)
await majo.fs.writeFile(target, str)
logger.success(`Successfully created: ${target}`)
return compName
})
}
18 changes: 18 additions & 0 deletions lib/templates/docute/sao.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = (options) => {
return {
prompts: {
title: {
type: 'input',
message: 'Sidbar title:',
default: 'Components'
}
},
data (answers) {
return {
title: answers.title,
componentNames: options.componentNames,
markdownDir: options.markdownDir
}
}
}
}
31 changes: 31 additions & 0 deletions lib/templates/docute/template/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>My Docs</title>
<link rel="stylesheet" href="https://unpkg.com/docute@4/dist/docute.css">
</head>
<body>
<div id="docute"></div>
<script src="https://unpkg.com/docute@4/dist/docute.js"></script>
<script>
new Docute({
target: '#docute',
sidebar: [
{
title: '<%= title %>',
links: [
<% for (var i = 0; i < componentNames.length; i++) { %>
{
title: '<%= componentNames[i] %>',
link: '/<%= markdownDir %>/<%= componentNames[i] %>'
}<% if (i !== componentNames.length - 1) { %>,<% } %>
<% } %>
]
}
]
})
</script>
</body>
</html>
File renamed without changes.
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"types": "dist/index.d.ts",
"files": [
"dist",
"bin"
"bin",
"lib"
],
"keywords": [
"vue",
Expand Down Expand Up @@ -36,8 +37,12 @@
"@vue/component-compiler-utils": "^2.2.0",
"cac": "^5.0.13",
"fast-glob": "^2.2.3",
"inquirer": "^6.2.0",
"joycon": "^2.1.2",
"log-horizon": "^0.1.2",
"majo": "^0.6.2",
"sao": "^0.22.17",
"serve-handler": "^5.0.5",
"vue-template-compiler": "^2.5.17"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit 9510b42

Please sign in to comment.