@hkvstore/taco-cli
Version:
taco-cli is a command-line interface for rapid Apache Cordova development (forked from Microsoft taco-cli)
262 lines (260 loc) • 12.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/nopt.d.ts" />
/// <reference path="../../typings/tacoKits.d.ts" />
/// <reference path="../../typings/tacoUtils.d.ts" />
/// <reference path="../../typings/tacoUtils.d.ts" />
"use strict";
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 fs = require("fs");
var path = require("path");
var Q = require("q");
var kit = require("./kit");
var kitHelper = require("./utils/kitHelper");
var resources = require("../resources/resourceManager");
var TacoErrorCodes = require("./tacoErrorCodes");
var errorHelper = require("./tacoErrorHelper");
var tacoUtility = require("taco-utils");
var templateManager = require("./utils/templateManager");
var telemetryHelper = tacoUtility.TelemetryHelper;
var commands = tacoUtility.Commands;
var CordovaWrapper = tacoUtility.CordovaWrapper;
var logger = tacoUtility.Logger;
var LoggerHelper = tacoUtility.LoggerHelper;
var utils = tacoUtility.UtilHelper;
/**
* Create
*
* Handles "taco create"
*/
var Create = (function (_super) {
__extends(Create, _super);
function Create() {
_super.apply(this, arguments);
this.name = "create";
}
Create.prototype.parseArgs = function (args) {
var commandData = tacoUtility.ArgsHelper.parseArguments(Create.KNOWN_OPTIONS, Create.SHORT_HANDS, args, 0);
this.cordovaParameters = {
projectPath: commandData.remain[0],
appId: commandData.remain[1] ? commandData.remain[1] : Create.DEFAULT_APP_ID,
appName: commandData.remain[2] ? commandData.remain[2] : Create.DEFAULT_APP_NAME,
cordovaConfig: commandData.remain[3],
copyFrom: commandData.options["copy-from"],
linkTo: commandData.options["link-to"]
};
this.verifyArguments(commandData);
return commandData;
};
Create.prototype.runCommand = function () {
var self = this;
var templateDisplayName;
return this.createProject()
.then(function (templateUsed) {
templateDisplayName = templateUsed;
var kitProject = self.isKitProject();
var valueToSerialize = kitProject ? self.data.options["kit"] : self.data.options["cordova"];
var tacoJsonEditParams = {
projectPath: self.cordovaParameters.projectPath,
isKitProject: kitProject,
version: valueToSerialize
};
return kitHelper.editTacoJsonFile(tacoJsonEditParams);
})
.then(function () {
self.finalize(templateDisplayName);
return Q.resolve({});
}).then(function () {
return self.generateTelemetryProperties();
});
};
/**
* Generates the telemetry properties for the create operation
*/
Create.prototype.generateTelemetryProperties = function () {
var telemetryProperties = {};
telemetryProperties["cliVersion"] = telemetryHelper.telemetryProperty(require("../package.json").version);
var self = this;
return kitHelper.getDefaultKit().then(function (defaultKitId) {
if (self.isKitProject()) {
telemetryProperties["kit"] = telemetryHelper.telemetryProperty(self.data.options["kit"] || defaultKitId);
telemetryProperties["template"] = telemetryHelper.telemetryProperty(self.data.options["template"] || "blank");
}
else {
telemetryProperties["cordova"] = telemetryHelper.telemetryProperty(self.data.options["cordova"]);
}
return Q.resolve(telemetryHelper.addPropertiesFromOptions(telemetryProperties, Create.KNOWN_OPTIONS, self.data.options, ["cordova", "kit", "template"]));
});
};
/**
* Verify that the right combination of options is passed
*/
Create.prototype.verifyArguments = function (data) {
// Parameter exclusivity validation
if (data.options.hasOwnProperty("template") && (data.options.hasOwnProperty("copy-from") || data.options.hasOwnProperty("link-to"))) {
throw errorHelper.get(TacoErrorCodes.CommandCreateNotTemplateIfCustomWww);
}
if (data.options.hasOwnProperty("cordova") && data.options.hasOwnProperty("kit")) {
throw errorHelper.get(TacoErrorCodes.CommandCreateNotBothCordovaCliAndKit);
}
if (data.options.hasOwnProperty("cordova") && data.options.hasOwnProperty("template")) {
throw errorHelper.get(TacoErrorCodes.CommandCreateNotBothTemplateAndCordovaCli);
}
// Make sure a path was specified
var createPath = this.cordovaParameters.projectPath;
if (!createPath) {
throw errorHelper.get(TacoErrorCodes.CommandCreateNoPath);
}
// Make sure the specified path is valid
if (!utils.isPathValid(createPath) || !fs.existsSync(path.dirname(createPath))) {
throw errorHelper.get(TacoErrorCodes.CommandCreateInvalidPath, createPath);
}
// Make sure the specified path is empty if it exists
if (fs.existsSync(createPath) && fs.readdirSync(createPath).length > 0) {
throw errorHelper.get(TacoErrorCodes.CommandCreatePathNotEmpty, createPath);
}
};
/**
* Creates the Kit or CLI project
*/
Create.prototype.createProject = function () {
var self = this;
var cordovaCli = this.data.options["cordova"];
var mustUseTemplate = this.isKitProject() && !this.cordovaParameters.copyFrom && !this.cordovaParameters.linkTo;
var kitId = this.data.options["kit"];
var templateId = this.data.options["template"];
// Create the project
if (!this.isKitProject()) {
return this.printStatusMessage()
.then(function () {
// Use the CLI version specified as an argument to create the project "command.create.status.cliProject
return CordovaWrapper.create(cordovaCli, self.cordovaParameters);
});
}
else {
return kitHelper.getValidCordovaCli(kitId).then(function (cordovaCliToUse) {
cordovaCli = cordovaCliToUse;
})
.then(function () {
return self.printStatusMessage();
})
.then(function () {
if (kitId) {
return kitHelper.getKitInfo(kitId);
}
else {
return Q.resolve(null);
}
})
.then(function (kitInfo) {
if (kitInfo && !!kitInfo.deprecated) {
// Warn the user
logger.log(resources.getString("CommandCreateWarningDeprecatedKit", kitId));
}
if (mustUseTemplate) {
var templates = new templateManager(kitHelper);
return templates.createKitProjectWithTemplate(kitId, templateId, cordovaCli, self.cordovaParameters)
.then(function (templateDisplayName) {
return Q.resolve(templateDisplayName);
});
}
else {
return CordovaWrapper.create(cordovaCli, self.cordovaParameters);
}
});
}
};
/**
* Prints the project creation status message
*/
Create.prototype.printStatusMessage = function () {
var self = this;
var cordovaParameters = this.cordovaParameters;
var projectPath = cordovaParameters.projectPath ? path.resolve(cordovaParameters.projectPath) : "''";
if (!this.isKitProject()) {
self.printNewProjectTable("CommandCreateStatusTableCordovaCLIVersionDescription", this.data.options["cordova"]);
return Q({});
}
else {
var kitIdArg = this.data.options["kit"];
return (kitIdArg ? Q(kitIdArg) : kitHelper.getDefaultKit())
.then(function (kitId) { return kitHelper.getKitInfo(kitId)
.then(function (kitInfo) { return self.printNewProjectTable("CommandCreateStatusTableKitVersionDescription", kit.getKitTitle(kitId, kitInfo)); }); });
}
};
Create.prototype.repeat = function (text, times) {
// From: http://stackoverflow.com/questions/1877475/repeat-character-n-times
return new Array(times + 1).join(text);
};
Create.prototype.printNewProjectTable = function (kitOrCordovaStringResource, kitOrCordovaVersion) {
var cordovaParameters = this.cordovaParameters;
var projectFullPath = path.resolve(this.cordovaParameters.projectPath);
var indentation = 6; // We leave some empty space on the left before the text/table starts
var nameDescriptionPairs = [
{ name: resources.getString("CommandCreateStatusTableNameDescription"), description: cordovaParameters.appName },
{ name: resources.getString("CommandCreateStatusTableIDDescription"), description: cordovaParameters.appId },
{ name: resources.getString("CommandCreateStatusTableLocationDescription"), description: projectFullPath },
{ name: resources.getString(kitOrCordovaStringResource), description: kitOrCordovaVersion },
];
LoggerHelper.logNameDescriptionTableWithHorizontalBorders(nameDescriptionPairs, indentation);
};
/**
* Finalizes the creation of project by printing the Success messages with information about the Kit and template used
*/
Create.prototype.finalize = function (templateDisplayName) {
// Report success over multiple loggings for different styles
var projectFullPath = path.resolve(this.cordovaParameters.projectPath);
if (this.isKitProject()) {
if (templateDisplayName) {
logger.log(resources.getString("CommandCreateSuccessProjectTemplate", templateDisplayName, projectFullPath));
if (this.data.options["template"] === "typescript") {
logger.log(resources.getString("CommandCreateInstallGulp"));
}
}
else {
// If both --copy-from and --link-to are specified, Cordova uses --copy-from and ignores --link-to, so for our message we should use the path provided to --copy-from if the user specified both
var customWwwPath = this.data.options["copy-from"] || this.data.options["link-to"];
logger.log(resources.getString("CommandCreateSuccessProjectCustomWww", customWwwPath, projectFullPath));
}
}
else {
logger.log(resources.getString("CommandCreateSuccessProjectCLI", projectFullPath));
}
// Print the onboarding experience
logger.log(resources.getString("OnboardingExperienceTitle"));
LoggerHelper.logList(["HowToUseChangeToProjectFolder",
"HowToUseCommandPlatformAddPlatform",
"HowToUseCommandInstallReqsPlugin",
"HowToUseCommandAddPlugin",
"HowToUseCommandSetupRemote",
"HowToUseCommandBuildPlatform",
"HowToUseCommandEmulatePlatform",
"HowToUseCommandRunPlatform"].map(function (msg) { return resources.getString(msg, projectFullPath); }));
["",
"HowToUseCommandHelp",
"HowToUseCommandDocs"].forEach(function (msg) { return logger.log(resources.getString(msg)); });
};
Create.prototype.isKitProject = function () {
return !this.data.options["cordova"];
};
Create.KNOWN_OPTIONS = {
kit: String,
template: String,
cordova: String,
"copy-from": String,
"link-to": String
};
Create.SHORT_HANDS = {
src: "--copy-from"
};
Create.DEFAULT_APP_ID = "io.taco.hellotaco";
Create.DEFAULT_APP_NAME = "HelloTaco";
return Create;
}(commands.TacoCommandBase));
module.exports = Create;
//# sourceMappingURL=create.js.map