From 9f3f97d382a2d6e335ec338bcdaf779afba03550 Mon Sep 17 00:00:00 2001 From: Vicente Jr Yuchitcho Date: Sun, 26 Feb 2017 03:48:53 +1100 Subject: [PATCH] Gracefully handle initial installation error (#1512) * Gracefully handle initial installation error * Print out message when problem occurs * Delete project folder on errors * Fix directory deleting message Resolves #1505 # Conflicts: # tasks/e2e-installs.sh --- packages/create-react-cy-app/index.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/create-react-cy-app/index.js b/packages/create-react-cy-app/index.js index 56d4c6b83e5..265fdbe0603 100755 --- a/packages/create-react-cy-app/index.js +++ b/packages/create-react-cy-app/index.js @@ -182,7 +182,30 @@ function run(root, appName, version, verbose, originalDirectory, template) { install(allDependencies, verbose, function(code, command, args) { if (code !== 0) { - console.error(chalk.cyan(command + ' ' + args.join(' ')) + ' failed'); + console.log(); + console.error('Aborting installation.', chalk.cyan(command + ' ' + args.join(' ')), 'has failed.'); + // On 'exit' we will delete these files from target directory. + var knownGeneratedFiles = [ + 'package.json', 'npm-debug.log', 'yarn-error.log', 'yarn-debug.log', 'node_modules' + ]; + var currentFiles = fs.readdirSync(path.join(root)); + currentFiles.forEach(function (file) { + knownGeneratedFiles.forEach(function (fileToMatch) { + // This will catch `(npm-debug|yarn-error|yarn-debug).log*` files + // and the rest of knownGeneratedFiles. + if ((fileToMatch.match(/.log/g) && file.indexOf(fileToMatch) === 0) || file === fileToMatch) { + console.log('Deleting generated file...', chalk.cyan(file)); + fs.removeSync(path.join(root, file)); + } + }); + }); + var remainingFiles = fs.readdirSync(path.join(root)); + if (!remainingFiles.length) { + // Delete target folder if empty + console.log('Deleting', chalk.cyan(appName + '/'), 'from', chalk.cyan(path.resolve(root, '..'))); + fs.removeSync(path.join(root)); + } + console.log('Done.'); process.exit(1); }