-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
150 lines (118 loc) · 3.48 KB
/
index.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
'use strict';
var _ = require('underscore'),
through2 = require('through2'),
fs = require('fs'),
hdiff = require('hdiff'),
spawn = require('child_process').spawn,
which = require('which'),
gutil = require('gulp-util'),
path = require('path');
module.exports = function () {
var diffMapper = jsonDiffMapper();
return through2(
{objectMode: true},
function (file, enc, cb) {
log('Checking ' + file.path + 'for updates');
execCommands(_.map(_.filter(mapDiffToCommands(diffMapper(file)), filterEmptyCommands), commandToCurrentCwdMapper(path.dirname(file.path))), cb);
this.push(file);
}
)
}
function log() {
return gutil.log.apply(gutil, [].slice.call(arguments));
}
function filterEmptyCommands(cmd) {
return !!cmd.length;
}
function execCommands(cmds, cb) {
if (!cmds.length) return cb();
var cmd = cmds.shift();
which(cmd[0], function (err, cmdpath) {
if (err) {
log("Couldn't find the path to " + cmd[0]);
return cb(err);
}
log(cmd[0] + ' ' + cmd[1].join(' '));
cmd[0] = cmdpath;
cmd[2].stdio = 'inherit';
cmd = spawn.apply(spawn, cmd);
cmd.on('close', function onClose() { return execCommands(cmds, cb); })
})
}
function jsonDiffMapper() {
var cache = {};
return function (file) {
var oldJson = cache[file.path];
var newJson = JSON.parse(fs.readFileSync(file.path, {encoding: 'utf8'}));
cache[file.path] = newJson;
if (!oldJson)
return {'$new': newJson}
return hdiff(oldJson, newJson, {unchanged: false});
}
}
function commandToCurrentCwdMapper(cwd) {
return function (cmd) {
if (!cmd) return;
cmd[2] = cmd[2] || {};
cmd[2].cwd = cwd;
return cmd;
}
}
function mapDiffToCommands(diff) {
if (!diff) return [];
var mappers = [mapInstallToCmd, mapRemoveToCmd, mapUpdateToCmd];
return _.union(
mapNewToCmds(diff),
mapDependenciesToCmds(diff.dependencies, mappers),
_.map(mapDependenciesToCmds(diff.devDependencies, mappers), mapDevCmds)
);
}
function mapNewToCmds(diff) {
if (!diff['$new']) return [];
return [createCommand('npm',['install'])];
}
function mapDependenciesToCmds(deps, mappers) {
if (!_.isObject(deps)) return [];
var cmds = [];
_.each(mappers,function(mapper) {
cmds.push(_mapDependenciesToCmds(deps, mapper));
})
return _.union.apply(_, cmds);
}
function _mapDependenciesToCmds(deps, mapper) {
if (!_.isObject(deps)) return [];
var cmds = [];
_.each(deps,function(diff, pkg) {
var cmd = mapper(diff, pkg);
if (cmd)
cmds.push(cmd);
})
return cmds;
}
function mapRemoveToCmd(diff, pkg) {
if (!isRemove(diff)) return;
return createCommand('npm',['remove', pkg]);
}
function mapInstallToCmd(diff, pkg) {
if (!isInstall(diff)) return [];
return createCommand('npm',['install', resolveFullPackageName(diff, pkg)]);
}
function mapUpdateToCmd(diff, pkg) {
if (!isUpdate(diff)) return [];
return createCommand('npm',['install', resolveFullPackageName(diff, pkg)]);
}
function mapDevCmds(cmd) {
if (cmd.length)
cmd[1].push('--save-dev');
return cmd;
}
function isUpdate(diff){ return diff['$add'] && diff['$del']; }
function isInstall(diff){ return diff['$add'] && !diff['$del']; }
function isRemove(diff){ return !diff['$add'] && diff['$del']; }
function resolveFullPackageName(diff, pkg) {
if (!diff['$add']) return pkg;
return pkg + '@' + diff['$add'];
}
function createCommand(cmd, args, opts) {
return [cmd, args, opts || {}];
}