diff --git a/lib/commands/new/project-template.js b/lib/commands/new/project-template.js index 0ac004760..fb137f9a8 100644 --- a/lib/commands/new/project-template.js +++ b/lib/commands/new/project-template.js @@ -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'); @@ -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', diff --git a/lib/package-managers/yarn.js b/lib/package-managers/yarn.js index 9e55af244..72d89b3b6 100644 --- a/lib/package-managers/yarn.js +++ b/lib/package-managers/yarn.js @@ -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) { @@ -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; diff --git a/package.json b/package.json index ea8e42326..87e814299 100644 --- a/package.json +++ b/package.json @@ -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"