UNPKG

5.66 kBJavaScriptView Raw
1const fs = require('fs');
2const inquirer = require('inquirer');
3const path = require('path');
4
5const CONSTANTS = require('@src/utils/constants');
6const stringUtils = require('@src/utils/string-utils');
7const JsonView = require('@src/view/json-view');
8const Messenger = require('@src/view/messenger');
9
10module.exports = {
11 showInitInstruction,
12 confirmOverwrite,
13 getSkillId,
14 getSkillMetaSrc,
15 getCodeSrcForRegion,
16 getSkillInfra,
17 showPreviewAndConfirm,
18 getProjectFolderName
19};
20
21function showInitInstruction(profile) {
22 const initInstruction = `\
23This utility will walk you through creating an ask-resources.json file to help deploy
24your skill. This only covers the most common attributes and will suggest sensible
25defaults using AWS Lambda as your endpoint.
26
27This command is supposed to be running at the root of your ask-cli project, with the
28Alexa skill package and AWS Lambda code downloaded already.
29- Use "ask smapi export-package" to download the skill package.
30- Move your Lambda code into this folder depending on how you manage the code. It can
31be downloaded from AWS console, or git-cloned if you use git to control version.
32
33This will utilize your '${profile}' ASK profile. Run with "--profile" to specify a
34different profile.
35
36Press ^C at any time to quit.
37`;
38
39 Messenger.getInstance().info(initInstruction);
40}
41
42function confirmOverwrite(callback) {
43 inquirer.prompt([{
44 message: `${CONSTANTS.FILE_PATH.ASK_RESOURCES_JSON_CONFIG} already exists in current directory. Do you want to overwrite it? `,
45 type: 'confirm',
46 default: true,
47 name: 'isOverwriteConfirmed'
48 }]).then((answer) => {
49 callback(null, answer.isOverwriteConfirmed);
50 }).catch((error) => {
51 callback(error);
52 });
53}
54
55function getSkillId(callback) {
56 inquirer.prompt([{
57 message: 'Skill Id (leave empty to create one): ',
58 type: 'input',
59 name: 'skillId'
60 }]).then((answer) => {
61 callback(null, answer.skillId.trim());
62 }).catch((error) => {
63 callback(error);
64 });
65}
66
67function getSkillMetaSrc(callback) {
68 inquirer.prompt([{
69 message: 'Skill package path: ',
70 type: 'input',
71 default: './skill-package',
72 name: 'skillMetaSrc',
73 validate: (input) => {
74 if (!stringUtils.isNonBlankString(input)) {
75 return 'Path for skill package cannot be empty.';
76 }
77 if (!fs.existsSync(input)) {
78 return 'File path does not exist.';
79 }
80 return true;
81 }
82 }]).then((answer) => {
83 callback(null, answer.skillMetaSrc);
84 }).catch((error) => {
85 callback(error);
86 });
87}
88
89function getCodeSrcForRegion(region, callback) {
90 inquirer.prompt([{
91 message: `Lambda code path for ${region} region (leave empty to not deploy Lambda): `,
92 type: 'input',
93 default: './lambda',
94 name: 'skillCodeSrc',
95 validate: (input) => {
96 if (stringUtils.isNonBlankString(input) && !fs.existsSync(input)) {
97 return 'File path does not exist.';
98 }
99 return true;
100 }
101 }]).then((answer) => {
102 callback(null, answer.skillCodeSrc.trim());
103 }).catch((error) => {
104 callback(error);
105 });
106}
107
108function getSkillInfra(callback) {
109 inquirer.prompt([
110 {
111 message: 'Use AWS CloudFormation to deploy Lambda? ',
112 type: 'confirm',
113 default: true,
114 name: 'isUsingCfn'
115 },
116 {
117 message: 'Lambda runtime: ',
118 type: 'input',
119 default: 'nodejs10.x',
120 name: 'runtime'
121 },
122 {
123 message: 'Lambda handler: ',
124 type: 'input',
125 default: 'index.handler',
126 name: 'handler'
127 }
128 ]).then((answer) => {
129 callback(null, {
130 isUsingCfn: answer.isUsingCfn,
131 runtime: answer.runtime,
132 handler: answer.handler
133 });
134 }).catch((error) => {
135 callback(error);
136 });
137}
138
139function showPreviewAndConfirm(rootPath, resourcesConfig, callback) {
140 const { askResources, askStates } = resourcesConfig;
141 Messenger.getInstance().info(`
142Writing to ${path.join(rootPath, CONSTANTS.FILE_PATH.ASK_RESOURCES_JSON_CONFIG)}:
143${JsonView.toString(askResources)}
144
145Writing to ${path.join(rootPath, CONSTANTS.FILE_PATH.HIDDEN_ASK_FOLDER, CONSTANTS.FILE_PATH.ASK_STATES_JSON_CONFIG)}:
146${JsonView.toString(askStates)}
147`);
148 inquirer.prompt([{
149 message: 'Does this look correct? ',
150 type: 'confirm',
151 default: true,
152 name: 'confirmPreview'
153 }]).then((answer) => {
154 callback(null, answer.confirmPreview);
155 }).catch((error) => {
156 callback(error);
157 });
158}
159
160/**
161 * To get user's input project folder name
162 * @param {string} defaultName a default project name
163 * @param {callback} callback { error, response }
164 */
165function getProjectFolderName(defaultName, callback) {
166 inquirer.prompt([{
167 message: 'Please type in your folder name for the skill project (alphanumeric): ',
168 type: 'input',
169 default: defaultName,
170 name: 'projectFolderName',
171 validate: (input) => {
172 if (!input || stringUtils.filterNonAlphanumeric(input) === '') {
173 return 'Project folder name should consist of alphanumeric character(s) plus "-" only.';
174 }
175 return true;
176 }
177 }]).then((answer) => {
178 callback(null, stringUtils.filterNonAlphanumeric(answer.projectFolderName));
179 }).catch((error) => {
180 callback(error);
181 });
182}