appc-cli-titanium
Version:
Titanium CLI Plugin
104 lines (88 loc) • 2.63 kB
JavaScript
/**
* This code is closed source and Confidential and Proprietary to
* Appcelerator, Inc. All Rights Reserved. This code MUST not be
* modified, copied or otherwise redistributed without express
* written permission of Appcelerator. This file is licensed as
* part of the Appcelerator Platform and governed under the terms
* of the Appcelerator license agreement.
*/
var path = require('path'),
util = require('./util');
var TYPE = require('../appc').TYPE;
module.exports = {
type: TYPE,
name: 'Titanium Module Install',
execute: execute,
when: function (opts) {
opts = opts || {};
return opts.type === 'module';
}
};
function execute(appc, componentName, opts, callback) {
var modId = componentName[0],
modData,
sdkPath,
activeSDK;
appc.opts = opts;
appc.async.series([
function (cb) {
util.getInstalledTiSdkInfo(appc, function (err, tiInfo) {
if (err) {
return cb(err);
}
appc.log.debug('active sdk info', tiInfo);
sdkPath = tiInfo.sdkPath;
activeSDK = opts.sdk || tiInfo.activeSDK;
cb();
});
},
function (cb) {
util.listTiDownloads(appc, function (err, data) {
if (err) {
return cb(err);
}
var modules = data && data.modules || [];
modules.forEach(function (mod) {
if (mod.id === modId) {
modData = mod;
}
});
if (!modData) {
return cb(new Error(modId + ' is not available.'));
}
cb();
});
},
function (cb) {
var latestCompatibleMod = util.getCompatibleModuleDownloadInfo(activeSDK, modData);
if (!latestCompatibleMod) {
return cb(new Error('Unable to find ' + modData.id + ' compatible with SDK ', +activeSDK));
}
var latestToDownloadVersion = latestCompatibleMod && latestCompatibleMod.version;
var platforms = util.getNormalizedPlatforms(opts);
var isInstalled = true;
platforms.forEach(function (platform) {
var modulePath = path.join(sdkPath, 'modules', platform, modId);
var version = util.detectAddonVersion(appc, modulePath, modId, platform, activeSDK, latestToDownloadVersion);
if (!version) {
isInstalled = false;
}
});
if (isInstalled) {
appc.log.debug('Latest compatible module is already installed.');
return cb();
}
appc.log.debug('Install SDK compatible module ' + modData.id + ' version ' + latestToDownloadVersion);
modData.url = latestCompatibleMod.url;
modData.version = latestToDownloadVersion;
util.installTiModule(appc, modData, sdkPath, function () {
cb();
});
}
], function (err) {
if (err) {
return callback(err);
}
callback(null, componentName);
});
}