forked from slushjs/gulp-install
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
165 lines (147 loc) · 3.51 KB
/
Copy pathindex.js
File metadata and controls
165 lines (147 loc) · 3.51 KB
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
160
161
162
163
164
165
'use strict';
var gutil = require('gulp-util');
var path = require("path");
var through2 = require('through2');
var commandRunner = require('./lib/commandRunner');
var cmdMap = {
'tsd.json': {
cmd: 'tsd',
args: ['reinstall', '--save']
},
'bower.json': {
cmd: 'bower',
args: ['install', '--config.interactive=false']
},
'package.json': {
cmd: 'npm',
args: ['install']
},
'requirements.txt': {
cmd: 'pip',
args: ['install', '-r', 'requirements.txt']
}
};
function log() {
if(process.env.NODE_ENV !== "test") {
gutil.log.apply(gutil, [].slice.call(arguments));
}
}
function formatCommands(cmds) {
return cmds.map(formatCommand).join(' && ');
}
function formatCommand(command) {
return command.cmd + ' ' + command.args.join(' ');
}
function formatArguments(args) {
if (Array.isArray(args)) {
args.forEach(function(arg, index, arr) {
arr[index] = formatArgument(arg);
});
return args;
} else if (typeof args === 'string' || args instanceof String) {
return [ formatArgument(args) ];
} else {
log('Arguments are in an invalid format: ' + args);
return [];
}
}
function formatArgument(arg) {
var result = arg;
while (!result.match(/--.*/)) {
result = '-' + result;
}
return result;
}
function skipInstall() {
return process.argv.indexOf('--skip-install') >= 0;
}
function clone(obj) {
if (Array.isArray(obj)) {
return obj.map(clone);
} else if (typeof obj === 'object') {
var copy = {};
Object.keys(obj).forEach(function(key) {
copy[key] = clone(obj[key]);
});
return copy;
} else {
return obj;
}
}
module.exports = exports = function install() {
var opts = null;
var callback = null;
for(var i = 0; i < arguments.length; i++) {
var arg = arguments[i];
var type = typeof(arg);
if(type === "function") {
callback = arg;
}
else if(type === "object") {
opts = arg;
}
}
var commands = [];
var captureCommands = function(file, enc, cb) {
var self = this;
if (!file.path) {
return cb();
}
var command = clone(cmdMap[path.basename(file.path)]);
if (command) {
if (opts && opts.production) {
command.args.push('--production');
}
if (opts && opts.ignoreScripts) {
command.args.push('--ignore-scripts');
}
if (opts && opts.args) {
formatArguments(opts.args).forEach(function(arg) {
command.args.push(arg);
});
}
if (command.cmd === 'bower' && opts && opts.allowRoot) {
command.args.push('--allow-root');
}
if (command.cmd === 'npm' && opts && opts.noOptional) {
command.args.push('--no-optional');
}
command.cwd = path.dirname(file.path);
commands.push(command);
}
runCommands(function() {
self.push(file);
cb();
});
}
var runCommands = function(cb) {
if (!commands.length) {
return;
}
if (skipInstall()) {
log('Skipping install.', 'Run `' + gutil.colors.yellow(formatCommands(commands)) + '` manually');
return;
}
else {
var i = 0;
var maxI = commands.length;
var next = function(err) {
if(err) {
callback(err);
}
else if(i < maxI) {
var command = commands[i++];
commandRunner.run(command, next);
}
else {
if(callback) {
callback();
}
cb();
}
}
next();
}
};
return through2.obj(captureCommands);
};