UNPKG

7.08 kBJavaScriptView Raw
1const homedir = require( 'os' ).homedir();
2const nconf = require( 'nconf' );
3const chalk = require( 'chalk' );
4const fs = require( 'fs' );
5const p = require( 'path' );
6const config = require( './config' );
7const util = require( './util' );
8const app = require( './app' );
9const validate = require( './validate' );
10const sanitize = require( './sanitize' );
11
12const prompt = {
13
14 /**
15 * Arguments for project selection prompt.
16 * @param {Object} dataProjects Remote project data.
17 * @return {Object} Inquirer prompt object.
18 */
19 getProjPrompt( dataProjects ) {
20 const choices = dataProjects.map( ( project, index ) => ({
21 key : index,
22 name : chalk`${project.name} {gray ${project.slug}}`,
23 value : project.slug,
24 short : project.slug,
25 }) );
26
27 Object.keys( dataProjects ).forEach( ( key ) => {
28 const { slug } = dataProjects[key];
29 const path = nconf.get( `local_projects:${slug}:path` );
30 const choice = choices.filter( obj => obj.value === slug );
31
32 if ( path !== undefined ) {
33 // Indicate if project is already available.
34 choice[0].name = chalk`{green.bold ⬤ }${choice[0].name}`;
35 } else {
36 // Indicate if project is not available.
37 choice[0].name = chalk`{gray.bold ⬤ }${choice[0].name}`;
38 }
39 });
40
41 return {
42 type : 'list',
43 name : 'answer',
44 message : 'Choose a project:',
45 choices,
46 };
47 },
48
49 /**
50 * Arguments for profile option prompt.
51 * @return {Object} Inquirer prompt object.
52 */
53 getprofileOptionPrompt() {
54 const choices = [
55 {
56 name : 'Change profiles',
57 value : 'change',
58 short : 'change',
59 },
60 {
61 name : 'Create new profile',
62 value : 'create',
63 short : 'create',
64 },
65 {
66 name : 'Remove a profile',
67 value : 'remove',
68 short : 'remove',
69 },
70 ];
71 return {
72 type : 'list',
73 name : 'answer',
74 message : 'What do you want to do?',
75 choices,
76 };
77 },
78
79 /**
80 * Arguments for change profile prompt.
81 * @return {Object} Inquirer prompt object.
82 */
83 getChangeProfilePrompt() {
84 let name;
85 const choices = [];
86 const profiles = util.getProfiles();
87 const currentProfileName = util.getCurrentProfileName();
88
89 profiles.forEach( ( profilePath ) => {
90 const profileName = util.getProfileName( profilePath );
91
92 if ( currentProfileName === profileName ) {
93 name = chalk`{green.bold ⬤ }${profileName}`;
94 } else {
95 name = chalk`{gray.bold ⬤ }${profileName}`;
96 }
97
98 choices.push({
99 name,
100 value : profilePath,
101 short : profileName,
102 });
103 });
104
105 return {
106 type : 'list',
107 name : 'answer',
108 message : 'Select a profile',
109 choices,
110 };
111 },
112
113 /**
114 * Arguments for available projects prompt.
115 * @return {Object} Inquirer prompt object.
116 */
117 getAvailableProjectsPrompt() {
118 const projectList = util.getLocalProjects();
119 const choices = [];
120
121 Object.keys( projectList ).forEach( ( key, i ) => {
122 const choice = {
123 key : i,
124 name : chalk`${projectList[key].name} {gray ${key}}`,
125 value : key,
126 short : key,
127 };
128 choices.push( choice );
129 });
130
131 return {
132 type : 'list',
133 name : 'answer',
134 message : 'Choose a project:',
135 choices,
136 };
137 },
138
139 /**
140 * Arguments for environment selection prompt.
141 * @param {Object} dataProjects Remote project data.
142 * @param {String} prj Name of project to get available environments from.
143 * @return {Object} Inquirer prompt object.
144 */
145 getEnvPrompt( dataProjects, prj ) {
146 const project = util.getProjectInfo( dataProjects, prj );
147 const envs = project.env;
148 const choices = envs.map( ( env, index ) => ({
149 key : index,
150 name : chalk`${env.name.toUpperCase()} {gray ${env.url}}`,
151 value : env.name,
152 }) );
153
154 return {
155 type : 'list',
156 name : 'answer',
157 message : 'Choose an environment:',
158 choices,
159 };
160 },
161
162 /**
163 * Arguments for project type selection prompt.
164 * @param {Array} dataProjectTypes The types of projects available to build.
165 * @return {Object} Inquirer prompt object.
166 */
167 getTypePrompt( dataProjectTypes ) {
168 const choices = dataProjectTypes.map( ( type, index ) => ({
169 key : index,
170 name : type.name,
171 value : type.name,
172 short : type.name,
173 }) );
174
175 return {
176 type : 'list',
177 name : 'answer',
178 message : 'Choose a project type:',
179 choices,
180 };
181 },
182
183 /**
184 * Arguments for directory selection prompt.
185 * @return {Object} Inquirer prompt object.
186 */
187 getDirPrompt() {
188 return {
189 type : 'input',
190 name : 'answer',
191 message : 'Choose a directory:',
192 default : nconf.get( 'default_clone_path' ),
193 validate : validate.path,
194 filter : sanitize.path,
195 };
196 },
197
198 /**
199 * Arguments for project name prompt.
200 * @return {Object} Inquirer prompt object.
201 */
202 getNamePrompt() {
203 return {
204 type : 'input',
205 name : 'answer',
206 message : 'Project Name:',
207 validate : validate.name,
208 };
209 },
210
211 /**
212 * Arguments for email selection prompt.
213 * @return {Object} Inquirer prompt object.
214 */
215 getEmailPrompt() {
216 return {
217 type : 'input',
218 name : 'answer',
219 message : 'Enter your email:',
220 validate : validate.email,
221 };
222 },
223
224 /**
225 * Arguments for profile name prompt.
226 * @return {Object} Inquirer prompt object.
227 */
228 getProfileNamePrompt() {
229 return {
230 type : 'input',
231 name : 'answer',
232 message : 'Enter a name for this profile:',
233 validate : validate.profileName,
234 };
235 },
236
237 /**
238 * Arguments for data projects url selection prompt.
239 * @return {Object} Inquirer prompt object.
240 */
241 getProjectsUrlPrompt() {
242 return {
243 type : 'input',
244 name : 'answer',
245 message : chalk`Enter url to json {green projects} config:`,
246 validate : validate.jsonUrl,
247 };
248 },
249
250 /**
251 * Arguments for data project types url selection prompt.
252 * @return {Object} Inquirer prompt object.
253 */
254 getTypesUrlPrompt() {
255 return {
256 type : 'input',
257 name : 'answer',
258 message : chalk`Enter url to json {green types} config:`,
259 validate : validate.jsonUrl,
260 default : 'https://masonitedoors.github.io/slab-cli/data/default_project_types.json',
261 };
262 },
263
264 /**
265 * Confirm action prompt.
266 * @return {Object} Inquirer prompt object.
267 */
268 getConfirmPrompt() {
269 return {
270 type : 'confirm',
271 name : 'answer',
272 message : 'Are you sure?',
273 };
274 },
275
276 /**
277 * If a project with this name already exists prompt..
278 * @param {String} existingSlug The slug for the project that already exists.
279 * @param {String} newSlug The slug for the new project.
280 * @return {Object} Inquirer prompt object.
281 */
282 getAlreadyExistsPrompt( existingSlug, newSlug ) {
283 return {
284 type : 'confirm',
285 name : 'answer',
286 message : chalk`A project with this name already exists. Continue as a new project? {yellow ${newSlug}}`,
287 };
288 },
289
290 /**
291 * Arguments for default directory prompt.
292 * @return {Object} Inquirer prompt object.
293 */
294 getDefaultDirPrompt() {
295 return {
296 type : 'input',
297 name : 'answer',
298 message : 'Enter default clone directory:',
299 default : homedir,
300 validate : validate.path,
301 filter : sanitize.path,
302 };
303 },
304
305};
306
307module.exports = prompt;