ask-cli
Version:
Alexa Skills Kit (ASK) Command Line Interfaces
120 lines (119 loc) • 5.27 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createCommand = void 0;
const fs_extra_1 = __importDefault(require("fs-extra"));
const jsonfile_1 = __importDefault(require("jsonfile"));
const os_1 = __importDefault(require("os"));
const path_1 = __importDefault(require("path"));
const abstract_command_1 = require("../abstract-command");
const option_model_json_1 = __importDefault(require("../option-model.json"));
const app_config_1 = __importDefault(require("../../model/app-config"));
const constants_1 = __importDefault(require("../../utils/constants"));
const string_utils_1 = __importDefault(require("../../utils/string-utils"));
const messenger_1 = __importDefault(require("../../view/messenger"));
const helper_1 = __importDefault(require("./helper"));
const messages_1 = __importDefault(require("./messages"));
const ui_1 = __importDefault(require("./ui"));
class ConfigureCommand extends abstract_command_1.AbstractCommand {
name() {
return "configure";
}
description() {
return "helps to configure the credentials that ask-cli uses to authenticate the user to Amazon developer services";
}
requiredOptions() {
return [];
}
optionalOptions() {
return ["no-browser", "profile", "debug"];
}
async handle(cmd) {
messenger_1.default.getInstance().info(messages_1.default.ASK_CLI_CONFIGURATION_MESSAGE);
try {
const askProfileConfig = _ensureAppConfigInitiated(cmd);
const askProfile = await _initiateAskProfileSetup(askProfileConfig);
const appConfig = app_config_1.default.getInstance();
messenger_1.default.getInstance().info(messages_1.default.CONFIGURE_SETUP_SUCCESS_MESSAGE);
messenger_1.default.getInstance().info(`ASK Profile: ${askProfile}`);
messenger_1.default.getInstance().info(`AWS Profile: ${appConfig.getAwsProfile(askProfile)}`);
messenger_1.default.getInstance().info(`Vendor ID: ${appConfig.getVendorId(askProfile)}`);
app_config_1.default.dispose();
}
catch (error) {
messenger_1.default.getInstance().error(error);
throw error;
}
}
}
exports.default = ConfigureCommand;
/**
* Initiates ASK profile setup.
* @param {Object} askProfileConfig
* @param {Function} callback
* @private
*/
async function _initiateAskProfileSetup(askProfileConfig) {
if (askProfileConfig.isFirstTimeProfileCreation || askProfileConfig.askProfile) {
const profile = (askProfileConfig.askProfile || constants_1.default.ASK_DEFAULT_PROFILE_NAME).trim();
if (!string_utils_1.default.validateSyntax("PROFILE_NAME", profile)) {
throw messages_1.default.PROFILE_NAME_VALIDATION_ERROR;
}
return new Promise((resolve, reject) => {
helper_1.default.initiateAskProfileSetup({ ...askProfileConfig, askProfile: profile }, (err, askProfile) => {
if (err) {
return reject(err);
}
resolve(askProfile);
});
});
}
return new Promise((resolve, reject) => {
ui_1.default.createOrUpdateProfile(app_config_1.default.getInstance().getProfilesList(), (error, profile) => {
if (error) {
return reject(error);
}
helper_1.default.initiateAskProfileSetup({ ...askProfileConfig, askProfile: profile }, (err, askProfile) => {
if (err) {
return reject(err);
}
resolve(askProfile);
});
});
});
}
/**
* Ensures AppConfig is initiated properly before proceeding to further steps.
* @param {Object} cmd commander object.
* @private
*/
function _ensureAppConfigInitiated(cmd) {
const askFolderPath = path_1.default.join(os_1.default.homedir(), constants_1.default.FILE_PATH.ASK.HIDDEN_FOLDER);
const configFilePath = path_1.default.join(askFolderPath, constants_1.default.FILE_PATH.ASK.PROFILE_FILE);
if (!fs_extra_1.default.existsSync(configFilePath)) {
fs_extra_1.default.ensureDirSync(askFolderPath);
jsonfile_1.default.writeFileSync(configFilePath, { profiles: {} }, { spaces: constants_1.default.CONFIGURATION.JSON_DISPLAY_INDENT, mode: constants_1.default.FILE_PERMISSION.USER_READ_WRITE });
}
new app_config_1.default(configFilePath);
return _buildAskConfig(cmd, askFolderPath, configFilePath);
}
/**
* Build configuration from input CLI arguments.
* @param {Object} cmd commander object.
* @param {String} askFolderPath ask folder path.
* @param {String} configFilePath cli_config file path.
* @private
*/
function _buildAskConfig(cmd, askFolderPath, configFilePath) {
return {
askFolderPath,
configFilePath,
isFirstTimeProfileCreation: app_config_1.default.getInstance().getProfilesList().length < 1,
askProfile: cmd.profile,
needBrowser: cmd.browser,
doDebug: cmd.debug,
};
}
exports.createCommand = new ConfigureCommand(option_model_json_1.default).createCommand();