-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
161 lines (132 loc) · 3.99 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
151
152
153
154
155
156
157
158
159
160
161
'use strict';
const _ = require('lodash');
var exec = require('child_process').exec;
const notifier = require('node-notifier');
const semver = require('semver');
const moment = require('moment');
var fs = require('fs');
try {
var config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
} catch (err) {
throw Error('Please provide a valid config.json JSON file with a list of directories to monitor');
}
var statsInfoNpm = {};
var statsInfoBower = {};
// special cases.
function validateNpmPackageValue(value) {
if (!value ||
!value.current ||
value.current === 'MISSING' ||
value.current === 'git' ||
!value.latest ||
value.latest === 'MISSING' ||
value.latest === 'git'
) {
console.log ('WARNING: Unable to check npm package: ');
console.log (value);
return false;
}
return true;
}
// special cases.
function validateBowerPackageValue(value) {
if (!value ||
!value.update ||
!value.update.latest ||
!value.pkgMeta ||
!value.pkgMeta.version
) {
console.log ('WARNING: Unable to check bower package: ');
if (_.has(value, 'pkgMeta.name')) {
console.log (value.pkgMeta.name);
}
return false;
}
return true;
}
function checkNpmPackages(data, monitoredDir) {
if (!statsInfoNpm[monitoredDir]) {
statsInfoNpm[monitoredDir] = 0;
}
statsInfoNpm[monitoredDir]++;
console.log(moment().format() + ': Check # ' + statsInfoNpm[monitoredDir] + ' npm packages for ' + monitoredDir);
_.each(data, function(value, key, info) {
if (!validateNpmPackageValue(value))
{
return;
}
if (semver.gt(value.latest, value.current)) {
console.log('Time to Upgrade NPM Package ' + key + ' from ' + value.current + ' to ' + value.latest + ' in ' + monitoredDir);
notifier.notify({
'title': 'Time to Upgrade NPM Package' + key,
'message': 'From ' + value.current + ' to ' + value.latest + '\n' + monitoredDir
});
}
});
}
function checkBowerPackages(data, monitoredDir) {
if (!statsInfoBower[monitoredDir]) {
statsInfoBower[monitoredDir] = 0;
}
statsInfoBower[monitoredDir]++;
console.log(moment().format() + ': Check # ' + statsInfoBower[monitoredDir] + ' bower packages for ' + monitoredDir);
_.each(data.dependencies, function(value, key, info) {
if (!validateBowerPackageValue(value))
{
return;
}
var latest = value.update.latest;
var current = value.pkgMeta.version;
if (semver.gt(latest, current)) {
console.log('Time to Upgrade Bower Package ' + key + ' from ' + current + ' to ' + latest + ' in ' + monitoredDir);
notifier.notify({
'title': 'Time to Upgrade Bower Package ' + key,
'message': 'From ' + current + ' to ' + latest + '\n' + monitoredDir
});
}
});
}
setInterval(function() {
_.each(config.monitoredDirs, function(monitoredDir) {
exec('npm outdated --json', {
cwd: monitoredDir,
maxBuffer: 1024 * 10000
}, function(error, stdout, stderr) {
//console.log('stdout: ' + stdout);
//console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
return;
}
if (!stdout) {
return;
}
try {
var parsed = JSON.parse(stdout);
//console.log(JSON.stringify(parsed, null, 4));
checkNpmPackages(JSON.parse(stdout), monitoredDir);
} catch (err) {
console.log(err);
}
});
exec('bower list --json', {
cwd: monitoredDir,
maxBuffer: 1024 * 10000
}, function(error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
return;
}
if (!stdout) {
return;
}
try {
var parsed = JSON.parse(stdout);
//console.log(JSON.stringify(parsed, null, 4));
checkBowerPackages(JSON.parse(stdout), monitoredDir);
} catch (err) {
console.log(err);
}
});
});
}, config.interval);