Skip to content

Commit cce5070

Browse files
committed
fix(install): leave yarn/npm up to the user
closes #878
1 parent 987cc68 commit cce5070

File tree

2 files changed

+61
-33
lines changed

2 files changed

+61
-33
lines changed

lib/commands/new/project-template.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -331,17 +331,21 @@ exports.ProjectTemplate = class {
331331
return this.root.create(ui, location);
332332
}
333333

334-
install(ui) {
335-
let workingDirectory = this.options.hasFlag('here')
334+
getWorkingDirectory() {
335+
return this.options.hasFlag('here')
336336
? process.cwd()
337337
: path.posix.join(process.cwd(), this.content.calculateRelativePath());
338+
}
339+
340+
install(ui, packageManager) {
341+
let workingDirectory = this.getWorkingDirectory();
338342

339-
return installDependencies(ui, workingDirectory)
343+
return installDependencies(workingDirectory, packageManager)
340344
.then(() => runPostInstallProcesses(ui, workingDirectory, this.postInstallProcesses));
341345
}
342346
};
343347

344-
function installDependencies(ui, workingDirectory, dependencies) {
348+
function installDependencies(workingDirectory, packageManager) {
345349
let npm = new NPM();
346350
let npmOptions = {
347351
loglevel: 'error',
@@ -351,24 +355,20 @@ function installDependencies(ui, workingDirectory, dependencies) {
351355
workingDirectory: workingDirectory
352356
};
353357

354-
// try yarn, but if something fails then fall back to NPM
355-
try {
356-
let yarn = new Yarn();
357-
if (yarn.isAvailable(workingDirectory)) {
358-
return yarn.install([], { cwd: workingDirectory })
359-
.catch(e => {
360-
logger.error('Something went wrong while attempting to use Yarn. Falling back to NPM');
361-
logger.info(e);
358+
if (packageManager === 'yarn') {
359+
const yarn = new Yarn();
360+
return yarn.install([], { cwd: workingDirectory })
361+
.catch(e => {
362+
logger.error('Something went wrong while attempting to use Yarn. Falling back to NPM');
363+
logger.info(e);
362364

363-
return npm.install([], npmOptions);
364-
});
365-
}
366-
} catch (e) {
367-
logger.error('Something went wrong while attempting to search for Yarn. Falling back to NPM');
368-
logger.info(e);
365+
return npm.install([], npmOptions);
366+
});
367+
} else if (packageManager === 'npm') {
368+
return npm.install([], npmOptions);
369369
}
370370

371-
return npm.install([], npmOptions);
371+
throw new Error(`Unknown package manager ${packageManager}`);
372372
}
373373

374374
function addDependencies(current, toAdd) {

lib/workflow/activities/project-install.js

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const os = require('os');
33
const UI = require('../../ui').UI;
44
const transform = require('../../colors/transform');
55
const createLines = require('../../string').createLines;
6+
const Yarn = require('../../package-managers/yarn').Yarn;
67
const CLIOptions = require('../../cli-options').CLIOptions;
78

89
module.exports = class {
@@ -15,22 +16,49 @@ module.exports = class {
1516

1617
execute(context) {
1718
let project = context.state.project;
19+
let options = [];
20+
21+
let yarn = new Yarn();
22+
if (yarn.isAvailable(project.getWorkingDirectory())) {
23+
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.');
24+
options = [
25+
{
26+
displayName: 'Yes, use Yarn',
27+
description: 'Installs all server, client and tooling dependencies needed to build the project using Yarn.',
28+
value: 'yarn'
29+
},
30+
{
31+
displayName: 'Yes, use NPM',
32+
description: 'Installs all server, client and tooling dependencies needed to build the project using NPM.',
33+
value: 'npm'
34+
},
35+
{
36+
displayName: 'No',
37+
description: 'Completes the new project wizard without installing dependencies.',
38+
value: 'no'
39+
}
40+
];
41+
} else {
42+
options = [
43+
{
44+
displayName: 'Yes',
45+
description: 'Installs all server, client and tooling dependencies needed to build the project.',
46+
value: 'yes'
47+
},
48+
{
49+
displayName: 'No',
50+
description: 'Completes the new project wizard without installing dependencies.',
51+
value: 'no'
52+
}
53+
];
54+
}
55+
56+
return this.ui.question('Would you like to install the project dependencies?', options).then(answer => {
57+
if (answer.value === 'yes' || answer.value === 'npm' || answer.value === 'yarn') {
58+
const packageManager = answer.value === 'yes' ? 'npm' : answer.value;
1859

19-
return this.ui.question('Would you like to install the project dependencies?', [
20-
{
21-
displayName: 'Yes',
22-
description: 'Installs all server, client and tooling dependencies needed to build the project.',
23-
value: 'yes'
24-
},
25-
{
26-
displayName: 'No',
27-
description: 'Completes the new project wizard without installing dependencies.',
28-
value: 'no'
29-
}
30-
]).then(answer => {
31-
if (answer.value === 'yes') {
3260
return this.ui.log(os.EOL + 'Installing project dependencies.')
33-
.then(() => project.install(this.ui))
61+
.then(() => project.install(this.ui, packageManager))
3462
.then(() => this.displayCompletionMessage(project))
3563
.then(() => context.next(this.nextActivity));
3664
}

0 commit comments

Comments
 (0)