Permalink
Browse files

fix(install): leave yarn/npm up to the user

closes #878
  • Loading branch information...
JeroenVinke committed Jul 23, 2018
1 parent 987cc68 commit cce5070dde1cecf05f5c2d352fcb941d6fbd908f
Showing with 61 additions and 33 deletions.
  1. +19 −19 lib/commands/new/project-template.js
  2. +42 −14 lib/workflow/activities/project-install.js
@@ -331,17 +331,21 @@ exports.ProjectTemplate = class {
return this.root.create(ui, location);
}
install(ui) {
let workingDirectory = this.options.hasFlag('here')
getWorkingDirectory() {
return this.options.hasFlag('here')
? process.cwd()
: path.posix.join(process.cwd(), this.content.calculateRelativePath());
}
install(ui, packageManager) {
let workingDirectory = this.getWorkingDirectory();
return installDependencies(ui, workingDirectory)
return installDependencies(workingDirectory, packageManager)
.then(() => runPostInstallProcesses(ui, workingDirectory, this.postInstallProcesses));
}
};
function installDependencies(ui, workingDirectory, dependencies) {
function installDependencies(workingDirectory, packageManager) {
let npm = new NPM();
let npmOptions = {
loglevel: 'error',
@@ -351,24 +355,20 @@ function installDependencies(ui, workingDirectory, dependencies) {
workingDirectory: workingDirectory
};
// try yarn, but if something fails then fall back to NPM
try {
let yarn = new Yarn();
if (yarn.isAvailable(workingDirectory)) {
return yarn.install([], { cwd: workingDirectory })
.catch(e => {
logger.error('Something went wrong while attempting to use Yarn. Falling back to NPM');
logger.info(e);
if (packageManager === 'yarn') {
const yarn = new Yarn();
return yarn.install([], { cwd: workingDirectory })
.catch(e => {
logger.error('Something went wrong while attempting to use Yarn. Falling back to NPM');
logger.info(e);
return npm.install([], npmOptions);
});
}
} catch (e) {
logger.error('Something went wrong while attempting to search for Yarn. Falling back to NPM');
logger.info(e);
return npm.install([], npmOptions);
});
} else if (packageManager === 'npm') {
return npm.install([], npmOptions);
}
return npm.install([], npmOptions);
throw new Error(`Unknown package manager ${packageManager}`);
}
function addDependencies(current, toAdd) {
@@ -3,6 +3,7 @@ const os = require('os');
const UI = require('../../ui').UI;
const transform = require('../../colors/transform');
const createLines = require('../../string').createLines;
const Yarn = require('../../package-managers/yarn').Yarn;
const CLIOptions = require('../../cli-options').CLIOptions;
module.exports = class {
@@ -15,22 +16,49 @@ module.exports = class {
execute(context) {
let project = context.state.project;
let options = [];
let yarn = new Yarn();
if (yarn.isAvailable(project.getWorkingDirectory())) {
this.ui.log('Note: lock files are not cross compatible between package managers. Choose Yarn here only if you intend to use Yarn for future package installs. Alternatively, remove either yarn.lock or package-lock.json from the project directory before installing new packages.');
options = [
{
displayName: 'Yes, use Yarn',
description: 'Installs all server, client and tooling dependencies needed to build the project using Yarn.',
value: 'yarn'
},
{
displayName: 'Yes, use NPM',
description: 'Installs all server, client and tooling dependencies needed to build the project using NPM.',
value: 'npm'
},
{
displayName: 'No',
description: 'Completes the new project wizard without installing dependencies.',
value: 'no'
}
];
} else {
options = [
{
displayName: 'Yes',
description: 'Installs all server, client and tooling dependencies needed to build the project.',
value: 'yes'
},
{
displayName: 'No',
description: 'Completes the new project wizard without installing dependencies.',
value: 'no'
}
];
}
return this.ui.question('Would you like to install the project dependencies?', options).then(answer => {
if (answer.value === 'yes' || answer.value === 'npm' || answer.value === 'yarn') {
const packageManager = answer.value === 'yes' ? 'npm' : answer.value;
return this.ui.question('Would you like to install the project dependencies?', [
{
displayName: 'Yes',
description: 'Installs all server, client and tooling dependencies needed to build the project.',
value: 'yes'
},
{
displayName: 'No',
description: 'Completes the new project wizard without installing dependencies.',
value: 'no'
}
]).then(answer => {
if (answer.value === 'yes') {
return this.ui.log(os.EOL + 'Installing project dependencies.')
.then(() => project.install(this.ui))
.then(() => project.install(this.ui, packageManager))
.then(() => this.displayCompletionMessage(project))
.then(() => context.next(this.nextActivity));
}

0 comments on commit cce5070

Please sign in to comment.