Skip to content
This repository has been archived by the owner on Mar 13, 2022. It is now read-only.

refactor: use async/await and make complete async #150

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ module.exports = {
css: {
type: 'list',
message: 'Pick your CSS preprocessor:',
default: 'scss',
choices: [
{
name: 'Sass with SCSS syntax',
Expand Down
87 changes: 49 additions & 38 deletions utils/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
const path = require('path')
const fs = require('fs')
const spawn = require('child_process').spawn
const { spawn } = require('child_process')

const lintStyles = ['standard', 'airbnb', 'prettier']
const lintStyles = ['prettier', 'standard', 'airbnb']

/**
* Shows a colored string prefixed with a star char
* @param {Function} color Helper color function
* @param {string} messagge Message to display
*/
function starredLog(color, message) {
console.log(`\n\n ${color(`[*] ${message}`)}\n\n`)
}

/**
* Sorts dependencies in package.json alphabetically.
Expand Down Expand Up @@ -36,32 +45,42 @@ function sortDependencies(data) {
* @param {string} cwd Path of the created project directory
* @param {object} data Data from questionnaire
*/
function installDependencies(cwd, executable = 'npm', color) {
console.log(`\n\n ${color('[*] Installing project dependencies ...')}\n`)
return runCommand(executable, ['install'], { cwd })
async function installDependencies(cwd, executable = 'npm', color) {
starredLog(color, 'Installing project dependencies...')

try {
await runCommand(executable, ['install'], { cwd })
} catch(e) {
console.log()
console.log(` ${executable} install FAILED... Possible temporary npm registry issues?`)
console.log(` Please try again later...`)
console.log()
process.exit(1)
}
}

/**
* Runs `npm run lint -- --fix` in the project directory
* @param {string} cwd Path of the created project directory
* @param {object} data Data from questionnaire
*/
function runLintFix(cwd, data, color) {
async function runLintFix(cwd, data, color) {
if (data.preset.lint && lintStyles.indexOf(data.lintConfig) !== -1) {
console.log(
`\n\n ${color(
'[*] Running eslint --fix to comply with chosen preset rules...'
)}\n\n`
)
starredLog(color, 'Running eslint --fix to comply with chosen preset rules...')
const args =
data.autoInstall === 'npm'
? ['run', 'lint', '--', '--fix']
: ['run', 'lint', '--fix']
return runCommand(data.autoInstall, args, {
cwd,
})

try {
await runCommand(data.autoInstall, args, { cwd })
} catch(e) {
console.log()
console.log(` ${data.autoInstall} lint FAILED...`)
console.log(` Please try again later...`)
console.log()
}
}
return Promise.resolve()
}

/**
Expand Down Expand Up @@ -143,14 +162,11 @@ function runCommand(cmd, args, options) {

spwan.on('exit', code => {
if (code) {
console.log()
console.log(` ${cmd} install FAILED... Possible temporary npm registry issues?`)
console.log(` Please try again later...`)
console.log()
process.exit(1)
reject(code)
}
else {
resolve()
}

resolve()
})
})
}
Expand All @@ -166,26 +182,21 @@ function sortObject(object) {
return sortedObject
}

module.exports.complete = function (data, { chalk }) {
const green = chalk.green
module.exports.complete = async function (data, { chalk }) {
const { green } = chalk

sortDependencies(data, green)

const cwd = path.join(process.cwd(), data.inPlace ? '' : data.destDirName)

if (data.autoInstall) {
installDependencies(cwd, data.autoInstall, green)
.then(() => {
return runLintFix(cwd, data, green)
})
.then(() => {
printMessage(data, green)
})
.catch(e => {
console.log(chalk.red('Error:'), e)
})
}
else {
printMessage(data, chalk)
try {
if (data.autoInstall) {
await installDependencies(cwd, data.autoInstall, green)
await runLintFix(cwd, data, green)
}
} catch(e) {
console.log(chalk.red('Error:'), e)
}

printMessage(data, chalk)
}