Skip to content

Commit d10933f

Browse files
authoredMar 14, 2017
Merge pull request #7 from jrappen/master
use chalk, y18n, yargonaut, yargs & remove commander
2 parents f8d1681 + 6b6aa6a commit d10933f

18 files changed

+251
-129
lines changed
 

Diff for: ‎.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.git/

Diff for: ‎.eslintrc

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
{
2-
"extends": ["vue"]
2+
"extends": [
3+
"vue"
4+
],
5+
"rules": {
6+
"no-unused-vars": "off"
7+
}
38
}

Diff for: ‎.npmignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.eslintignore
2+
.eslintrc
3+
.gitignore
4+
.travis.yml

Diff for: ‎.travis.yml

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
sudo: false
22
language: node_js
33
node_js: stable
4+
os:
5+
- linux
6+
- osx
7+
install:
8+
- npm install --no-optional

Diff for: ‎bin/docsify

+78-49
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,86 @@
11
#!/usr/bin/env node
22

3-
var program = require('commander')
4-
var updateNotifier = require('update-notifier')
5-
var run = require('../src')
6-
var pkg = require('../package.json')
3+
'use strict'
4+
5+
const pkg = require('../package.json')
6+
const run = require('../lib')
7+
const chalk = require('chalk')
8+
const updateNotifier = require('update-notifier')
79

810
updateNotifier({ pkg: pkg }).notify()
911

10-
program
11-
.version(require('../package.json').version)
12+
const Locales = require('./locales')
13+
const y18n = new Locales()
1214

13-
program
14-
.usage('init [options] <relative path>')
15-
.command('init [path .]')
16-
.alias('i')
17-
.description('create new docs')
18-
.option('-l, --local', 'copy docsify to local (default: false)', false)
19-
.option('-t, --theme [vue|buble|dark|pure]', 'theme file (default: vue)', 'vue')
20-
.on('--help', function () {
21-
console.log('\tExamples:\n')
22-
console.log('\t\tdocsify i')
23-
console.log('\t\tdocsify init')
24-
console.log('\t\tdocsify init -l')
25-
console.log('\t\tdocsify init --local')
26-
console.log('\t\tdocsify init -t buble')
27-
console.log('\t\tdocsify init -t dark')
28-
console.log('\t\tdocsify init --theme pure')
29-
console.log('\t\tdocsify init -l -t buble')
30-
console.log('\t\tdocsify init --local -t dark')
31-
console.log('\t\tdocsify init --local --theme pure')
32-
console.log('\t\tdocsify init "./docs"')
33-
console.log('\t\tdocsify init -l "./docs"')
34-
console.log('\t\tdocsify init -l -t buble "./docs"')
35-
console.log('\t\tdocsify init -l -t dark "./docs"')
36-
console.log('\t\tdocsify init -l -t pure "./docs"')
37-
})
38-
.action(run.init)
15+
require('yargonaut')
16+
.style('yellow', 'required')
17+
.helpStyle('green')
18+
.errorsStyle('red.bold')
3919

