-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.js
352 lines (312 loc) · 10.6 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/bin/env node
/**********
* Modules
**********/
// Core
var path = require('path');
var exec = require('child_process').exec;
// lib
var logger = require('./lib/logger.js')();
var update = require('./lib/update.js')();
var errorHandler = require('./lib/errorHandler.js')();
var progress = require('./lib/progress.js')();
var cliArgs = require('./lib/cliArgs.js')().args;
var config = require('./lib/config.js')();
var packageReader = require('./lib/packageReader.js')();
var remote = require('./lib/remote.js')();
var local = require('./lib/local.js')();
// 3rd party
try{
var fs = require('fs-extra');
var colors = require('colors');
var jsonfile = require('jsonfile');
var _ = require('lodash');
var cordovaCommon = require('cordova-common');
var PluginInfoProvider = cordovaCommon.PluginInfoProvider;
var pluginInfoProvider = new PluginInfoProvider();
}catch(e){
errorHandler.handleFatalException(e, "Failed to acquire module dependencies");
}
/******************
* Global variables
******************/
var verbose = false;
var unconstrainVersions = false;
var updateMode = null;
var plugins = {};
var cliArgs,
pluginCount,
target,
cwd = '';
function start(){
cwd = path.resolve(cwd);
if(!fs.existsSync(cwd)) {
errorHandler.handleFatalError("The path specified with --cwd could not be resolved: " + cwd);
}
logger.verbose("cwd is: " + cwd);
local.cwd = cwd;
readJson();
}
function readJson(){
logger.verbose("Finding installed plugins");
progress.start("Checking local versions");
local.readFetchJson(function(err, json){
try{
if(err){
logger.warn( "Failed to read plugins/fetch.json - ensure you're running this command from the root of, or have specified the path using --cwd to, a valid Cordova project.\n\n"+err);
return getInstalledVersions();
}
pluginCount = 0;
for(var id in json){
var plugin = json[id];
plugins[id] = {
source: plugin['source'],
variables: plugin['variables']
};
pluginCount++;
}
getInstalledVersions();
}catch(e){
errorHandler.handleFatalException(e);
}
});
}
function getInstalledVersions(){
logger.verbose("Reading installed plugin versions");
var installedPlugins = pluginInfoProvider.getAllWithinSearchPath(path.resolve(cwd, local.PLUGINS_DIR));
installedPlugins.forEach(function(plugin){
if(plugins[plugin.id]){
plugins[plugin.id]['installed'] = plugin.version;
}else{
var msg = "Plugin '"+plugin.id+"' is present in /plugins folder but not in fetch.json";
logger.error(msg);
}
});
if(cliArgs["remove-all"]){
update.removeAll(installedPlugins, {
force: true,
nosave: cliArgs["nosave"],
cwd: cwd
});
}else{
getTargetVersions();
}
}
function getTargetVersions(){
var targetModule = target === "config" ? config : remote;
targetModule = target === "package" ? packageReader : targetModule;
targetModule.check({
plugins: plugins,
onFinish: displayResults,
pluginCount: pluginCount,
unconstrainVersions: unconstrainVersions,
cwd: cwd
});
}
function displayResults(target){
if ( !target) { target = 'config.xml'; }
var pluginsForUpdate = [];
// Up-to-date (verbose)
var equal = _.filter(plugins, function(plugin, id){
plugin.id = id;
return plugin.status === "equal";
});
if(equal.length > 0){
logger.log(getTitle("Up-to-date plugins").grey);
equal.forEach(function(plugin){
logger.log(getPluginSnippet(plugin.id, plugin.source, plugin.installed, plugin.target).grey);
});
}
// newer installed
var newerInstalled = _.filter(plugins, function(plugin, id){
plugin.id = id;
return plugin.status === "newer-installed";
});
if(newerInstalled.length > 0){
var title = target === "config" ? "config.xml version older than installed" : "Installed plugin version newer than remote default";
logger.log(getTitle(title).yellow);
newerInstalled.forEach(function(plugin){
logger.log(getPluginSnippet(plugin.id, plugin.source, plugin.installed, plugin.target).yellow);
});
if(target === "config" && cliArgs["allow-downdate"]){
pluginsForUpdate = pluginsForUpdate.concat(newerInstalled);
}
}
// new installed (config only)
var newInstalled = _.filter(plugins, function(plugin, id){
plugin.id = id;
return plugin.status === "new-installed";
});
if(newInstalled.length > 0){
logger.log(getTitle("Locally installed plugins not in " + target).yellow);
newInstalled.forEach(function(plugin){
logger.log(getPluginSnippet(plugin.id, plugin.source, plugin.installed, plugin.target).yellow);
});
}
// unknown mismatch
var unknown = _.filter(plugins, function(plugin, id){
plugin.id = id;
return plugin.status === "unknown-mismatch";
});
if(unknown.length > 0){
logger.log(getTitle("Unknown plugin version mismatch").yellow);
unknown.forEach(function(plugin){
logger.log(getPluginSnippet(plugin.id, plugin.source, plugin.installed, plugin.target).yellow);
});
}
// error
var error = _.filter(plugins, function(plugin, id){
plugin.id = id;
return plugin.status === "error";
});
if(error.length > 0){
logger.log(getTitle("Error checking plugin version").red);
error.forEach(function(plugin){
logger.log(getPluginSnippet(plugin.id, plugin.source, plugin.installed, plugin.target, plugin.error).red);
});
}
// newer target
var newerTarget = _.filter(plugins, function(plugin, id){
plugin.id = id;
return plugin.status === "newer-target";
});
if(newerTarget.length > 0){
var title = target === "config" ? "config.xml version newer than installed" : "Plugin update available";
logger.log(getTitle(title).green);
newerTarget.forEach(function(plugin){
logger.log(getPluginSnippet(plugin.id, plugin.source, plugin.installed, plugin.target).green);
});
pluginsForUpdate = pluginsForUpdate.concat(newerTarget);
}
// new target (config only)
var newTarget = _.filter(plugins, function(plugin, id){
plugin.id = id;
return plugin.status === "new-target";
});
if(newTarget.length > 0){
logger.log(getTitle("New plugins in config.xml").green);
newTarget.forEach(function(plugin){
logger.log(getPluginSnippet(plugin.id, plugin.source, plugin.installed, plugin.target).green);
});
pluginsForUpdate = pluginsForUpdate.concat(newTarget);
}
if(pluginsForUpdate.length > 0){
update.doUpdate(plugins, pluginsForUpdate, {
updateMode: updateMode,
unconstrainVersions: unconstrainVersions,
nosave: cliArgs["nosave"],
force: cliArgs["force"] || cliArgs["force-update"],
nofetch: cliArgs["nofetch"],
target: target,
allowDowndate: cliArgs["allow-downdate"],
cwd: cwd
});
}
}
function getTitle(msg){
var title = "";
var lineLength = msg.length+4;
function addLine(){
for(var i=0; i<lineLength; i++){
title += "*";
}
}
title += "\n";
addLine();
title += "\n";
title += "* "+msg+" *";
title += "\n";
addLine();
title += "\n";
return title;
}
function getPluginSnippet(id, source, installedVersion, targetVersion, error){
if(!source){
source = "N/A";
}else if(source.type === "git"){
source = remote.normalizeGithubSource(source);
}else if(source.type === "registry"){
if(source.id.match(remote.GITHUB_REGEX)){
source = remote.normalizeGithubSource(source);
}else{
source = "npm://"+source.id;
}
}else if(source.type === "local"){
source = source.path;
}else{
source = "UNKNOWN";
}
installedVersion = installedVersion ? installedVersion : "UNKNOWN";
targetVersion = targetVersion ? targetVersion : "UNKNOWN";
if(installedVersion === "UNKNOWN" && targetVersion !== "UNKNOWN"){
if(target === "config"){
installedVersion = "N/A";
}else{
installedVersion += " - check plugins/fetch.json for orphaned entries";
}
}else if(targetVersion === "UNKNOWN" && installedVersion !== "UNKNOWN"){
if(target === "config"){
targetVersion = "N/A";
}else{
targetVersion += " - check "+target+" source is valid";
}
}
if(targetVersion.match(remote.GITHUB_REGEX)){
targetVersion = remote.normalizeGithubURL(targetVersion);
}
var snippet = "plugin: "+id+
"\nsource: "+source+
"\ninstalled version: "+installedVersion+
"\n"+target+" version: "+targetVersion;
if(error){
snippet += "\nerror: "+error;
}
snippet += "\n";
return snippet;
}
function help(){
var helpText = fs.readFileSync(path.resolve(__dirname, 'usage.txt'), 'utf-8');
logger.log(helpText);
}
/***********
* Main
***********/
function run(){
try{
logger.verbose("Running cordova-check-plugins...");
if(cliArgs["v"] || cliArgs["version"]){
return logger.log(require('./package.json').version);
}
if(cliArgs["h"] || cliArgs["help"]){
return help();
}
if(cliArgs["obfuscate-credentials"]){
logger.setCredentialsToObfuscate(cliArgs["obfuscate-credentials"].split(' '));
}
if(cliArgs["verbose"]){
verbose = true;
logger.verbose("Verbose output enabled");
}
if(cliArgs["unconstrain-versions"]){
unconstrainVersions = true;
logger.verbose("Unconstraining version checks: highest remote version will be displayed regardless of locally specified version");
}
if(cliArgs["update"]){
updateMode = cliArgs["update"];
}
if(cliArgs["cwd"]){
cwd = cliArgs["cwd"];
}
if(cliArgs["nospinner"]){
progress.disable();
}
target = cliArgs["target"];
start();
}catch(e){
errorHandler.handleFatalException(e);
}
}
/*******************
* Module invocation
*******************/
run();