@hkvstore/taco-cli
Version:
taco-cli is a command-line interface for rapid Apache Cordova development (forked from Microsoft taco-cli)
252 lines (250 loc) • 11.4 kB
JavaScript
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
/// <reference path="../../../typings/node.d.ts" />
/// <reference path="../../../typings/Q.d.ts" />
/// <reference path="../../../typings/semver.d.ts" />
/// <reference path="../../../typings/tacoKits.d.ts" />
;
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var Q = require("q");
var CliTelemetryHelper = require("./cliTelemetryHelper");
var TacoErrorCodes = require("../tacoErrorCodes");
var errorHelper = require("../tacoErrorHelper");
var tacoUtility = require("taco-utils");
var commands = tacoUtility.Commands;
var packageLoader = tacoUtility.TacoPackageLoader;
var ProjectHelper = tacoUtility.ProjectHelper;
var PromiseUtils = tacoUtility.PromisesUtils;
var telemetryHelper = tacoUtility.TelemetryHelper;
;
;
/**
* KitComponentCommand
*
* Base class to handle add/remove operations for cordova components (platforms, plugins)
*/
var KitComponentCommand = (function (_super) {
__extends(KitComponentCommand, _super);
function KitComponentCommand() {
_super.apply(this, arguments);
}
Object.defineProperty(KitComponentCommand.prototype, "knownOptions", {
get: function () {
throw errorHelper.get(TacoErrorCodes.UnimplementedAbstractMethod);
},
enumerable: true,
configurable: true
});
Object.defineProperty(KitComponentCommand.prototype, "knownSubCommands", {
get: function () {
throw errorHelper.get(TacoErrorCodes.UnimplementedAbstractMethod);
},
enumerable: true,
configurable: true
});
Object.defineProperty(KitComponentCommand.prototype, "knownTargetsSubCommands", {
get: function () {
throw errorHelper.get(TacoErrorCodes.UnimplementedAbstractMethod);
},
enumerable: true,
configurable: true
});
Object.defineProperty(KitComponentCommand.prototype, "shortFlags", {
get: function () {
throw errorHelper.get(TacoErrorCodes.UnimplementedAbstractMethod);
},
enumerable: true,
configurable: true
});
/**
* Runs the underlying cordova command to perform the required operation
*/
KitComponentCommand.prototype.runCordovaCommand = function (targets) {
throw errorHelper.get(TacoErrorCodes.UnimplementedAbstractMethod);
};
/**
* Reads config.xml and returns the version overrides for a given target
*/
KitComponentCommand.prototype.getConfigXmlVersionSpec = function (targetName, projectInfo) {
throw errorHelper.get(TacoErrorCodes.UnimplementedAbstractMethod);
};
/**
* Edits the version override info to config.xml of the cordova project
*/
KitComponentCommand.prototype.editVersionOverrideInfo = function (componentInfo, projectInfo) {
throw errorHelper.get(TacoErrorCodes.UnimplementedAbstractMethod);
};
/**
* Given a targetName (say android, cordova-camera-plugin) for a given kit,
* returns the overriden version/src values
* returns null if target is not overridden for the given kit
*/
KitComponentCommand.prototype.getKitOverride = function (targetName, kitId) {
throw errorHelper.get(TacoErrorCodes.UnimplementedAbstractMethod);
};
/**
* returns command options for a kit component command
*/
KitComponentCommand.prototype.getCommandOptions = function (commandData) {
throw errorHelper.get(TacoErrorCodes.UnimplementedAbstractMethod);
};
/**
* Given bunch of targets getting added/removed prints a success message on the screen
*/
KitComponentCommand.prototype.printSuccessMessage = function (targets) {
throw errorHelper.get(TacoErrorCodes.UnimplementedAbstractMethod);
};
/**
* Given bunch of targets getting added/removed prints a progress message on the screen
*/
KitComponentCommand.prototype.printInProgressMessage = function (targets) {
throw errorHelper.get(TacoErrorCodes.UnimplementedAbstractMethod);
};
/**
* specific handling for whether this command can handle the args given, otherwise falls through to Cordova CLI
*/
KitComponentCommand.prototype.canHandleArgs = function (data) {
return true;
};
/**
* Parse the arguments and construct the command parameters.
*/
KitComponentCommand.prototype.parseArgs = function (args) {
var commandData = tacoUtility.ArgsHelper.parseArguments(this.knownOptions, this.shortFlags, args, 0);
var subCommand = commandData.remain[0];
var targets = commandData.remain.slice(1).filter(function (name) {
return !!name && name.length > 0;
});
if (targets.length === 0 && this.knownTargetsSubCommands.indexOf(subCommand) != -1) {
throw errorHelper.get(TacoErrorCodes.ErrorNoPluginOrPlatformSpecified, this.name, subCommand);
}
return {
remain: commandData.remain,
options: commandData.options,
original: commandData.original,
subCommand: this.resolveAlias(commandData.remain[0]),
targets: commandData.remain.slice(1),
commandOptions: this.getCommandOptions(commandData)
};
};
/**
* Performs add operation for a kit component
*/
KitComponentCommand.prototype.add = function () {
var commandData = this.data;
var self = this;
return ProjectHelper.getProjectInfo()
.then(function (projectInfo) {
if (!projectInfo.tacoKitId) {
return self.runCordovaCommand(commandData.targets)
.then(function () {
return self.generateTelemetryProperties();
});
}
return self.applyKitOverridesIfApplicable(projectInfo)
.then(function (componentInfos) {
// Print status message
self.printInProgressMessage(KitComponentCommand.getTargetsForStatus(commandData.targets));
var targets = componentInfos.map(function (t) { return t.targetId; });
return self.runCordovaCommand(targets)
.then(function () {
// Persist the specs which need to be persisted
return self.editVersionOverrideInfo(componentInfos.filter(function (t) { return t.spec != null; }), projectInfo);
})
.then(function () {
return self.generateTelemetryProperties(targets);
});
});
});
};
/**
* Passthrough method which calls into cordova to perform the required operation for a kit component
*/
KitComponentCommand.prototype.passthrough = function () {
var commandData = this.data;
var self = this;
return this.runCordovaCommand(commandData.targets)
.then(function () {
return self.generateTelemetryProperties();
});
};
/**
* Checks for kit overrides for the targets and massages the command targets
* parameter to be consumed by the "platform" command
*/
KitComponentCommand.prototype.applyKitOverridesIfApplicable = function (projectInfo) {
var commandData = this.data;
var self = this;
// For each of the platforms specified at command-line, check for overrides in the current kit
return PromiseUtils.chain(commandData.targets, function (targetId, targetSpecs) {
return self.getKitComponentInfo(targetId, projectInfo)
.then(function (spec) {
targetSpecs.push(spec);
return targetSpecs;
});
}, [] /* initial value */);
};
KitComponentCommand.prototype.getKitComponentInfo = function (targetId, projectInfo) {
var self = this;
// Proceed only if the version has not already been overridden on the command line or config.xml
// i.e, proceed only if user did not do "taco platform <subcommand> platform@<verion|src>"
return PromiseUtils.condition(self.isTargetVersionOverridden(targetId, projectInfo),
// no spec to persist, if overridden
{ targetId: targetId },
// cordova handles "cordova platform add foo@ and adds the foo@latest"
function () { return self.getKitOverride(targetId.split("@")[0], projectInfo.tacoKitId); });
};
KitComponentCommand.makeKitComponentInfo = function (targetName, version, src) {
if (version) {
return { name: targetName, spec: version, targetId: targetName + "@" + version };
}
if (src) {
return { name: targetName, spec: src, targetId: src };
}
// no spec to persist, if not a kit plugin
return { targetId: targetName, name: "", spec: "" };
};
/**
* Generates the telemetry properties for the platform/plugin operation
*/
KitComponentCommand.prototype.generateTelemetryProperties = function (targets) {
var commandData = this.data;
if (!targets) {
targets = commandData.targets;
}
this.printSuccessMessage(KitComponentCommand.getTargetsForStatus(commandData.targets));
var self = this;
return CliTelemetryHelper.getCurrentProjectTelemetryProperties()
.then(function (telemetryProperties) {
telemetryProperties["subCommand"] = telemetryHelper.telemetryProperty(commandData.subCommand);
for (var i = 0; i < targets.length; i++) {
telemetryProperties["target" + (i + 1)] = telemetryHelper.telemetryProperty(targets[i], true);
}
return Q(telemetryHelper.addPropertiesFromOptions(telemetryProperties, self.knownOptions, self.data.options, Object.keys(self.knownOptions)));
});
};
KitComponentCommand.prototype.isTargetVersionOverridden = function (targetId, projectInfo) {
// check if target is overridden on CLI using @version, @tag, git URI, file URI or in config.xml
var cliParamOveridden = !!(targetId.split("@")[1] || packageLoader.GIT_URI_REGEX.test(targetId) || packageLoader.FILE_URI_REGEX.test(targetId));
var self = this;
return PromiseUtils.or(cliParamOveridden, function () { return self.getConfigXmlVersionSpec(targetId, projectInfo)
.then(function (versionSpec) {
return (versionSpec !== "");
}); });
};
KitComponentCommand.getTargetsForStatus = function (targets) {
// Ignore git/file uris and strip off @version
targets = targets || [];
return targets.filter(function (target) {
return !packageLoader.GIT_URI_REGEX.test(target) &&
!packageLoader.FILE_URI_REGEX.test(target);
}).map(function (target) { return target.split("@")[0]; }).join(",");
};
return KitComponentCommand;
}(commands.TacoCommandBase));
exports.KitComponentCommand = KitComponentCommand;
//# sourceMappingURL=kitComponentCommand.js.map