3
3
const fs = require ( 'fs' )
4
4
const cp = require ( 'cp-file' ) . sync
5
5
const chalk = require ( 'chalk' )
6
+ const { version} = require ( '../../package.json' )
6
7
const logger = require ( '../util/logger' )
7
- const { prompt} = require ( 'enquirer' )
8
+ const { prompt, MultiSelect } = require ( 'enquirer' )
8
9
const { cwd, exists, pkg, pwd, read, resolve} = require ( '../util' )
10
+ const colors = require ( 'ansi-colors' )
9
11
10
12
const replace = function ( file , tpl , replace ) {
11
13
fs . writeFileSync ( file , read ( file ) . replace ( tpl , replace ) , 'utf-8' )
12
14
}
13
15
14
16
// eslint-disable-next-line
15
- module . exports = function ( path = '' , local , theme ) {
17
+ module . exports = async function ( path = '' , local , theme , plugins ) {
16
18
const msg =
17
19
'\n' +
18
20
chalk . green ( 'Initialization succeeded!' ) +
@@ -25,32 +27,31 @@ module.exports = function (path = '', local, theme) {
25
27
if ( exists ( cwdPath ) ) {
26
28
logger . error ( `${ path || '.' } already exists.` )
27
29
28
- prompt ( {
29
- type : 'confirm' ,
30
- name : 'rewrite' ,
31
- symbols : {
32
- separator : ''
33
- } ,
34
- message : 'Are you sure you want to rewrite it?'
35
- } )
36
- . then ( answers => {
37
- if ( answers . rewrite === false ) {
38
- return process . exit ( 0 )
39
- }
40
-
41
- createFile ( cwdPath , local , theme )
42
- console . log ( msg )
30
+ let answer = { }
31
+ try {
32
+ answer = await prompt ( {
33
+ type : 'confirm' ,
34
+ name : 'rewrite' ,
35
+ symbols : {
36
+ separator : ''
37
+ } ,
38
+ message : 'Are you sure you want to rewrite it?'
43
39
} )
44
- . catch ( console . error )
45
-
46
- return false
40
+ } catch ( err ) {
41
+ err && logger . error ( err )
42
+ process . exit ( 1 )
43
+ }
44
+
45
+ if ( ! answer . rewrite ) {
46
+ return
47
+ }
47
48
}
48
49
49
- createFile ( cwdPath , local , theme )
50
+ await createFile ( cwdPath , local , theme , plugins )
50
51
console . log ( msg )
51
52
}
52
53
53
- function createFile ( path , local , theme ) {
54
+ async function createFile ( path , local , theme , plugins ) {
54
55
const target = file => resolve ( path , file )
55
56
const readme = exists ( cwd ( 'README.md' ) ) || pwd ( 'template/README.md' )
56
57
let main = pwd ( 'template/index.html' )
@@ -95,4 +96,60 @@ function createFile(path, local, theme) {
95
96
. replace ( / ^ g i t \+ / g, '' )
96
97
replace ( target ( filename ) , 'repo: \'\'' , `repo: '${ repo } '` )
97
98
}
99
+
100
+ // Return early if not opted for plugins
101
+ if ( ! plugins ) {
102
+ return replace ( target ( filename ) , '\n _plugins_' , '' )
103
+ }
104
+
105
+ const officialPlugins = [
106
+ 'front-matter' ,
107
+ 'search' ,
108
+ 'disqus' ,
109
+ 'emoji' ,
110
+ 'external-script' ,
111
+ 'ga' ,
112
+ 'gitalk' ,
113
+ 'matomo' ,
114
+ 'zoom-image'
115
+ ]
116
+
117
+ const choices = officialPlugins . map ( name => ( { name, value : name } ) )
118
+ const prompt = new MultiSelect ( {
119
+ name : 'plugins' ,
120
+ message : 'Select plugins to be used' ,
121
+ hint : '(Use <space> to select, <return> to submit)' ,
122
+ default : '' ,
123
+ choices,
124
+ indicator ( state , choice ) {
125
+ if ( choice . enabled ) {
126
+ return colors . cyan ( state . symbols . radio . on )
127
+ }
128
+
129
+ return colors . gray ( state . symbols . radio . off )
130
+ }
131
+ } )
132
+
133
+ prompt . on ( 'cancel' , ( ) => replace ( target ( filename ) , '\n _plugins_' , '' ) )
134
+
135
+ let answers = [ ]
136
+ try {
137
+ answers = await prompt . run ( )
138
+ } catch ( err ) {
139
+ if ( err ) {
140
+ logger . error ( err )
141
+ process . exitCode = 1
142
+ }
143
+
144
+ return
145
+ }
146
+
147
+ replace ( target ( filename ) , ' _plugins_' , '_plugin' . repeat ( answers . length + 1 ) )
148
+
149
+ answers . forEach ( plugin => {
150
+ const url = `//cdn.jsdelivr.net/npm/docsify@${ version [ 0 ] } /lib/plugins/${ plugin } .min.js`
151
+ replace ( target ( filename ) , '_plugin' , ` <script src="${ url } "></script>\n` )
152
+ } )
153
+
154
+ replace ( target ( filename ) , '\n_plugin' , '' )
98
155
}
0 commit comments