-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompting.js
472 lines (471 loc) · 19.1 KB
/
prompting.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
// buildPrompts.js
const chalk = require( "chalk" );
module.exports = {
// Define list values globally for shared use
status: [ "active", "inactive", "private" ],
custom_types: [ "string", "number", "boolean", "object", "array", "$ref" ],
string_formats: [ "none", "url", "textarea", "html" ],
array_formats: [ "none", "tabs", "table" ],
background_properties: [ "color", "isFixed", "align", "overlay", "images" ],
// Define the filter function to convert a delineated string
// to an array and trim each item in the list of whitespace
stringToArrayAndTrim: function( string, delimiter, dash = true ) {
let toArray = [];
string.split( delimiter ).forEach( function( value, index ){
value = value.replace( /[^a-z,A-Z,0-9,-|_| ]/g, "" ).trim();
if ( dash ) {
value = value.replace( /[_| ]/g, "-" );
}
if( value !== '' ) {
// replace underscores with dashes
toArray.push( value );
}
} );
return toArray;
},
findDuplicateValues: function( arr ) {
let uniq = arr.map( ( item ) => {
return { count: 1, item: item }
} ).reduce( ( a, b ) => {
a[ b.item ] = ( a[ b.item ] || 0 ) + b.count;
return a;
}, { } );
return Object.keys( uniq ).filter( ( a ) => uniq[ a ] > 1 );
},
init: function() {
let _this = this;
return [ {
type: "list",
name: "template_type",
message: "What would you like to create?",
choices: [ "atom", "component", "sub_pattern", "layout", "pattern", "pattern_group", "page" ],
default: "component"
}, {
type: "input",
name: "name",
message: "What will this be called (i.e. \"Awesome video\")?",
validate: function( answer ) {
if ( answer.length < 1 ) {
return "I get it, naming is hard; but it must have a name. You can always change it later.";
}
return true;
},
when: function( answers ) {
return answers.template_type !== "page";
}
}, {
type: "input",
name: "description",
message: function( answers ) {
return "Describe the " + answers.name.toLowCase() + " " + answers.template_type + ": ";
},
when: function ( answers ) {
return answers.template_type !== "page";
}
}, {
type: "input",
name: "name",
message: "What will this be called (i.e. \"Awesome page\")?",
validate: function( answer ) {
if ( answer.length < 1 ) {
return "I get it, naming is hard; but it must have a name. You can always change it later.";
}
return true;
},
when: function( answers ) {
return answers.template_type === "page";
},
filter: function( response ) {
if ( !response.endsWith( "page" ) ) {
response += " page";
} else if ( response.endsWith( "content type" ) ) {
response.replace( "/content type/g", "page" );
}
return response;
}
}, {
type: "list",
name: "status",
message: "What status would you like for this pattern?",
choices: _this.status,
when: function( answers ) {
return answers.template_type !== "atom" && answers.template_type !== "component" && answers.template_type !== "page";
}
}, {
type: "checkbox",
name: "template_style",
message: "What type of atom are you creating (can choose more than 1)?",
choices: [ "json/entity", "twig template" ],
when: function( answers ) {
return answers.template_type === "atom";
},
validate: function( answer ) {
if ( answer.length < 1 ) {
return "You must choose at least one template type for an atom.";
}
return true;
}
}, {
type: "checkbox",
name: "template_elements",
message: "Please select any of the templated elements below you want included:",
choices: [ "align", "size", "seo", "title", "headline", "summary", "link", "image", "video" ],
when: function( answers ) {
return answers.template_type === "component";
}
}, {
type: "checkbox",
name: "template_elements",
message: "Please select any of the templated elements below you want included:",
choices: [ "background", "header", "body", "aside", "footer" ],
when: function( answers ) {
return answers.template_type === "pattern";
}
}, {
type: "checkbox",
name: "template_elements",
message: "Please select any of the templated elements below you want included:",
choices: [ "navigation", "background", "header", "body", "aside", "footer" ],
when: function( answers ) {
return answers.template_type === "pattern_group";
}
}, {
type: "checkbox",
name: "template_elements",
message: "Please select any of the templated elements below you want included:",
choices: [ "container", "content" ], // [ "config", "container", "content" ],
when: function( answers ) {
return answers.template_type === "sub_pattern";
}
}, {
type: "checkbox",
name: "template_elements",
message: "Please select any of the templated elements below you want included:",
choices: [ "config", "background", "header", "body", "aside", "footer" ],
when: function( answers ) {
return answers.template_type === "layout";
}
}, {
type: "checkbox",
name: "config_options",
message: "Please select any of the templated config options below you want included:",
choices: [ "align", "breakpoints" ],
when: function( answers ) {
// Don't run these for now
//return answers.template_type === "sub_pattern" && answers.template_elements.includes( "config" );
return false;
}
}, {
type: "checkbox",
name: "config_options",
message: "Please select any of the templated config options below you want included:",
choices: [ "collapse", "vertical_spacing", "eqpts", "justify" ],
when: function( answers ) {
return answers.template_type === "layout" && answers.template_elements.includes( "config" );
}
}, {
type: "checkbox",
name: "background_options",
message: "Please select any of the background options you want included:",
choices: _this.background_properties,
when: function( answers ) {
return ( answers.template_type === "pattern" || answers.template_type === "pattern_group" ) && answers.template_elements.includes( "background" );
}
}, {
type: "input",
name: "content_elements",
message: "List the name of any custom properties you want included in the content section (i.e. \"my-el, my-other-el\"):",
validate: function( answer ) {
let duplicates = _this.findDuplicateValues( answer );
if ( duplicates.length > 0 ) {
return "This list cannot contain duplicate values.";
} else {
return true;
}
},
filter: function( response ) {
return _this.stringToArrayAndTrim( response, "," );
},
when: function( answers ) {
return answers.template_type === "sub_pattern" && answers.template_elements.includes( "content" );
}
}, {
type: "input",
name: "custom_elements",
message: "List any custom properties you would like to include (i.e. \"my-foo, my-other-bar\"):",
validate: function( answer ) {
let duplicates =_this.findDuplicateValues( answer );
if ( duplicates.length > 0 ) {
return "This list cannot contain duplicate values.";
} else {
return true;
}
},
filter: function( response ) {
return _this.stringToArrayAndTrim( response, "," );
},
when: function( answers ) {
return answers.template_type !== "page";
}
} ];
},
refs: function( section ) {
// Set context to this parent object to reference array datasets
let _this = this;
return [ {
type: "input",
name: section + "_refs",
message: "List the components/subpatterns you would like in the " + section + " (i.e. \"cta, footnote, raw_html\"):",
validate: function( answer ) {
let duplicates =_this.findDuplicateValues( answer );
if ( duplicates.length > 0 ) {
return "This list cannot contain duplicate values.";
} else {
return true;
}
},
filter: function( response ) {
return _this.stringToArrayAndTrim( response, ",", false );
}
} ];
},
// Dynamically build the custom element prompts
custom: function( element, parent, grid, nestLevel ) {
// Set context to this parent object to reference array datasets
let _this = this;
// Get just the parent and child names separately
let child = element;
if ( parent !== "" ) {
parent = " in " + chalk.blue( parent );
}
// Set up the prompt data.
let prompts = [ {
type: "list",
name: element + "_type",
message: "Select a data type for " + chalk.red(child) + parent + ":",
choices: _this.custom_types,
default: "string"
} ];
if ( nestLevel > 1 ) {
prompts.push( {
type: "confirm",
name: element + "_type_confirm",
message: "Are you sure you want to nest this element as an object? Too much nesting can lead to difficult user interfaces.",
default: false,
when: function( answers ) {
return answers[ element + "_type" ] === "object";
}
}, {
type: "list",
name: element + "_type",
message: "Select a data type for " + chalk.red(child) + parent + ":",
default: "string",
choices: function() {
// Remove object from the options list
_this.custom_types.splice( _this.custom_types.indexOf( "object" ), 1 );
return _this.custom_types;
},
when: function( answers ) {
return answers[ element + "_type_confirm" ] === false;
}
} );
}
prompts.push( {
type: "list",
name: element + "_format",
message: "Would you like to apply a format to your string?",
choices: _this.string_formats,
default: "none",
when: function( answers ) {
return answers[ element + "_type" ] === "string";
},
filter: function( response ) {
if ( response === "none" ) {
return null;
} else {
return response;
}
}
}, {
type: "input",
name: element + "_refs",
message: "List any references that you would like in this array: (i.e. \"raw_html, generic, cta\")",
validate: function( answer ) {
let duplicates =_this.findDuplicateValues( answer );
if ( duplicates.length > 0 ) {
return "This list cannot contain duplicate values.";
} else {
return true;
}
},
filter: function( response ) {
return _this.stringToArrayAndTrim( response, ",", false );
},
when: function( answers ) {
return answers[ element + "_type" ] === "array";
}
}, {
type: "input",
name: element + "_refs",
message: "Please enter the ref details: (i.e. \"band_header.json\" or \"band_header.json#/properties/title\")",
when: function( answers ) {
return answers[ element + "_type" ] === "$ref";
}
}, {
type: "confirm",
name: element + "_minmax_confirm",
message: "Do you want a min/max for your array?",
default: false,
when: function( answers ) {
return answers[ element + "_type" ] === "array";
}
}, {
type: "input",
name: element + "_min",
message: "Min:",
validate: function( answer ) {
if ( isNaN( parseInt( answer ) ) || parseInt( answer ) < 1 ) {
return "You must enter a valid number.";
}
return true;
},
filter: function( response ) {
return parseInt( response );
},
when: function( answers ) {
return answers[ element + "_minmax_confirm" ];
}
}, {
type: "input",
name: element + "_max",
message: "Max:",
validate: function( answer ) {
if ( isNaN( parseInt( answer ) ) || parseInt( answer ) < 1 ) {
return "You must enter a valid number.";
}
return true;
},
filter: function( response ) {
return parseInt( response );
},
when: function( answers ) {
return answers[ element + "_minmax_confirm" ];
}
}, {
type: "list",
name: element + "_format",
message: "Would you like to apply a display format to your array?",
choices: _this.array_formats,
default: "none",
when: function( answers ) {
return answers[ element + "_type" ] === "array";
},
filter: function( response ) {
if ( response === "none" ) {
return null;
} else {
return response;
}
}
}, {
type: "confirm",
name: element + "_hidden",
message: "Would you like this property to be hidden?",
default: false,
when: function( answers ) {
// Don't ask this question for now, possibly add back later
//return answers[ element + "_type" ] !== "$ref";
return false;
}
}, {
type: "confirm",
name: element + "_collapsed",
message: "Would you like this property to be collapsed?",
default: false,
when: function( answers ) {
// Don't ask this question for now, possibly add back later
//return answers[ element + "_type" ] === "object";
return false;
}
}, {
type: "confirm",
name: element + "_wysiwyg",
message: "Would you like the wysiwyg to be available?",
default: true,
when: function( answers ) {
return answers[ element + "_format" ] === "html";
}
}, {
type: "confirm",
name: element + "_dropdown",
message: "Is this a multiple choice property?",
default: false,
when: function( answers ) {
return answers[ element + "_type" ] === "string" && answers[ element + "_format" ] === null;
}
}, {
type: "input",
name: element + "_dropdown_items",
message: "List your dropdown properties (i.e. \"small, medium, large\"):",
validate: function( answer ) {
let duplicates =_this.findDuplicateValues( answer );
if ( duplicates.length > 0 ) {
return "This list cannot contain duplicate values.";
} else {
return true;
}
},
when: function( answers ) {
return answers[ element + "_dropdown" ];
},
filter: function( response ) {
return _this.stringToArrayAndTrim( response, "," );
}
}, {
type: "confirm",
name: element + "_required",
default: false,
message: "Is this a required field?"
}, {
type: "input",
name: element + "_grid",
message: "In a " + grid + " column grid, how many columns should this field occupy in the admin interface?:",
validate: function( answer ) {
if ( isNaN( parseInt( answer ) ) || parseInt( answer ) < 1 || parseInt( answer ) > grid ) {
return "Number should be between 1 and " + grid + ".";
}
return true;
},
filter: function( response ) {
return parseInt( response );
},
when: function( answers ) {
// Turn off this question for now, possibly add back later
//return answers[ element + "_type" ] !== "$ref" && answers[ element + "_hidden" ] === false;
return false;
}
} );
if ( nestLevel < 2 ) {
prompts.push( {
type: "input",
name: element + "_children",
message: "List any child elements you want for " + chalk.red(child) + " (i.e. \"my-el, my-other-el\"):",
validate: function( answer ) {
let duplicates =_this.findDuplicateValues( answer );
if ( duplicates.length > 0 ) {
return "This list cannot contain duplicate values.";
} else {
return true;
}
},
filter: function( response ) {
return _this.stringToArrayAndTrim( response, "," );
},
when: function( answers ) {
return answers[ element + "_type" ] === "object";
}
} );
}
return prompts;
}
};