forked from docsifyjs/docsify-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.js
79 lines (64 loc) · 2.18 KB
/
init.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'use strict'
const fs = require('fs')
const cp = require('cp-file').sync
const chalk = require('chalk')
const {cwd, exists, pkg, pwd, read, resolve} = require('../util')
const replace = function (file, tpl, replace) {
fs.writeFileSync(file, read(file).replace(tpl, replace), 'utf-8')
}
// eslint-disable-next-line
module.exports = function (path = '', local, theme, plugins) {
const msg =
'\n' +
chalk.green('Initialization succeeded!') +
' Please run ' +
chalk.inverse(`docsify serve ${path}`) +
'\n'
path = cwd(path || '.')
const target = file => resolve(path, file)
const readme = exists(cwd('README.md')) || pwd('template/README.md')
let main = pwd('template/index.html')
if (local) {
main = pwd('template/index.local.html')
const vendor =
exists(cwd('node_modules/docsify')) || pwd('../node_modules/docsify')
cp(resolve(vendor, 'lib/docsify.min.js'), target('vendor/docsify.js'))
cp(
resolve(vendor, `lib/themes/${theme}.css`),
target(`vendor/themes/${theme}.css`)
)
}
const filename = 'index.html'
cp(readme, target('README.md'))
cp(main, target(filename))
cp(pwd('template/.nojekyll'), target('.nojekyll'))
replace(target(filename), 'vue.css', `${theme}.css`)
if (pkg.name) {
replace(
target(filename),
'Document',
pkg.name + (pkg.description ? ' - ' + pkg.description : '')
)
replace(target(filename), 'name: \'\',', `name: '${pkg.name}',`)
}
if (pkg.description) {
replace(target(filename), 'Description', pkg.description)
}
if (pkg.repository) {
const repo = (pkg.repository.url || pkg.repository)
.replace(/\.git$/g, '')
.replace(/^git\+/g, '')
replace(target(filename), 'repo: \'\'', `repo: '${repo}'`)
}
if (plugins) {
replace(target(filename), '_plugins_', '_plugin\n '.repeat(plugins.length))
plugins.forEach(plugin => {
const url = plugin.includes('//') ? plugin : `https://unpkg.com/docsify/lib/plugins/${plugin}.min.js`
const scriptTemplate = `<script src="${url}"></script>`
replace(target(filename), '_plugin', scriptTemplate)
})
} else {
replace(target(filename), '_plugins_', '')
}
console.log(msg)
}