33const fs = require ( 'fs' )
44const cp = require ( 'cp-file' ) . sync
55const chalk = require ( 'chalk' )
6+ const { version} = require ( '../../package.json' )
67const logger = require ( '../util/logger' )
7- const { prompt} = require ( 'enquirer' )
8+ const { prompt, MultiSelect } = require ( 'enquirer' )
89const { cwd, exists, pkg, pwd, read, resolve} = require ( '../util' )
10+ const colors = require ( 'ansi-colors' )
911
1012const replace = function ( file , tpl , replace ) {
1113 fs . writeFileSync ( file , read ( file ) . replace ( tpl , replace ) , 'utf-8' )
1214}
1315
1416// eslint-disable-next-line
15- module . exports = function ( path = '' , local , theme ) {
17+ module . exports = async function ( path = '' , local , theme , plugins ) {
1618 const msg =
1719 '\n' +
1820 chalk . green ( 'Initialization succeeded!' ) +
@@ -25,32 +27,31 @@ module.exports = function (path = '', local, theme) {
2527 if ( exists ( cwdPath ) ) {
2628 logger . error ( `${ path || '.' } already exists.` )
2729
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?'
4339 } )
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+ }
4748 }
4849
49- createFile ( cwdPath , local , theme )
50+ await createFile ( cwdPath , local , theme , plugins )
5051 console . log ( msg )
5152}
5253
53- function createFile ( path , local , theme ) {
54+ async function createFile ( path , local , theme , plugins ) {
5455 const target = file => resolve ( path , file )
5556 const readme = exists ( cwd ( 'README.md' ) ) || pwd ( 'template/README.md' )
5657 let main = pwd ( 'template/index.html' )
@@ -95,4 +96,60 @@ function createFile(path, local, theme) {
9596 . replace ( / ^ g i t \+ / g, '' )
9697 replace ( target ( filename ) , 'repo: \'\'' , `repo: '${ repo } '` )
9798 }
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' , '' )
98155}
0 commit comments