UNPKG

ask-cli-x

Version:

Alexa Skills Kit (ASK) Command Line Interfaces

135 lines (134 loc) 5.78 kB
"use strict"; const fs = require("fs-extra"); const os = require("os"); const path = require("path"); const R = require("ramda"); const GitClient = require("../../clients/git-client"); const ResourcesConfig = require("../../model/resources-config"); const CONSTANTS = require("../../utils/constants"); const Messenger = require("../../view/messenger"); const SpinnerView = require("../../view/spinner-view"); const filesToIgnore = ["lambda/node_modules", ".ask/", "ask-resources.json"]; module.exports = { generateProject, cloneProjectFromGit, doSkillPackageExist, }; /** * To generate local skill project folder and ask-resources.json * @param {string} projectPath The skill project folder path * @param {string} skillId The skill id * @param {string} skillName The skill name * @param {JSON} metadata The Alexa hosted skill metadata * @param {string} profile The current profile */ function generateProject(projectPath, skillId, skillName, metadata, profile) { _generateProjectDirectory(projectPath); _updateAskResources(projectPath, skillId, metadata, profile); Messenger.getInstance().info(`\nProject directory for ${skillName} created at\n\t${projectPath}`); } function _generateProjectDirectory(projectPath) { const askResourcesPath = path.join(projectPath, CONSTANTS.FILE_PATH.ASK_RESOURCES_JSON_CONFIG); const askResourcesJson = R.clone(ResourcesConfig.BASE); fs.mkdirSync(projectPath); fs.writeJSONSync(askResourcesPath, askResourcesJson, { spaces: CONSTANTS.CONFIGURATION.JSON_DISPLAY_INDENT }); } function _updateAskResources(projectPath, skillId, metadata, profile) { new ResourcesConfig(path.join(projectPath, CONSTANTS.FILE_PATH.ASK_RESOURCES_JSON_CONFIG)); ResourcesConfig.getInstance().setSkillId(profile, skillId); ResourcesConfig.getInstance().setSkillInfraType(profile, "@ask-cli/hosted-skill-deployer"); ResourcesConfig.getInstance().write(); } /** * To go through the git clone process to get the project from git repository * @param {string} projectPath The skill project folder path * @param {string} skillId The skill Id * @param {string} skillName The skill name * @param {string} profile The current profile * @param {string} repositoryUrl The git url * @param {boolean} doDebug is debug mode or not */ function cloneProjectFromGit(projectPath, skillId, skillName, profile, repositoryUrl, doDebug) { const verbosityOptions = { showCommand: !!doDebug, showOutput: !!doDebug, }; _gitCloneWorkflow(projectPath, repositoryUrl, verbosityOptions, skillId, profile); Messenger.getInstance().info(`\nLambda code for ${skillName} created at\n\t./lambda`); } function _gitCloneWorkflow(projectPath, repositoryUrl, verbosityOptions, skillId, profile) { const verbose = verbosityOptions.showCommand; const progressSpinner = new SpinnerView(); if (!verbose) { progressSpinner.start("Cloning Alexa Hosted Skill..."); } if (verbose) { Messenger.getInstance().info("- Setting up git repo..."); } const gitClient = new GitClient(projectPath, verbosityOptions); gitClient.init(); const credentialScriptPath = path.join(os.homedir(), CONSTANTS.FILE_PATH.ASK.HIDDEN_FOLDER, CONSTANTS.FILE_PATH.ASK.SCRIPTS_FOLDER.NAME, CONSTANTS.FILE_PATH.ASK.SCRIPTS_FOLDER.GIT_CREDENTIAL_HELPER); const credentialScriptExecution = `'${credentialScriptPath}' ${profile} ${skillId}`; gitClient.configureCredentialHelper(credentialScriptExecution, repositoryUrl); gitClient.addOrigin(repositoryUrl); if (verbose) { Messenger.getInstance().info("- Fetching git repo..."); } gitClient.fetchAll(projectPath, verbosityOptions); if (verbose) { Messenger.getInstance().info("- Checking out master branch..."); } gitClient.checkoutBranch("master"); if (verbose) { Messenger.getInstance().info("- Setting up .gitignore..."); } gitClient.setupGitIgnore(filesToIgnore); if (verbose) { Messenger.getInstance().info("- Git repo successfully setup"); } _makeBranchesVisible(gitClient); if (!verbose) { progressSpinner.terminate(); } } function _makeBranchesVisible(gitClient) { const branchesToCheckout = ["prod", "master"]; branchesToCheckout.forEach((branch) => { gitClient.checkoutBranch(branch); }); } /** * To check whether a skill-package folder exists, * it can be generated by collecting git-cloned skill.json, models and isps. * otherwise, return false. * @param {string} skillName The skill name * @param {string} projectPath The skill project folder path * @param {string} skillId The skill id * @param {callback} callback { error, response } */ function doSkillPackageExist(skillName, projectPath, skillId, callback) { const skillPackagePath = path.join(projectPath, "skill-package"); if (fs.existsSync(skillPackagePath)) { return callback(null, true); } fs.mkdirSync(skillPackagePath); const modelsPath = path.join(projectPath, "models"); const skillJsonPath = path.join(projectPath, "skill.json"); const ispsPath = path.join(projectPath, "isps"); const doModelsExist = fs.existsSync(modelsPath); const doSkillJsonExist = fs.existsSync(skillJsonPath); const doIspsExist = fs.existsSync(ispsPath); if (!(doModelsExist || doSkillJsonExist)) { return callback(null, false); } if (doModelsExist) { fs.moveSync(modelsPath, path.join(skillPackagePath, "interactionModels/custom")); } if (doSkillJsonExist) { fs.moveSync(skillJsonPath, path.join(skillPackagePath, "skill.json")); } if (doIspsExist) { fs.moveSync(skillJsonPath, path.join(skillPackagePath, "isps")); } callback(null, true); }