-
Notifications
You must be signed in to change notification settings - Fork 242
/
uninstall.js
361 lines (303 loc) · 14.3 KB
/
uninstall.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
353
354
355
356
357
358
359
360
361
/**
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
/* jshint laxcomma:true, sub:true, expr:true */
var path = require('path'),
fs = require('fs'),
semver = require('semver'),
shell= require('shelljs'),
action_stack = require('cordova-common').ActionStack,
dependencies = require('./util/dependencies'),
CordovaError = require('cordova-common').CordovaError,
underscore = require('underscore'),
Q = require('q'),
events = require('cordova-common').events,
platform_modules = require('../platforms/platforms'),
promiseutil = require('../util/promise-util'),
HooksRunner = require('../hooks/HooksRunner'),
cordovaUtil = require('../cordova/util'),
npmUninstall = require('cordova-fetch').uninstall;
var superspawn = require('cordova-common').superspawn;
var PlatformJson = require('cordova-common').PlatformJson;
var PluginInfoProvider = require('cordova-common').PluginInfoProvider;
// possible options: cli_variables, www_dir
// Returns a promise.
module.exports = uninstall;
function uninstall(platform, project_dir, id, plugins_dir, options) {
project_dir = cordovaUtil.convertToRealPathSafe(project_dir);
plugins_dir = cordovaUtil.convertToRealPathSafe(plugins_dir);
options = options || {};
options.is_top_level = true;
options.pluginInfoProvider = options.pluginInfoProvider || new PluginInfoProvider();
plugins_dir = plugins_dir || path.join(project_dir, 'cordova', 'plugins');
// Allow `id` to be a path to a file.
var xml_path = path.join(id, 'plugin.xml');
if ( fs.existsSync(xml_path) ) {
id = options.pluginInfoProvider.get(id).id;
}
return module.exports.uninstallPlatform(platform, project_dir, id, plugins_dir, options)
.then(function() {
return module.exports.uninstallPlugin(id, plugins_dir, options);
});
}
// Returns a promise.
module.exports.uninstallPlatform = function(platform, project_dir, id, plugins_dir, options) {
project_dir = cordovaUtil.convertToRealPathSafe(project_dir);
plugins_dir = cordovaUtil.convertToRealPathSafe(plugins_dir);
options = options || {};
options.is_top_level = true;
options.pluginInfoProvider = options.pluginInfoProvider || new PluginInfoProvider();
plugins_dir = plugins_dir || path.join(project_dir, 'cordova', 'plugins');
if (!platform_modules[platform]) {
return Q.reject(new CordovaError(platform + ' not supported.'));
}
var plugin_dir = path.join(plugins_dir, id);
if (!fs.existsSync(plugin_dir)) {
return Q.reject(new CordovaError('Plugin "' + id + '" not found. Already uninstalled?'));
}
var current_stack = new action_stack();
return Q().then(function() {
if (options.platformVersion) {
return Q(options.platformVersion);
}
return Q(superspawn.maybeSpawn(path.join(project_dir, 'cordova', 'version'), [], { chmod: true }));
}).then(function(platformVersion) {
options.platformVersion = platformVersion;
return runUninstallPlatform(current_stack, platform, project_dir, plugin_dir, plugins_dir, options);
});
};
// Returns a promise.
module.exports.uninstallPlugin = function(id, plugins_dir, options) {
plugins_dir = cordovaUtil.convertToRealPathSafe(plugins_dir);
options = options || {};
options.pluginInfoProvider = options.pluginInfoProvider || new PluginInfoProvider();
var pluginInfoProvider = options.pluginInfoProvider;
var plugin_dir = path.join(plugins_dir, id);
// @tests - important this event is checked spec/uninstall.spec.js
events.emit('log', 'Removing "'+ id +'"');
// If already removed, skip.
if ( !fs.existsSync(plugin_dir) ) {
events.emit('verbose', 'Plugin "'+ id +'" already removed ('+ plugin_dir +')');
return Q();
}
/*
* Deletes plugin from plugins directory and
* node_modules directory if --fetch was supplied.
*
* @param {String} id the id of the plugin being removed
*
* @return {Promise||Error} Returns a empty promise or a promise of doing the npm uninstall
*/
var doDelete = function(id) {
var plugin_dir = path.join(plugins_dir, id);
if ( !fs.existsSync(plugin_dir) ) {
events.emit('verbose', 'Plugin "'+ id +'" already removed ('+ plugin_dir +')');
return Q();
}
shell.rm('-rf', plugin_dir);
events.emit('verbose', 'Deleted "'+ id +'"');
if(options.fetch) {
//remove plugin from node_modules directory
return npmUninstall(id, options.projectRoot, options);
}
return Q();
};
// We've now lost the metadata for the plugins that have been uninstalled, so we can't use that info.
// Instead, we list all dependencies of the target plugin, and check the remaining metadata to see if
// anything depends on them, or if they're listed as top-level.
// If neither, they can be deleted.
var top_plugin_id = id;
// Recursively remove plugins which were installed as dependents (that are not top-level)
var toDelete = [];
function findDependencies(pluginId) {
var depPluginDir = path.join(plugin_dir, '..', pluginId);
// Skip plugin check for dependencies if it does not exist (CB-7846).
if (!fs.existsSync(depPluginDir) ) {
events.emit('verbose', 'Plugin "'+ pluginId +'" does not exist ('+ depPluginDir +')');
return;
}
var pluginInfo = pluginInfoProvider.get(depPluginDir);
// TODO: Should remove dependencies in a separate step, since dependencies depend on platform.
var deps = pluginInfo.getDependencies();
deps.forEach(function (d) {
if (toDelete.indexOf(d.id) === -1) {
toDelete.push(d.id);
findDependencies(d.id);
}
});
}
findDependencies(top_plugin_id);
toDelete.push(top_plugin_id);
// Okay, now we check if any of these are depended on, or top-level.
// Find the installed platforms by whether they have a metadata file.
var platforms = Object.keys(platform_modules).filter(function(platform) {
return fs.existsSync(path.join(plugins_dir, platform + '.json'));
});
// Can have missing plugins on some platforms when not supported..
var dependList = {};
platforms.forEach(function(platform) {
var platformJson = PlatformJson.load(plugins_dir, platform);
var depsInfo = dependencies.generateDependencyInfo(platformJson, plugins_dir, pluginInfoProvider);
var tlps = depsInfo.top_level_plugins;
var deps;
// Top-level deps must always be explicitely asked to remove by user
tlps.forEach(function(plugin_id){
if(top_plugin_id == plugin_id)
return;
var i = toDelete.indexOf(plugin_id);
if(i >= 0)
toDelete.splice(i, 1);
});
toDelete.forEach(function(plugin) {
deps = dependencies.dependents(plugin, depsInfo, platformJson, pluginInfoProvider);
var i = deps.indexOf(top_plugin_id);
if(i >= 0)
deps.splice(i, 1); // remove current/top-level plugin as blocking uninstall
if(deps.length) {
dependList[plugin] = deps.join(', ');
}
});
});
var i, plugin_id, msg, delArray = [];
for(i in toDelete) {
plugin_id = toDelete[i];
if( dependList[plugin_id] ) {
msg = '"' + plugin_id + '" is required by ('+ dependList[plugin_id] + ')';
if(options.force) {
events.emit('log', msg +' but forcing removal.');
} else {
// @tests - error and event message is checked spec/uninstall.spec.js
msg += ' and cannot be removed (hint: use -f or --force)';
if(plugin_id == top_plugin_id) {
return Q.reject( new CordovaError(msg) );
} else {
events.emit('warn', msg);
continue;
}
}
}
//create an array of promises
delArray.push(doDelete(plugin_id));
}
//return promise.all
return Q.all(delArray);
};
// possible options: cli_variables, www_dir, is_top_level
// Returns a promise
function runUninstallPlatform(actions, platform, project_dir, plugin_dir, plugins_dir, options) {
var pluginInfoProvider = options.pluginInfoProvider;
// If this plugin is not really installed, return (CB-7004).
if (!fs.existsSync(plugin_dir)) {
return Q(true);
}
var pluginInfo = pluginInfoProvider.get(plugin_dir);
var plugin_id = pluginInfo.id;
// Deps info can be passed recusively
var platformJson = PlatformJson.load(plugins_dir, platform);
var depsInfo = options.depsInfo || dependencies.generateDependencyInfo(platformJson, plugins_dir, pluginInfoProvider);
// Check that this plugin has no dependents.
var dependents = dependencies.dependents(plugin_id, depsInfo, platformJson, pluginInfoProvider);
if(options.is_top_level && dependents && dependents.length > 0) {
var msg = 'The plugin \'' + plugin_id + '\' is required by (' + dependents.join(', ') + ')';
if(options.force) {
events.emit('warn', msg + ' but forcing removal');
} else {
return Q.reject( new CordovaError(msg + ', skipping uninstallation. (try --force if trying to update)') );
}
}
// Check how many dangling dependencies this plugin has.
var deps = depsInfo.graph.getChain(plugin_id);
var danglers = dependencies.danglers(plugin_id, depsInfo, platformJson, pluginInfoProvider);
var promise;
if (deps && deps.length && danglers && danglers.length) {
// @tests - important this event is checked spec/uninstall.spec.js
events.emit('log', 'Uninstalling ' + danglers.length + ' dependent plugins.');
promise = promiseutil.Q_chainmap(danglers, function(dangler) {
var dependent_path = path.join(plugins_dir, dangler);
var opts = underscore.extend({}, options, {
is_top_level: depsInfo.top_level_plugins.indexOf(dangler) > -1,
depsInfo: depsInfo
});
return runUninstallPlatform(actions, platform, project_dir, dependent_path, plugins_dir, opts);
});
} else {
promise = Q();
}
var projectRoot = cordovaUtil.isCordova();
if(projectRoot) {
// using unified hooksRunner
var hooksRunnerOptions = {
cordova: { platforms: [ platform ] },
plugin: {
id: pluginInfo.id,
pluginInfo: pluginInfo,
platform: platform,
dir: plugin_dir
}
};
// CB-10708 This is the case when we're trying to uninstall plugin using plugman from specific
// platform inside of the existing CLI project. This option is usually set by cordova-lib for CLI projects
// but since we're running this code through plugman, we need to set it here implicitly
options.usePlatformWww = true;
var hooksRunner = new HooksRunner(projectRoot);
return promise.then(function() {
return hooksRunner.fire('before_plugin_uninstall', hooksRunnerOptions);
}).then(function() {
return handleUninstall(actions, platform, pluginInfo, project_dir, options.www_dir, plugins_dir, options.is_top_level, options);
});
} else {
// TODO: Need review here - this condition added for plugman install.spec.js and uninstall.spec.js passing -
// where should we get projectRoot - via going up from project_dir?
return handleUninstall(actions, platform, pluginInfo, project_dir, options.www_dir, plugins_dir, options.is_top_level, options);
}
}
// Returns a promise.
function handleUninstall(actions, platform, pluginInfo, project_dir, www_dir, plugins_dir, is_top_level, options) {
events.emit('log', 'Uninstalling ' + pluginInfo.id + ' from ' + platform);
// Set up platform to uninstall asset files/js modules
// from <platform>/platform_www dir instead of <platform>/www.
options.usePlatformWww = true;
return platform_modules.getPlatformApi(platform, project_dir)
.removePlugin(pluginInfo, options)
.then(function(result) {
// Remove plugin from installed list. This already done in platform,
// but need to be duplicated here to remove plugin entry from project's
// plugin list to manage dependencies properly.
PlatformJson.load(plugins_dir, platform)
.removePlugin(pluginInfo.id, is_top_level)
.save();
if (platform == 'android' &&
semver.gte(options.platformVersion, '4.0.0-dev') &&
// CB-10533 since 5.2.0-dev prepBuildFiles is now called internally by android platform and
// no more exported from build module
// TODO: This might be removed once we deprecate non-PlatformApi compatible platforms support
semver.lte(options.platformVersion, '5.2.0-dev') &&
pluginInfo.getFrameworks(platform).length > 0) {
events.emit('verbose', 'Updating build files since android plugin contained <framework>');
var buildModule;
try {
buildModule = require(path.join(project_dir, 'cordova', 'lib', 'build'));
} catch (e) {
// Should occur only in unit tests.
}
if (buildModule && buildModule.prepBuildFiles) {
buildModule.prepBuildFiles();
}
}
// CB-11022 propagate `removePlugin` result to the caller
return Q(result);
});
}