Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle npm install error gracefully #1172

Closed
wants to merge 4 commits into from
Closed
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
7 changes: 4 additions & 3 deletions meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,16 @@ module.exports = {
const cwd = path.join(process.cwd(), data.inPlace ? '' : data.destDirName)

if (data.autoInstall) {
installDependencies(cwd, data.autoInstall, green)
installDependencies(cwd, data, chalk)
.then(() => {
return runLintFix(cwd, data, green)
return runLintFix(cwd, data, chalk)
})
.then(() => {
printMessage(data, green)
})
.catch(e => {
console.log(chalk.red('Error:'), e)
console.log(e.name)
console.log(e.message)
})
} else {
printMessage(data, chalk)
Expand Down
52 changes: 44 additions & 8 deletions utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,33 @@ exports.sortDependencies = function sortDependencies(data) {
*/
exports.installDependencies = function installDependencies(
cwd,
executable = 'npm',
color
data,
chalk
) {
console.log(`\n\n# ${color('Installing project dependencies ...')}`)
const executable = data.autoInstall
console.log(`\n\n# ${chalk.green('Installing project dependencies ...')}`)
console.log('# ========================\n')
return runCommand(executable, ['install'], {
cwd,
})
.catch(err => {

const msg = npmErrorMsg(data, chalk)
const error = new Error(msg)
error.name = 'VueTemplateNpmInstallError'
throw error
})
}

/**
* Runs `npm run lint -- --fix` in the project directory
* @param {string} cwd Path of the created project directory
* @param {object} data Data from questionnaire
*/
exports.runLintFix = function runLintFix(cwd, data, color) {
exports.runLintFix = function runLintFix(cwd, data, chalk) {
if (data.lint && lintStyles.indexOf(data.lintConfig) !== -1) {
console.log(
`\n\n${color(
`\n\n${chalk.green(
'Running eslint --fix to comply with chosen preset rules...'
)}`
)
Expand Down Expand Up @@ -73,7 +81,7 @@ exports.printMessage = function printMessage(data, { green, yellow }) {
To get started:

${yellow(
`${data.inPlace ? '' : `cd ${data.destDirName}\n `}${installMsg(
`${dirMsg(data)}${installMsg(
data
)}${lintMsg(data)}npm run dev`
)}
Expand All @@ -83,6 +91,17 @@ Documentation can be found at https://vuejs-templates.github.io/webpack
console.log(message)
}

/**
* If the project was generated in a new subdirectory,
* this will print a instructions to navigate to that directory.
* @param {Object} data Data from questionnaire.
*/
function dirMsg(data) {
return data.inPlace
? ''
: `cd ${data.destDirName}\n `
}

/**
* If the user will have to run lint --fix themselves, it returns a string
* containing the instruction for this step.
Expand All @@ -105,6 +124,19 @@ function installMsg(data) {
return !data.autoInstall ? 'npm install (or if using yarn: yarn)\n ' : ''
}

function npmErrorMsg(data, chalk) {
return `
# ${chalk.red('Error installing dependencies')}
# ========================

Don't worry though, your project is still totally fine.
We just had problems running \`npm install\` for you.

So run these commands in your project directory and you will be fine:
${dirMsg(data)}${chalk.yellow('npm install')} (or for yarn: ${chalk.yellow('yarn')})
${data.lint ? `${chalk.yellow('npm run lint -- --fix')} (or for yarn: ${chalk.yellow('yarn lint --fix')})` : ''}`
}

/**
* Spawns a child process and runs the specified command
* By default, runs in the CWD and inherits stdio
Expand All @@ -115,7 +147,7 @@ function installMsg(data) {
*/
function runCommand(cmd, args, options) {
return new Promise((resolve, reject) => {
const spwan = spawn(
const child = spawn(
cmd,
args,
Object.assign(
Expand All @@ -128,9 +160,13 @@ function runCommand(cmd, args, options) {
)
)

spwan.on('exit', () => {
child.on('exit', () => {
resolve()
})

child.on('error', error => {
reject(error)
})
})
}

Expand Down