40-
program
41-
.usage('serve [options] <relative path>')
42-
.command('serve [path .]')
43-
.alias('s')
44-
.description('run serve to preview')
45-
.option('-p, --port [port 3000]', 'listen port', 3000)
46-
.on('--help', function () {
47-
console.log('\tExamples:\n')
48-
console.log('\t\tdocsify s')
49-
console.log('\t\tdocsify serve')
50-
console.log('\t\tdocsify serve -p 8080')
51-
console.log('\t\tdocsify serve --port 8080')
20+
var yargs = require('yargs')
21+
.usage(chalk.bold(y18n.localized('usageDesc') + ': docsify <command> <path> [options]'))
22+
.command({
23+
command: '*',
24+
desc: 'the default command',
25+
builder: () => {},
26+
handler: (yargs) => {
27+
yargs.help()
28+
}
5229
})
53-
.action(run.serve)
54-
55-
program.parse(process.argv)
56-
57-
if (!program.args.length) program.help()
30+
.command({
31+
command: 'init <path> [--local] [--theme vue]',
32+
alias: 'i',
33+
desc: chalk.gray(y18n.localized('commandInitDesc')),
34+
builder: (yargs) => yargs.options({
35+
'local': {
36+
alias: 'l',
37+
default: false,
38+
desc: chalk.gray(y18n.localized('commandInitOptionLocalDesc')),
39+
nargs: 0,
40+
requiresArg: false,
41+
type: 'boolean'
42+
},
43+
'theme': {
44+
alias: 't',
45+
default: 'vue',
46+
desc: chalk.gray(y18n.localized('commandInitOptionThemeDesc')),
47+
choices: ['vue', 'buble', 'dark', 'pure'],
48+
nargs: 1,
49+
requiresArg: true,
50+
type: 'string'
51+
}
52+
}),
53+
handler: (argv) => run.init(argv.path, argv.local, argv.theme)
54+
})
55+
.command({
56+
command: 'serve <path> [--port 3000]',
57+
alias: 's',
58+
desc: chalk.gray(y18n.localized('commandServeDesc')),
59+
builder: (yargs) => yargs.options({
60+
'port': {
61+
alias: 'p',
62+
default: 3000,
63+
desc: chalk.gray(y18n.localized('commandServeOptionPortDesc')),
64+
nargs: 1,
65+
requiresArg: true,
66+
type: 'number'
67+
}
68+
}),
69+
handler: (argv) => run.serve(argv.path, argv.port)
70+
})
71+
.help()
72+
.option('help', {
73+
alias: 'h',
74+
type: 'boolean',
75+
desc: chalk.gray(y18n.localized('optionHelpDesc'))
76+
})
77+
.version('\ndocsify-cli version:\n ' + pkg.version + '\n')
78+
.option('version', {
79+
alias: 'v',
80+
type: 'boolean',
81+
desc: chalk.gray(y18n.localized('optionVersionDesc'))
82+
})
83+
.epilog(chalk.green(y18n.localized('epilogDesc')) + ':\n' +
84+
' https://QingWei-Li.github.io/docsify\n' +
85+
' https://QingWei-Li.github.io/docsify-cli\n')
86+
.argv

Diff for: ‎bin/locales/de.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"usageDesc": "Anwendung",
3+
"commandInitDesc": "Erzeuge neue Dokumentation.",
4+
"commandInitOptionLocalDesc": "Kopiere docsify Dateien in lokale Ordner.",
5+
"commandInitOptionThemeDesc": "Zu verwendende Theme Dateien.",
6+
"commandServeDesc": "Lasse lokalen Server zur Webseitenvorschau laufen.",
7+
"commandServeOptionPortDesc": "Listen port.",
8+
"optionHelpDesc": "Zeige Hilfe an",
9+
"optionVersionDesc": "Zeige Versionsnummer an",
10+
"epilogDesc": "Dokumentation"
11+
}

Diff for: ‎bin/locales/en.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"usageDesc": "Usage",
3+
"commandInitDesc": "Creates new docs",
4+
"commandInitOptionLocalDesc": "Copy docsify files to local.",
5+
"commandInitOptionThemeDesc": "Theme file to be used.",
6+
"commandServeDesc": "Run local server to preview site.",
7+
"commandServeOptionPortDesc": "Listen port.",
8+
"optionHelpDesc": "Show help",
9+
"optionVersionDesc": "Show version number",
10+
"epilogDesc": "Documentation"
11+
}

Diff for: ‎bin/locales/index.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict'
2+
3+
const osLocale = require('os-locale')
4+
const path = require('path')
5+
const Y18n = require('y18n')
6+
const fse = require('fs-extra')
7+
8+
class Locales {
9+
10+
constructor () {
11+
this.y18n = Y18n({
12+
directory: path.resolve(__dirname),
13+
updateFiles: false,
14+
locale: this.detectOsLocale()
15+
})
16+
}
17+
18+
detectOsLocale () {
19+
const locale = this._getOsLocale()
20+
21+
const exist = this._checkIfLocaleFileExist(locale)
22+
23+
if (exist) {
24+
return locale
25+
}
26+
27+
return 'en'
28+
}
29+
30+
_getOsLocale () {
31+
var locale
32+
try {
33+
locale = osLocale.sync({ spawn: false })
34+
} catch (err) {
35+
locale = 'en'
36+
}
37+
38+
return locale
39+
}
40+
41+
_checkIfLocaleFileExist (locale) {
42+
const json = fse.readJsonSync(path.join(__dirname, `${locale.substring(0, 2)}.json`), { throws: false })
43+
44+
return json != null
45+
}
46+
47+
localized (key) {
48+
return this.y18n.__(key)
49+
}
50+
51+
}
52+
53+
module.exports = Locales

