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