-
Notifications
You must be signed in to change notification settings - Fork 1
/
updater.js
executable file
·179 lines (166 loc) · 5.88 KB
/
updater.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env node
var nspAPI = require('nsp-api');
var fs = require('fs');
var npm = require("npm");
var request = require('request');
var colors = require('colors');
var cmpVer = require('compare-version');
var spawn = require('child_process').spawn;
var msg;
var semver = require('semver');
colors.setTheme({
prompt: 'cyan',
info: 'magenta',
success: 'green',
warn: 'yellow',
error: 'red'
});
function updateLib(libName, currentVer, versions) {
while (isNaN(currentVer[0])) {
currentVer = currentVer.substr(1);
}
var currentIndexInVers = versions.lastIndexOf(currentVer);
if (currentIndexInVers === -1) {
console.log(colors.error("Can not recognize version: " + currentVer));
return;
//version not 100% match, should find the closest instead
//currentIndexInVers = findRelatedVer(currentVer, versions);
}
var start = currentIndexInVers;
var end = versions.length - 1;
while (start !== end) {
start = end; //temporarily fake statement, should be replaced by the binary version search algorithm
}
var newVer = versions[start];
return newVer;
}
var pkgReadIn = fs.readFileSync("package.json", {encoding: 'utf8'});
var pkg = JSON.parse(pkgReadIn);
var depCount = Object.keys(pkg.dependencies).length;
msg = "Try to upgrade the dependencies of " + pkg.name + ' version ' + pkg.version + ' in safer method!\n';
console.log(msg.prompt);
msg = "Found " + depCount + (depCount ? ' dependencies :' : ' dependency');
console.log(msg.prompt);
var deps = [];
var depsVer = [];
function checkVer(lib, version) {
version = pkg.dependencies[lib];
request.get('http://registry.npmjs.org/' + lib, function (error, response, body) {
msg = "\nChecking verion of " + lib + ", current: ";
console.log(msg.prompt + colors.yellow(version));
if (!error && response.statusCode === 200) {
var versions = [],
temp = JSON.parse(body).versions,
ver;
for (ver in temp) {
versions.push(ver);
}
console.log("versions: ".prompt + colors.bold(versions));
console.log("The latest version: ".prompt + colors.bold(versions[versions.length - 1]));
var needUpdate = (cmpVer(versions[versions.length - 1], version) > 0 ? true : false);
console.log(colors.info((needUpdate ? colors.bold("Need") : "No need") + " to update!"));
if (needUpdate) {
var newVer = updateLib(lib, version, versions);
pkg.dependencies[lib] = newVer;
/* bad implement ... for prototype only npm install start */
var npmInstall = spawn('npm', ['install', lib + '@' + newVer, '--save']);
npmInstall.on('close', function (code) {
if (!code) {
var npmUpdate = spawn('npm', ['update', lib]);
npmUpdate.on('close', function (code2) {
if (!code2) {
msg = "Dependency - " + lib + " upgaded to v" + newVer + " successfully!";
} else {
msg = "Dependency - " + lib + " v" + newVer + " has been installed, but update process failed! " + code2;
}
console.log(colors.prompt(msg));
});
} else {
console.log(colors.error("Dependency - " + lib + " upgrade failed! exit code: " + code));
console.log(colors.error("This may not make sense 'cause this is just the final step, please open an issue on GitHub"));
}
});
/* the dirty part above should be replced by the confirmed and fixed version below */
/*npm.load(function() {
//not sure if npm api support this usage like npm command, need to be confirmed
npm.commands.install(lib + "@" + newVer, function(res){
console.log(res);
);
npm.commands.update([lib], function(res){
console.log(res);
})
})*/
}
}
});
}
function findRelatedVer(currentVer, versions) {
// cmpVer(currentVer,)
}
function findCompatibleVer(currentVer, versions) {
console.log("Competible Version: ".prompt + semver.maxSatisfying(versions, currentVer));
return semver.maxSatisfying(versions, currentVer);
}
//Should also handle devDependencies in future
if (depCount) {
var dep;
for (dep in pkg.dependencies) {
console.log(colors.info(' - ' + dep + ': ') + colors.bold(pkg.dependencies[dep]));
deps.push(dep);
depsVer.push(pkg.dependencies[dep]);
checkVer(dep, pkg.dependencies[dep]);
}
}
function testLibVersion(lib, version) {
npm.load("", function (er) {
var module = lib + "@" + version;
if (er) {
/* loading error */
console.log(er);
}
npm.commands.install([module], function (er, data) {
/*
* install module succeeded, then go testing.
* otherwise, alert error and do nothing.
*/
if (er) {
console.log(er);
} else {
npm.commands.test("package.json", function (er) {
/*
* if passed the test, write back to package.json.
* otherwise, reinstall by package.json.
*/
if (er) {
console.log(er);
/* (I'm not really sure about the situation,
* that when i testing another module, it will be possible unpassed
* test because of last problematic moduel.
*/
npm.commands.install([""]);
} else {
console.log("OK!");
// set install with --save
npm.config.set('save', true);
npm.commands.install([module]);
npm.config.set('save', false);
}
});
console.log(data);
}
});
});
}
function saferVersion(model, version, callback) {
nspAPI.validateModule(model, version, function (err, results) {
if (err) {
// An error generated from the underlying request.
console.log(err);
} else if (results.length !== 0) {
console.log("in func : %j", results);
return callback(false);
} else {
return callback(true);
}
});
}