Skip to content

Commit

Permalink
refactor: simplify code with async/await
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgeorge007 committed May 27, 2021
1 parent 63e9d95 commit 790e96c
Showing 1 changed file with 57 additions and 56 deletions.
113 changes: 57 additions & 56 deletions lib/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const replace = function (file, tpl, replace) {
}

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

prompt({
const {rewrite} = await prompt({
type: 'confirm',
name: 'rewrite',
symbols: {
separator: ''
},
message: 'Are you sure you want to rewrite it?'
})
.then(answers => {
if (answers.rewrite === false) {
return process.exit(0)
}

createFile(cwdPath, local, theme, plugins, msg)
})
.catch(console.error)

return false
if (!rewrite) {
return
}
}

createFile(cwdPath, local, theme, plugins, msg)
await createFile(cwdPath, local, theme, plugins)
console.log(msg)
}

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

if (plugins) {
const officialPlugins = [
'front-matter',
'search',
'disqus',
'emoji',
'external-script',
'ga',
'gitalk',
'matomo',
'zoom-image'
]

const choices = officialPlugins.map(name => ({name, value: name}))
const prompt = new MultiSelect({
name: 'plugins',
message: 'Select plugins to be used',
hint: '(Use <space> to select, <return> to submit)',
default: '',
choices,
indicator(state, choice) {
if (choice.enabled) {
return colors.cyan(state.symbols.radio.on)
}

return colors.gray(state.symbols.radio.off)
// Return early if not opted for plugins
if (!plugins) {
return replace(target(filename), '\n _plugins_', '')
}

const officialPlugins = [
'front-matter',
'search',
'disqus',
'emoji',
'external-script',
'ga',
'gitalk',
'matomo',
'zoom-image'
]

const choices = officialPlugins.map(name => ({name, value: name}))
const prompt = new MultiSelect({
name: 'plugins',
message: 'Select plugins to be used',
hint: '(Use <space> to select, <return> to submit)',
default: '',
choices,
indicator(state, choice) {
if (choice.enabled) {
return colors.cyan(state.symbols.radio.on)
}
})
prompt.on('cancel', () => replace(target(filename), '\n _plugins_', ''))
prompt.on('close', () => console.log(msg))
prompt.run()
.then(answers => {
replace(target(filename), ' _plugins_', '_plugin'.repeat(answers.length + 1))
let url = ''
answers.forEach(plugin => {
url = `//cdn.jsdelivr.net/npm/docsify@${version[0]}/lib/plugins/${plugin}.min.js`
replace(target(filename), '_plugin', ` <script src="${url}"></script>\n`)
})
replace(target(filename), '\n_plugin', '')
})
.catch(console.error)
} else {
replace(target(filename), '\n _plugins_', '')
console.log(msg)

return colors.gray(state.symbols.radio.off)
}
})

prompt.on('cancel', () => replace(target(filename), '\n _plugins_', ''))

let answers = []
try {
answers = await prompt.run()
} catch (err) {
logger.error(err)
process.exit(1)
}

replace(target(filename), ' _plugins_', '_plugin'.repeat(answers.length + 1))

answers.forEach(plugin => {
const url = `//cdn.jsdelivr.net/npm/docsify@${version[0]}/lib/plugins/${plugin}.min.js`
replace(target(filename), '_plugin', ` <script src="${url}"></script>\n`)
})

replace(target(filename), '\n_plugin', '')
}

0 comments on commit 790e96c

Please sign in to comment.