Skip to content

Commit

Permalink
feat(yarn): use yarn to install deps if possible
Browse files Browse the repository at this point in the history
closes #703
  • Loading branch information
JeroenVinke committed Aug 21, 2017
1 parent ac235bd commit 460887e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
6 changes: 6 additions & 0 deletions lib/commands/new/project-template.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
const ProjectItem = require('../../project-item').ProjectItem;
const NPM = require('../../package-managers/npm').NPM;
const Yarn = require('../../package-managers/yarn').Yarn;
const path = require('path');
const fs = require('../../file-system');
const string = require('../../string');
Expand Down Expand Up @@ -321,6 +322,11 @@ exports.ProjectTemplate = class {
};

function installDependencies(ui, workingDirectory, dependencies) {
let yarn = new Yarn();
if (yarn.isAvailable(workingDirectory)) {
return yarn.install([], { cwd: workingDirectory });
}

let npm = new NPM();
let npmOptions = {
loglevel: 'error',
Expand Down
34 changes: 29 additions & 5 deletions lib/package-managers/yarn.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
'use strict';

const childProcess = require('child_process');
const npmWhich = require('npm-which');

exports.yarn = class {
install(packages) {
exports.Yarn = class {
install(packages, options) {
return new Promise((resolve, reject) => {
let cmd = `yarn add ${packages.join(' ')}`;
let options = { cwd: process.cwd() };
if (!options) {
options = { cwd: process.cwd() };
}
let yarnPath = this.getYarnPath(options.cwd);
let cmd = `"${yarnPath}"`;

if (packages && packages.length > 0) {
cmd += ` add ${packages.join(' ')}`;
}

let installProcess = childProcess.exec(cmd, options, (error, stdout, stderr) => {
if (error) {
Expand All @@ -20,6 +28,22 @@ exports.yarn = class {
installProcess.stdout.on('data', data => console.log(data));
});
}

getYarnPath(directory) {
return getPathToExecutable('yarn', directory);
}

isAvailable(directory) {
return !!this.getYarnPath(directory);
}
};

exports.default = exports.yarn;
function getPathToExecutable(executableName, directory) {
try {
return npmWhich(directory).sync(executableName);
} catch (ex) {
return null;
}
}

exports.default = exports.Yarn;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"esprima": "^4.0.0",
"glob": "^7.1.1",
"npm": "^3.10.8",
"npm-which": "^3.0.1",
"preprocess": "^3.1.0",
"rfc6902": "^1.2.2",
"semver": "^5.3.0"
Expand Down

0 comments on commit 460887e

Please sign in to comment.