-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
install.js
159 lines (137 loc) · 4.34 KB
/
install.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
'use strict';
var _ = require('lodash');
var dargs = require('dargs');
var async = require('async');
var chalk = require('chalk');
var assert = require('assert');
/**
* @mixin
* @alias actions/install
*/
var install = module.exports;
/**
* Combine package manager cmd line arguments and run the `install` command.
*
* @param {String} installer Which package manager to use
* @param {String|Array} paths Packages to install, empty string for `npm install`
* @param {Object} options Options to invoke `install` with
* @param {Function} cb
*/
install.runInstall = function (installer, paths, options, cb) {
if (!cb && _.isFunction(options)) {
cb = options;
options = {};
}
options = options || {};
cb = cb || function () {};
paths = Array.isArray(paths) ? paths : (paths && paths.split(' ') || []);
this.emit(installer + 'Install', paths);
var args = ['install'].concat(paths).concat(dargs(options));
this.spawnCommand(installer, args, cb)
.on('error', cb)
.on('exit', this.emit.bind(this, installer + 'Install:end', paths))
.on('exit', function (err) {
if (err === 127) {
this.log.error('Could not find ' + installer + '. Please install with ' +
'`npm install -g ' + installer + '`.');
}
cb(err);
}.bind(this));
return this;
};
/**
* Runs `npm` and `bower` in the generated directory concurrently and prints a
* message to let the user know.
*
* ### Options:
*
* - `npm` Boolean whether to run `npm install` (`true`)
* - `bower` Boolean whether to run `bower install` (`true`)
* - `skipInstall` Boolean whether to skip automatic installation (`false`)
* - `skipMessage` Boolean whether to show the used bower/npm commands (`false`)
*
* ### Examples:
*
* this.installDependencies({
* bower: true,
* npm: true,
* skipInstall: false,
* callback: function () {
* console.log('Everything is ready!');
* }
* });
*
* @param {Object} options
*/
install.installDependencies = function (options) {
var msg = {
commands: [],
template: _.template('\n\nI\'m all done. ' +
'<%= skipInstall ? "Just run" : "Running" %> <%= commands %> ' +
'<%= skipInstall ? "" : "for you " %>to install the required dependencies.' +
'<% if (!skipInstall) { %> If this fails, try running the command yourself.<% } %>\n\n')
};
var commands = [];
if (_.isFunction(options)) {
options = {
callback: options
};
}
options = _.defaults(options || {}, {
bower: true,
npm: true,
skipInstall: false,
skipMessage: false,
callback: function () {}
});
if (options.bower) {
msg.commands.push('bower install');
commands.push(function (cb) {
this.env.runLoop.add('end', function (done) {
this.bowerInstall(null, null, function () {
done();
cb();
}, { once: 'installDependencies-bower' });
}.bind(this));
}.bind(this));
}
if (options.npm) {
msg.commands.push('npm install');
commands.push(function (cb) {
this.env.runLoop.add('end', function (done) {
this.npmInstall(null, null, function () {
done();
cb();
});
}.bind(this), { once: 'installDependencies-npm' });
}.bind(this));
}
assert(msg.commands.length, 'installDependencies needs at least one of npm or bower to run.');
if (options.skipInstall) {
return options.callback();
}
if (!options.skipMessage) {
this.env.adapter.log(msg.template(_.extend(options, { commands: chalk.yellow.bold(msg.commands.join(' & ')) })));
}
async.parallel(commands, options.callback);
};
/**
* Receives a list of `paths`, and an Hash of `options` to install through bower.
*
* @param {String|Array} paths Packages to install
* @param {Object} options Options to invoke `bower install` with, see `bower help install`
* @param {Function} cb
*/
install.bowerInstall = function install(paths, options, cb) {
return this.runInstall('bower', paths, options, cb);
};
/**
* Receives a list of `paths`, and an Hash of `options` to install through npm.
*
* @param {String|Array} paths Packages to install
* @param {Object} options Options to invoke `npm install` with, see `npm help install`
* @param {Function} cb
*/
install.npmInstall = function install(paths, options, cb) {
return this.runInstall('npm', paths, options, cb);
};