Skip to content

Commit 790e96c

Browse files
refactor: simplify code with async/await
1 parent 63e9d95 commit 790e96c

File tree

1 file changed

+57
-56
lines changed

1 file changed

+57
-56
lines changed

Diff for: lib/commands/init.js

+57-56
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const replace = function (file, tpl, replace) {
1414
}
1515

1616
// eslint-disable-next-line
17-
module.exports = function (path = '', local, theme, plugins) {
17+
module.exports = async function (path = '', local, theme, plugins) {
1818
const msg =
1919
'\n' +
2020
chalk.green('Initialization succeeded!') +
@@ -27,30 +27,25 @@ module.exports = function (path = '', local, theme, plugins) {
2727
if (exists(cwdPath)) {
2828
logger.error(`${path || '.'} already exists.`)
2929

30-
prompt({
30+
const {rewrite} = await prompt({
3131
type: 'confirm',
3232
name: 'rewrite',
3333
symbols: {
3434
separator: ''
3535
},
3636
message: 'Are you sure you want to rewrite it?'
3737
})
38-
.then(answers => {
39-
if (answers.rewrite === false) {
40-
return process.exit(0)
41-
}
4238

43-
createFile(cwdPath, local, theme, plugins, msg)
44-
})
45-
.catch(console.error)
46-
47-
return false
39+
if (!rewrite) {
40+
return
41+
}
4842
}
4943

50-
createFile(cwdPath, local, theme, plugins, msg)
44+
await createFile(cwdPath, local, theme, plugins)
45+
console.log(msg)
5146
}
5247

53-
function createFile(path, local, theme, plugins, msg) {
48+
async function createFile(path, local, theme, plugins) {
5449
const target = file => resolve(path, file)
5550
const readme = exists(cwd('README.md')) || pwd('template/README.md')
5651
let main = pwd('template/index.html')
@@ -96,49 +91,55 @@ function createFile(path, local, theme, plugins, msg) {
9691
replace(target(filename), 'repo: \'\'', `repo: '${repo}'`)
9792
}
9893

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)
125121
}
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)
143135
}
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', '')
144145
}

0 commit comments

Comments
 (0)