Diff for: ‎src/index.js renamed to ‎lib/commands/init.js

+14-16
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
1-
var fs = require('fs')
2-
var cp = require('cp-file').sync
3-
var util = require('./util')
1+
const fs = require('fs')
2+
const cp = require('cp-file').sync
3+
const chalk = require('chalk')
4+
const util = require('../util/index')
45

5-
var exist = util.exist
6+
var exists = util.exists
67
var cwd = util.cwd
78
var pwd = util.pwd
89
var resolve = util.resolve
9-
var green = util.green
1010

1111
var replace = function (file, tpl, replace) {
1212
fs.writeFileSync(file, fs.readFileSync(file).toString().replace(tpl, replace), 'utf-8')
1313
}
1414

15-
var PKG = exist(cwd('package.json')) ? require(cwd('package.json')) : {}
15+
var PKG = exists(cwd('package.json')) ? require(cwd('package.json')) : {}
1616

17-
exports.init = function (path, option) {
17+
module.exports = function (path, local, theme) {
1818
path = path || '.'
19-
var msg = `\nCreate succeed! Please run\n
20-
> ${green(`docsify serve ${path}`)}\n`
19+
var msg = '\n' + chalk.green('Initialization succeeded!') + ' Please run ' +
20+
chalk.inverse(`docsify serve ${path}`) + '\n'
2121

2222
path = cwd(path)
2323
var target = function (file) {
2424
return resolve(path, file)
2525
}
26-
var readme = exist(cwd('README.md')) || pwd('template/README.md')
26+
var readme = exists(cwd('README.md')) || pwd('template/README.md')
2727
var main = pwd('template/index.html')
2828

29-
if (option.local) {
29+
if (local) {
3030
main = pwd('template/index.local.html')
3131

32-
var vendor = exist(cwd('node_modules/docsify')) || pwd('../node_modules/docsify')
32+
var vendor = exists(cwd('node_modules/docsify')) || pwd('../node_modules/docsify')
3333

3434
cp(resolve(vendor, 'lib/docsify.min.js'), target('vendor/docsify.js'))
35-
cp(resolve(vendor, `lib/themes/${option.theme}.css`), target(`vendor/themes/${option.theme}.css`))
35+
cp(resolve(vendor, `lib/themes/${theme}.css`), target(`vendor/themes/${theme}.css`))
3636
}
3737
var filename = 'index.html'
3838

3939
cp(readme, target('README.md'))
4040
cp(main, target(filename))
4141
cp(pwd('template/.nojekyll'), target('.nojekyll'))
4242

43-
replace(target(filename), 'vue.css', `${option.theme}.css`)
43+
replace(target(filename), 'vue.css', `${theme}.css`)
4444

4545
if (PKG.name) {
4646
replace(target(filename), 'Document', PKG.name + (PKG.description ? (' - ' + PKG.description) : ''))
@@ -55,5 +55,3 @@ exports.init = function (path, option) {
5555
}
5656
console.log(msg)
5757
}
58-
59-
exports.serve = require('./serve')

Diff for: ‎lib/commands/serve.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const serveStatic = require('serve-static')
2+
const connect = require('connect')
3+
const livereload = require('connect-livereload')
4+
const lrserver = require('livereload')
5+
const chalk = require('chalk')
6+
const util = require('../util/index')
7+
8+
var exists = util.exists
9+
var resolve = util.resolve
10+
11+
module.exports = function (path, port) {
12+
path = resolve(path || '.')
13+
var indexFile = resolve(path, 'index.html')
14+
15+
if (!exists(indexFile)) {
16+
const msg = '\nNo docs found, please run ' + chalk.inverse('docsify init') + ' first.\n'
17+
console.log(msg)
18+
process.exit(0)
19+
}
20+
21+
var server = connect()
22+
23+
server.use(livereload())
24+
server.use(serveStatic(path))
25+
server.listen(port)
26+
lrserver.createServer({
27+
exts: ['md']
28+
}).watch(path)
29+
30+
const msg = '\nServing ' + chalk.inverse(`${path}`) + ' now.\n' +
31+
'Listening at ' + chalk.inverse(`http://localhost:${port}`) + '\n'
32+
console.log(msg)
33+
}

Diff for: ‎lib/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
init: require('./commands/init'),
3+
serve: require('./commands/serve')
4+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Diff for: ‎src/util.js renamed to ‎lib/util/index.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,12 @@ exports.cwd = function (path) {
77
}
88

99
exports.pwd = function (path) {
10-
return resolve(__dirname, path)
10+
return resolve(require('path').dirname(__dirname), path)
1111
}
1212

13-
exports.exist = function (path) {
13+
exports.exists = function (path) {
1414
if (fs.existsSync(path)) {
1515
return path
1616
}
1717
return undefined
1818
}
19-
20-
exports.green = function (str) {
21-
return '\u001B[32m' + str + '\u001B[39m'
22-
}

Diff for: ‎package.json

+29-24
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,54 @@
22
"name": "docsify-cli",
33
"version": "3.0.2",
44
"description": "A magical documentation generator.",
5+
"author": "qingwei-li <cinwell.li@gmail.com> (https://github.com/QingWei-Li)",
6+
"homepage": "https://github.com/QingWei-Li/docsify-cli#readme",
7+
"repository": {
8+
"type": "git",
9+
"url": "git+https://github.com/QingWei-Li/docsify-cli.git"
10+
},
11+
"license": "MIT",
12+
"engines": {
13+
"node": ">= 4"
14+
},
515
"main": "bin/docsify",
616
"bin": {
717
"docsify": "bin/docsify"
818
},
919
"files": [
1020
"bin",
11-
"src"
21+
"lib"
1222
],
1323
"scripts": {
14-
"test": "eslint src bin/docsify --fix"
15-
},
16-
"engines": {
17-
"node": ">= 4"
18-
},
19-
"keywords": [
20-
"docsify",
21-
"doc",
22-
"docs",
23-
"documentation",
24-
"creator",
25-
"generator",
26-
"cli"
27-
],
28-
"author": "qingwei-li <cinwell.li@gmail.com> (https://github.com/QingWei-Li)",
29-
"homepage": "https://github.com/QingWei-Li/docsify-cli",
30-
"repository": {
31-
"type": "git",
32-
"url": "https://github.com/QingWei-Li/docsify-cli.git"
24+
"test": "eslint lib bin/docsify --fix"
3325
},
34-
"license": "MIT",
3526
"dependencies": {
36-
"commander": "^2.9.0",
27+
"chalk": "^1.1.0",
3728
"connect": "^3.5.0",
3829
"connect-livereload": "^0.6.0",
3930
"cp-file": "^4.1.0",
4031
"docsify": ">=2",
32+
"fs-extra": "^2.0.0",
4133
"livereload": "^0.6.0",
34+
"os-locale": "^2.0.0",
4235
"serve-static": "^1.11.1",
43-
"update-notifier": "^1.0.3"
36+
"update-notifier": "^1.0.3",
37+
"y18n": "^3.2.1",
38+
"yargonaut": "^1.1.2",
39+
"yargs": "^6.4.0"
4440
},
4541
"devDependencies": {
4642
"eslint": "^3.10.2",
4743
"eslint-config-vue": "^2.0.1",
4844
"eslint-plugin-vue": "^1.0.0"
49-
}
45+
},
46+
"keywords": [
47+
"docsify",
48+
"doc",
49+
"docs",
50+
"documentation",
51+
"creator",
52+
"generator",
53+
"cli"
54+
]
5055
}

Diff for: ‎src/serve.js

-33
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.