UNPKG

ask-cli

Version:

Alexa Skills Kit (ASK) Command Line Interfaces

148 lines (130 loc) 5.47 kB
const fs = require('fs-extra'); const path = require('path'); const R = require('ramda'); const GitClient = require('@src/clients/git-client'); const ResourcesConfig = require('@src/model/resources-config'); const CONSTANTS = require('@src/utils/constants'); const Messenger = require('@src/view/messenger'); const SpinnerView = require('@src/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} 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, skillName, profile, repositoryUrl, doDebug) { const verbosityOptions = { showCommand: !!doDebug, showOutput: !!doDebug }; _gitCloneWorkflow(projectPath, repositoryUrl, verbosityOptions, profile); Messenger.getInstance().info(`\nLambda code for ${skillName} created at\n\t./lambda`); } function _gitCloneWorkflow(projectPath, repositoryUrl, verbosityOptions, 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 credentialHelperPath = `ask util git-credentials-helper --profile ${profile}`; gitClient.configureCredentialHelper(credentialHelperPath, 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); }