/
timodule.js
561 lines (502 loc) · 20.1 KB
/
timodule.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/**
* Detects installed Titanium modules.
*
* @module timodule
*
* @copyright
* Copyright (c) 2009-2016 by Appcelerator, Inc. All Rights Reserved.
*
* @license
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
'use strict';
const
__ = require('./i18n')(__dirname).__,
afs = require('./fs'),
async = require('async'),
fs = require('fs'),
path = require('path'),
util = require('./util'),
version = require('./version'),
zip = require('./zip'),
platformAliases = {
// add additional aliases here for new platforms
ipad: 'iphone',
ios: 'iphone'
};
let moduleCache = {};
exports.scopedDetect = scopedDetect;
exports.detect = detect;
exports.find = find;
/**
* Scans search paths for Titanium modules. This function will not scan any paths
* other than the ones explicitly told to scan.
*
* @param {Object} searchPaths - An object of scopes to arrays of paths to search for Titanium modules.
* @param {Object} config - The CLI config.
* @param {Object} logger - A logger instance.
* @param {Function} callback - A function to call when done.
* @param {Boolean} [bypassCache=false] - When true, re-scans the specified paths for modules.
*/
function scopedDetect(searchPaths, config, logger, callback, bypassCache) {
if (!searchPaths || typeof searchPaths !== 'object') {
callback();
return;
}
const tasks = [],
results = {};
Object.keys(searchPaths).forEach(function (scope) {
(Array.isArray(searchPaths[scope]) ? searchPaths[scope] : [ searchPaths[scope] ]).forEach(function (searchPath) {
if (!searchPath) {
return;
}
tasks.push(function (cb) {
detectModules({
bypassCache: bypassCache,
modulesDir: searchPath,
logger: logger,
callback: function (err, result) {
err || (results[scope] = result);
cb();
}
});
});
});
});
async.parallel(tasks, function () {
callback(results);
});
}
/**
* Scans search paths for Titanium modules. This function will scan all known
* Titanium SDK locations.
*
* @param {Object|string[]} paramsOrSearchPaths - An object with the following params; or an array of paths to search for Titanium modules.
* @param {Array<String>} [paramsOrSearchPaths.searchPaths] - An array of paths to search for Titanium modules.
* @param {Object} [paramsOrSearchPaths.logger] - A logger instance.
* @param {Function} [paramsOrSearchPaths.callback] - A function to call when done.
* @param {Boolean} [paramsOrSearchPaths.bypassCache=false] - When true, re-scans the specified paths for modules.
* @param {Object} [logger] - A logger instance.
* @param {Function} [callback] - A function to call when done.
* @param {Boolean} [bypassCache=false] - When true, re-scans the specified paths for modules.
*/
function detect(paramsOrSearchPaths, logger, callback, bypassCache) {
let params;
if (arguments.length === 1 && typeof paramsOrSearchPaths === 'object' && paramsOrSearchPaths !== null) {
params = paramsOrSearchPaths;
} else {
params = {
bypassCache: bypassCache,
callback: callback,
logger: logger,
searchPaths: paramsOrSearchPaths
};
}
const sdkPaths = [].concat(require('./environ').os.sdkPaths);
let i = sdkPaths.length - 1;
// resolve all sdk paths
while (i--) {
sdkPaths[i] = afs.resolvePath(sdkPaths[i]);
}
// non-destructively, but deeply mix two objects
function mix(src, dest) {
if (!src || !dest) {
return;
}
Object.keys(src).forEach(function (key) {
if (!dest[key] || typeof dest[key] !== 'object') {
dest[key] = {};
}
if (src[key] !== null && typeof src[key] === 'object' && !Array.isArray(src[key])) {
mix(src[key], dest[key]);
} else {
dest[key] = src[key];
}
});
}
async.parallel({
project: function (next) {
// resolve all search paths, but also remove a search path if it's already in the sdk paths
const searchPaths = {},
results = {};
(Array.isArray(params.searchPaths) ? params.searchPaths : [ params.searchPaths ]).forEach(function (p) {
if (!p || searchPaths[p]) {
return;
}
p = afs.resolvePath(p);
if (sdkPaths.indexOf(p) !== -1) {
return;
}
searchPaths[p] = 1;
});
async.each(Object.keys(searchPaths), function (modulePath, cb) {
detectModules({
bypassCache: params.bypassCache,
modulesDir: path.join(modulePath, 'modules'),
logger: params.logger,
callback: function (err, result) {
err || mix(result, results);
cb();
}
});
}, function () {
next(null, results);
});
},
global: function (next) {
const results = {};
async.each(sdkPaths, function (modulePath, cb) {
detectModules({
bypassCache: params.bypassCache,
modulesDir: path.join(modulePath, 'modules'),
logger: params.logger,
callback: function (err, result) {
err || mix(result, results);
cb();
}
});
}, function () {
next(null, results);
});
}
}, function (err, results) {
typeof params.callback === 'function' && params.callback(results);
});
}
/**
* Detects all installed Titanium modules, then it will validate that the
* specified modules are found, incompatible, missing, or conflicting.
*
* @param {Array<Object>|Object} modulesOrParams - An object with the following params; or An array of modules to search for
* @param {Array<Object>|Object} [modulesOrParams.modules] - An array of modules to search for.
* @param {Array<String>|String} [modulesOrParams.platforms] - An array of platform names (if the platform has more than one name) or a string of comma-separated platform names.
* @param {Array<String>|String} [modulesOrParams.deployType] - An array of deploy types or a string of comma-separated deploy types to filter by.
* @param {Object} [modulesOrParams.tiManifest] - The Titanium SDK manifest data.
* @param {Array<String>} [modulesOrParams.searchPaths] - An array of paths to search for Titanium modules.
* @param {Object} [modulesOrParams.logger] - A logger instance.
* @param {Function} [modulesOrParams.callback] - A function to call when done.
* @param {Boolean} [modulesOrParams.bypassCache=false] - When true, re-detects all modules.
* @param {Array<String>|String} [platforms] - An array of platform names (if the platform has more than one name) or a string of comma-separated platform names.
* @param {Array<String>|String} [deployType] - An array of deploy types or a string of comma-separated deploy types to filter by.
* @param {Object} [tiManifest] - The Titanium SDK manifest data.
* @param {Array<String>} [searchPaths] - An array of paths to search for Titanium modules.
* @param {Object} [logger] - A logger instance.
* @param {Function} [callback] - A function to call when done.
* @param {Boolean} [bypassCache=false] - When true, re-detects all modules.
*/
function find(modulesOrParams, platforms, deployType, tiManifest, searchPaths, logger, callback, bypassCache) {
const result = {
found: [],
missing: [],
incompatible: [],
conflict: []
},
visited = {},
modulesById = {};
let sdkVersion,
moduleAPIVersion;
if (tiManifest && typeof tiManifest === 'object') {
sdkVersion = tiManifest.version;
moduleAPIVersion = tiManifest.moduleAPIVersion;
} else {
sdkVersion = tiManifest;
}
let params;
if (arguments.length === 1 && typeof modulesOrParams === 'object' && modulesOrParams !== null) {
params = modulesOrParams;
} else {
params = {
bypassCache: bypassCache,
callback: callback,
deployType: deployType,
logger: logger,
modules: modulesOrParams,
platforms: platforms,
sdkVersion: sdkVersion,
searchPaths: searchPaths
};
}
// clean up platforms
if (typeof params.platforms === 'string') {
params.platforms = params.platforms.split(',').filter(function (p) {
return p;
});
} else if (Array.isArray(params.platforms)) {
params.platforms = params.platforms.filter(function (p) {
return p;
});
} else {
params.platforms = [];
}
params.platforms.indexOf('commonjs') === -1 && params.platforms.push('commonjs'); // add commonjs to the list of valid module platforms
detect({
searchPaths: params.searchPaths,
bypassCache: params.bypassCache,
logger: params.logger,
callback: function (installed) {
params.modules && params.modules.forEach(function (module) {
const originalVersion = module.version || 'latest',
scopes = [ 'project', 'global' ];
// make sure the module has a valid array of platforms
module.platform || (module.platform = params.platforms);
Array.isArray(module.platform) || (module.platform = module.platform.split(','));
module.deployType || (module.deployType = params.deployType);
Array.isArray(module.deployType) || (module.deployType = module.deployType.split(','));
// if this module doesn't support any of the platforms we're building for, skip it
if (module.deployType.indexOf(params.deployType) === -1
|| !module.platform.some(function (a) {
return params.platforms.indexOf(a) !== -1;
})) {
return;
}
// strip all platforms that aren't supported by this build
for (let i = 0; i < module.platform.length; i++) {
if (params.platforms.indexOf(module.platform[i]) === -1) {
module.platform.splice(i--, 1);
} else if (platformAliases[module.platform[i]] && module.platform.indexOf(platformAliases[module.platform[i]]) === -1) {
module.platform.push(platformAliases[module.platform[i]]);
}
}
const key = module.id + '|' + module.deployType.join(',') + '|' + module.platform.join(',') + '|' + module.version;
if (visited[key]) {
return;
}
visited[key] = 1;
params.logger && params.logger.debug(__('Looking for Titanium module id=%s version=%s platform=%s deploy-type=%s', module.id.cyan, originalVersion.cyan, module.platform.join(',').cyan, module.deployType.join(',').cyan));
// loop through each scope (project, global)
let foundIncompatible, found;
for (let i = 0; i < scopes.length; i++) {
const scope = installed[scopes[i]];
if (!scope) {
continue;
}
// loop through each platform attribute from <module platform="ios,android">
for (let j = 0; j < module.platform.length; j++) {
const platform = module.platform[j];
// check that we even have a module with the specified id and platform
if (!scope[platform] || !scope[platform][module.id]) {
continue;
}
// sort all versions
Object.keys(scope[platform][module.id]).sort().reverse().filter(function (ver) {
return !module.version || ver === module.version;
}).forEach(function (ver) { // eslint-disable-line no-loop-func
const info = scope[platform][module.id][ver];
if (!info) {
return;
}
const tmp = util.mix({}, module, info);
if (params.sdkVersion && info.manifest && info.manifest.minsdk && version.gt(info.manifest.minsdk, params.sdkVersion)) {
if (params.logger) {
params.logger.debug(__('Found incompatible Titanium module id=%s version=%s platform=%s deploy-type=%s', tmp.id.cyan, tmp.version.cyan, tmp.platform.join(',').cyan, tmp.deployType.join(',').cyan));
params.logger.debug(__('Module %s requires Titanium SDK %s or newer, but the selected SDK is %s', tmp.id.cyan, info.manifest.minsdk, params.sdkVersion));
}
result.incompatible.push(tmp);
return;
}
if (moduleAPIVersion && moduleAPIVersion[platform] && info.manifest && info.manifest.apiversion && info.manifest.apiversion !== moduleAPIVersion[platform]) {
if (params.logger) {
params.logger.debug(__('Found incompatible Titanium module id=%s version=%s platform=%s api-version=%s deploy-type=%s', tmp.id.cyan, tmp.version.cyan, tmp.platform.join(',').cyan, String(info.manifest.apiversion).cyan, tmp.deployType.join(',').cyan));
params.logger.debug(__('Module %s has apiversion=%s, but the selected SDK supports module apiversion=%s on platform=%s', tmp.id.cyan, info.manifest.apiversion.cyan, moduleAPIVersion[platform].cyan, platform.cyan));
}
result.incompatible.push(tmp);
foundIncompatible = true;
return;
}
// make sure we haven't already added this module
let alreadyAdded = false,
foundBetter = false,
addToModuleMap = true;
for (let k = 0; k < result.found.length; k++) {
if (result.found[k].id === tmp.id) {
// if we find a the same module twice, but the versions differ
if (originalVersion === 'latest') {
if (version.lt(result.found[k].version, ver)) {
// found a better module
params.logger && params.logger.info(__('Found better matching module id=%s version=%s platform=%s deploy-type=%s path=%s', tmp.id.cyan, originalVersion.cyan, tmp.platform.join(',').cyan, tmp.deployType.join(',').cyan, tmp.modulePath.cyan));
result.found.splice(k, 1);
foundBetter = true;
} else if (version.eq(result.found[k].version, ver)) {
alreadyAdded = true;
if (result.found[k].platform.map(function (p) { return platformAliases[p] || p; }).indexOf(platformAliases[platform] || platform) !== -1) { // eslint-disable-line max-statements-per-line
addToModuleMap = false;
}
} else {
alreadyAdded = true;
}
} else if (version.eq(result.found[k].version, ver)) {
alreadyAdded = true;
if (result.found[k].platform.indexOf(platformAliases[platform] || platform) !== -1) {
addToModuleMap = false;
}
}
}
}
if (!alreadyAdded) {
tmp.platform = [ platform ];
!foundBetter && params.logger && params.logger.info(__('Found Titanium module id=%s version=%s platform=%s deploy-type=%s path=%s', tmp.id.cyan, tmp.version.cyan, tmp.platform.join(',').cyan, tmp.deployType.join(',').cyan, tmp.modulePath.cyan));
result.found.push(tmp);
}
if (addToModuleMap) {
// add this module to a hash so we can check later for conflicts
modulesById[module.id] || (modulesById[module.id] = []);
modulesById[module.id].push(tmp);
}
found = true;
});
}
}
if (!found) {
params.logger && params.logger.warn(__('Could not find a valid Titanium module id=%s version=%s platform=%s deploy-type=%s', module.id.cyan, originalVersion.cyan, module.platform.join(',').cyan, module.deployType.join(',').cyan));
// don't add to missing when the module is already in the incompatible list
!foundIncompatible && result.missing.push(module);
} else {
// since we found a valid version, remove this module if was previously detected as incompatible
// this happens when module version is 'latest', we iterated through the list of versions and found a compatible one
// but subsequent versions are added to the incompatible list
for (let x = 0; x < result.incompatible.length; x++) {
if (result.incompatible[x].id === module.id) {
result.incompatible.splice(x--, 1);
}
}
}
});
// detect conflicts
Object.keys(modulesById).forEach(function (id) {
const mods = modulesById[id],
len = mods.length;
if (len <= 1) {
return;
}
let commonJs = 0,
nonCommonJs = 0;
// we have a potential conflict...
// verify that we have at least one commonjs platform and at least one non-commonjs platform
for (let i = 0; i < len; i++) {
platforms = Array.isArray(mods[i].platform) ? mods[i].platform : [ mods[i].platform ];
platforms.forEach(function (p) { // eslint-disable-line no-loop-func
if (p.toLowerCase() === 'commonjs') {
commonJs++;
} else {
nonCommonJs++;
}
});
}
if (commonJs && nonCommonJs) {
result.conflict.push({
id: id,
modules: mods
});
// remove from found
for (let i = 0; i < result.found.length; i++) {
if (result.found[i].id === id) {
result.found.splice(i--, 1);
}
}
}
});
typeof params.callback === 'function' && params.callback(result);
}
});
}
/**
* Searches an array of paths for Titanium modules. If it encounters a zip file
* that matches module zip filename pattern, it will automatically unzip it and
* remove the zip file prior to detecting modules.
*
* @param {Object} params - An object with the following params.
* @param {Array<String>} params.modulesDir - An array of paths to search for Titanium modules.
* @param {Boolean} [params.bypassCache=false] - When true, re-scans the specified path for modules.
* @param {Function} [params.callback] - A function to call when done.
* @param {Object} [params.config] - The CLI config.
* @param {Object} [params.logger] - A logger instance.
* @returns {void}
* @private
*/
function detectModules(params) {
const modulesDir = params.modulesDir,
callback = typeof params.callback === 'function' ? params.callback : function () {};
// make sure they specified a modulesDir
if (!modulesDir) {
return callback(new Error(__('Missing required argument "%s"', 'modulesDir')));
}
if (moduleCache[modulesDir] && !params.bypassCache) {
return callback(null, moduleCache[modulesDir]);
}
const results = {},
zipRegExp = /^.+-.+?-.+?\.zip$/,
osNamesRegExp = /^osx|win32|linux$/,
ignoreDirs = new RegExp(params.config && params.config.get('cli.ignoreDirs') || '^(.svn|.git|.hg|.?[Cc][Vv][Ss]|.bzr)$'), // eslint-disable-line security/detect-non-literal-regexp
moduleRoot = path.resolve(params.modulesDir, '..');
// make sure the module's parent dir (the root) exists
if (!fs.existsSync(moduleRoot)) {
return callback(null, results);
}
// auto-install zipped modules
async.each(fs.readdirSync(moduleRoot), function (name, next) {
const file = path.join(moduleRoot, name);
if (!zipRegExp.test(name) || !fs.existsSync(file) || !fs.statSync(file).isFile()) {
return next();
}
params.logger && params.logger.info(__('Installing module: %s', name));
zip.unzip(file, moduleRoot, null, function (err) {
if (err) {
params.logger && params.logger.error(__('Failed to unzip module "%s"', file));
} else {
fs.unlinkSync(file);
}
next();
});
}, function () {
if (!fs.existsSync(modulesDir)) {
return callback(null, results);
}
params.logger && params.logger.debug(__('Detecting modules in %s', modulesDir.cyan));
// loop through platforms
fs.readdirSync(modulesDir).forEach(function (platform) {
const modulesPath = path.join(modulesDir, platform);
if (fs.existsSync(modulesPath) && fs.statSync(modulesPath).isDirectory() && !osNamesRegExp.test(platform) && !ignoreDirs.test(platform)) {
// loop through module names
fs.readdirSync(modulesPath).forEach(function (moduleName) {
const modulePath = path.join(modulesPath, moduleName);
if (fs.existsSync(modulePath) && fs.statSync(modulePath).isDirectory() && !ignoreDirs.test(moduleName)) {
// loop through versions
fs.readdirSync(modulePath).forEach(function (ver) {
if (ignoreDirs.test(ver)) {
return;
}
const versionPath = path.join(modulePath, ver),
manifestFile = path.join(versionPath, 'manifest');
if (fs.existsSync(versionPath) && fs.statSync(versionPath).isDirectory() && fs.existsSync(manifestFile)) {
const dest = results[platform] || (results[platform] = {}),
mod = dest[moduleName] || (dest[moduleName] = {});
if (!mod[ver]) {
mod[ver] = {
modulePath: versionPath,
version: ver,
manifest: {}
};
fs.readFileSync(manifestFile).toString().split('\n').forEach(function (line) {
const p = line.indexOf(':');
if (line.charAt(0) !== '#' && p !== -1) {
mod[ver].manifest[line.substring(0, p)] = line.substring(p + 1).trim();
}
});
if (mod[ver].manifest && mod[ver].manifest.platform) {
mod[ver].platform = [ mod[ver].manifest.platform ];
if (mod[ver].manifest.platform === 'iphone') {
mod[ver].platform.unshift('ios');
}
}
params.logger && params.logger.debug(__('Detected %s module: %s %s @ %s', platform, mod[ver].manifest.moduleid.cyan, ver, mod[ver].modulePath));
}
}
});
}
});
}
});
callback(null, moduleCache[modulesDir] = results);
});
}