Skip to content

Commit 4eccb2a

Browse files
BorjagodoySBoudrias
authored andcommittedNov 25, 2016
Add yarnInstall method
1 parent f632f01 commit 4eccb2a

File tree

2 files changed

+77
-13
lines changed

2 files changed

+77
-13
lines changed
 

‎lib/actions/install.js

+26-11
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,13 @@ install.runInstall = function (installer, paths, options, cb) {
8181
* @param {Object} [options]
8282
* @param {Boolean} [options.npm=true] - whether to run `npm install`
8383
* @param {Boolean} [options.bower=true] - whether to run `bower install`
84+
* @param {Boolean} [options.yarn=false] - whether to run `yarn install`
8485
* @param {Boolean} [options.skipMessage=false] - whether to log the used commands
8586
* @param {Function} [options.callback] - call once all commands have run
8687
*/
8788

8889
install.installDependencies = function (options) {
90+
options = options || {};
8991
var commands = [];
9092
var msg = {
9193
commands: [],
@@ -101,28 +103,28 @@ install.installDependencies = function (options) {
101103
};
102104
}
103105

104-
options = _.defaults(options || {}, {
105-
bower: true,
106-
npm: true,
107-
skipMessage: false,
108-
callback: function () {}
109-
});
110-
111-
if (options.npm) {
106+
if (options.npm !== false) {
112107
msg.commands.push('npm install');
113108
commands.push(function (cb) {
114109
this.npmInstall(null, null, cb);
115110
}.bind(this));
116111
}
117112

118-
if (options.bower) {
113+
if (options.yarn === true) {
114+
msg.commands.push('yarn install');
115+
commands.push(function (cb) {
116+
this.yarnInstall(null, null, cb);
117+
}.bind(this));
118+
}
119+
120+
if (options.bower !== false) {
119121
msg.commands.push('bower install');
120122
commands.push(function (cb) {
121123
this.bowerInstall(null, null, cb);
122124
}.bind(this));
123125
}
124126

125-
assert(msg.commands.length, 'installDependencies needs at least one of `npm` or `bower` to run.');
127+
assert(msg.commands.length, 'installDependencies needs at least one of `npm`, `bower` or `yarn` to run.');
126128

127129
if (!options.skipMessage) {
128130
var tplValues = _.extend({
@@ -133,7 +135,7 @@ install.installDependencies = function (options) {
133135
this.log(msg.template(tplValues));
134136
}
135137

136-
async.parallel(commands, options.callback);
138+
async.parallel(commands, options.callback || _.noop);
137139
};
138140

139141
/**
@@ -163,3 +165,16 @@ install.bowerInstall = function install(cmpnt, options, cb) {
163165
install.npmInstall = function install(pkgs, options, cb) {
164166
return this.runInstall('npm', pkgs, options, cb);
165167
};
168+
/**
169+
* Receives a list of `packages` and an `options` object to install through npm.
170+
*
171+
* The installation will automatically run during the run loop `install` phase.
172+
*
173+
* @param {String|Array} [pkgs] Packages to install
174+
* @param {Object} [options] Options to pass to `child_process.spawn` when invoking npm.
175+
* @param {Function} [cb]
176+
*/
177+
178+
install.yarnInstall = function install(pkgs, options, cb) {
179+
return this.runInstall('yarn', pkgs, options, cb);
180+
};

‎test/install.js

+51-2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ describe('generators.Base (actions/install mixin)', function () {
7070
}.bind(this));
7171
});
7272

73+
it('does not spawn anything with skipInstall', function (done) {
74+
this.dummy.runInstall('yarn', ['install']);
75+
this.dummy.run(function () {
76+
sinon.assert.notCalled(this.spawnCommandStub);
77+
done();
78+
}.bind(this));
79+
});
80+
7381
it('call callback if skipInstall', function (done) {
7482
var spy = sinon.spy();
7583
this.dummy.runInstall('npm', ['install'], spy);
@@ -78,6 +86,15 @@ describe('generators.Base (actions/install mixin)', function () {
7886
done();
7987
});
8088
});
89+
90+
it('call callback if skipInstall', function (done) {
91+
var spy = sinon.spy();
92+
this.dummy.runInstall('yarn', ['install'], spy);
93+
this.dummy.run(function () {
94+
sinon.assert.calledOnce(spy);
95+
done();
96+
});
97+
});
8198
});
8299
});
83100

@@ -135,6 +152,33 @@ describe('generators.Base (actions/install mixin)', function () {
135152
this.dummy.run();
136153
});
137154
});
155+
describe('#yarnInstall()', function () {
156+
it('spawn an install process once per commands', function (done) {
157+
this.dummy.yarnInstall();
158+
this.dummy.yarnInstall();
159+
this.dummy.run(function () {
160+
sinon.assert.calledOnce(this.spawnCommandStub);
161+
sinon.assert.calledWithExactly(this.spawnCommandStub, 'yarn', ['install'], {});
162+
done();
163+
}.bind(this));
164+
});
165+
166+
it('run without callback', function (done) {
167+
this.dummy.yarnInstall('yo', { save: true });
168+
this.dummy.run(function () {
169+
sinon.assert.calledOnce(this.spawnCommandStub);
170+
done();
171+
}.bind(this));
172+
});
173+
174+
it('run with callback', function (done) {
175+
this.dummy.yarnInstall('yo', { save: true }, function () {
176+
sinon.assert.calledOnce(this.spawnCommandStub);
177+
done();
178+
}.bind(this));
179+
this.dummy.run();
180+
});
181+
});
138182

139183
describe('#installDependencies()', function () {
140184
it('spawn npm and bower', function (done) {
@@ -152,8 +196,13 @@ describe('generators.Base (actions/install mixin)', function () {
152196
this.dummy.run();
153197
});
154198

155-
it('accept and execute a function as its only argument', function (done) {
156-
this.dummy.installDependencies(done);
199+
it('spawn yarn', function (done) {
200+
this.dummy.installDependencies({ yarn: true, npm: false, callback: function () {
201+
sinon.assert.calledTwice(this.spawnCommandStub);
202+
sinon.assert.calledWithExactly(this.spawnCommandStub, 'bower', ['install'], {});
203+
sinon.assert.calledWithExactly(this.spawnCommandStub, 'yarn', ['install'], {});
204+
done();
205+
}.bind(this)});
157206
this.dummy.run();
158207
});
159208
});

5 commit comments

Comments
 (5)

bmatto commented on Nov 29, 2016

@bmatto

This is stellar 💥 - any timeline on a release 😃 ?

mischah commented on Nov 29, 2016

@mischah
Member

Not a timeline though, but we are not far away: https://github.com/yeoman/generator/projects/2 😘

bmatto commented on Nov 29, 2016

@bmatto

Woah github projects - neato.

lucasbento commented on Sep 27, 2017

@lucasbento

What happens if the user doesn't have yarn installed? shouldn't it fallback to npm?

SBoudrias commented on Sep 28, 2017

@SBoudrias
Member

@lucasbento not at the moment, we're tracking this idea in #991 - help welcomed :)

Please sign in to comment.