-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.js
463 lines (398 loc) · 13 KB
/
setup.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
import fs from 'fs-extra'
import { replaceInFile, replaceInFileSync } from 'replace-in-file'
import { log } from 'console'
import enquirer from 'enquirer'
import chalk from 'chalk'
import './config.js'
class ModuleObject {
constructor(config) {
this.moduleName = config.moduleName
this.filesAndFolders = config.filesAndFolders instanceof Array
? config.filesAndFolders
: [config.filesAndFolders]
this.htmlConnectStrings = config.htmlConnectStrings
}
deleteFilesAndFolders() {
for (let fileOrFolder of this.filesAndFolders) {
fs.removeSync(fileOrFolder)
}
}
}
class VariableTemplate {
constructor(config) {
this.fields = config.fields
this.message = config.message
this.template = config.template
this.snippetName = config.snippetName
this.variableFilePath = config.variableFilePath
}
}
const
postcssFunctionsFile = globalThis.paths.root + 'plugins/other/postcssFunctions.js',
styles = {
normalize: globalThis.paths.sources.styles + 'normalize.pcss',
},
html = {
layout: globalThis.paths.sources.htmlComponents + 'layout.html',
index: globalThis.paths.sources.root + 'index.html',
}
logSomeImportantInConsole(
`Salute!
You will have to use some keys, such as:
${chalk.yellowBright('↑')} - focus up,
${chalk.yellowBright('↓')} - focus down,
${chalk.yellowBright('← →')} - choosing between elements on the same line,
${chalk.yellowBright('space')} - to select an option,
${chalk.yellowBright('⭾ (tab)')} - to move to a next element, for example, in templates.
`,
chalk.green
)
await enquirer.toggle({
message: chalk.italic('Any questions?'),
enabled: chalk.greenBright('Nope, i totally understand!'),
disabled: chalk.greenBright('Nope, i understand!'),
})
await includeModuleByQuestion(
'Whether you want to save the plugin...',
new ModuleObject({
moduleName: 'Just Validate',
filesAndFolders: globalThis.paths.sources.assets + 'justValidate/',
}),
new ModuleObject({
moduleName: 'scroll-timeline polyfill',
filesAndFolders: globalThis.paths.sources.assets + 'scroll-timeline.js',
}),
new ModuleObject({
moduleName: `Slider Swiper ${chalk.magenta(`[MANDATORY FOR MODULE 'STEP BY STEP BLOCK']`)
}`,
filesAndFolders: globalThis.paths.sources.assets + 'swiper/',
}),
new ModuleObject({
moduleName: 'Typed',
filesAndFolders: globalThis.paths.sources.assets + 'typed/',
}),
new ModuleObject({
moduleName: 'Input Mask',
filesAndFolders: globalThis.paths.sources.assets + 'inputmask/',
}),
new ModuleObject({
moduleName: 'Photo Swipe',
filesAndFolders: globalThis.paths.sources.assets + 'photoswipe/',
}),
new ModuleObject({
moduleName: 'No Ui Slider',
filesAndFolders: globalThis.paths.sources.assets + 'nouislider/',
})
)
await includeModuleByQuestion(
'Whether you want to save the module...',
new ModuleObject({
moduleName: 'Scripts for dialog',
filesAndFolders: [
globalThis.paths.sources.scripts + 'dialogs/',
globalThis.paths.sources.htmlComponents + 'modals.html',
],
}),
new ModuleObject({
moduleName: 'Tabs',
filesAndFolders: globalThis.paths.sources.scripts + 'tab/',
}),
new ModuleObject({
moduleName: 'Parallax by mouse',
filesAndFolders: globalThis.paths.sources.scripts + 'mouseParallax/',
}),
new ModuleObject({
moduleName: 'AutoScrollPadding',
filesAndFolders: globalThis.paths.sources.scripts + 'autoScrollPadding/',
}),
new ModuleObject({
moduleName: 'Tools for observer',
filesAndFolders: globalThis.paths.sources.scripts + 'observerTools/',
}),
new ModuleObject({
moduleName: 'Horizontal scroll by mouse wheel',
filesAndFolders: globalThis.paths.sources.scripts + 'horizontalMouseScroll.ts',
}),
new ModuleObject({
moduleName: 'Switching by swipe',
filesAndFolders: globalThis.paths.sources.scripts + 'toggleBySwipe/',
}),
new ModuleObject({
moduleName: 'Step By Step block',
filesAndFolders: globalThis.paths.sources.scripts + 'stepByStepBlock/',
}),
new ModuleObject({
moduleName: 'Infinite auto-scroll',
filesAndFolders: globalThis.paths.sources.scripts + 'infiniteScroll/',
}),
new ModuleObject({
moduleName: 'Horizontal scrolling with a mouse wheel',
filesAndFolders: globalThis.paths.sources.scripts + 'horizontalMouseScroll/',
}),
)
await setMainFont()
logSomeImportantInConsole(
`\nNow, i suggest you change the values of the main variables.\n`,
chalk.greenBright
)
await setVariables(
new VariableTemplate({
snippetName: 'html layout',
message: chalk.cyanBright(`Fill in the fields in the html-layout. \nSet the main language of pages. (${chalk.yellow.underline(html.layout)})`),
variableFilePath: html.layout,
fields: [
{ name: 'mainLangOfPages', initial: 'en' },
],
template: `<html lang="\${mainLangOfPages}">`
}),
new VariableTemplate({
snippetName: 'title of index page',
message: chalk.cyanBright('Title of index page...'),
variableFilePath: html.index,
fields: [
{ name: 'title', initial: '' },
],
template: `page_title="\${title}"`
}),
new VariableTemplate({
snippetName: 'stylesheetVariables',
message: chalk.cyanBright(
`Fill in some css variables ${chalk.yellow.underline(styles.normalize)}`),
variableFilePath: styles.normalize,
fields: [
{ name: 'mainTextColor', initial: 'black', },
{ name: 'backgroundColor', initial: 'white', },
],
template: `--main-text-color: \${mainTextColor};
--background: \${backgroundColor};`
}),
new VariableTemplate({
snippetName: 'another name',
message: chalk.cyanBright('Enter the width of the largest design layout.'
+ `\n${chalk.yellow.underline(postcssFunctionsFile)}`),
variableFilePath: postcssFunctionsFile,
fields: [
{ name: 'maxDesignLayoutWidth', initial: '1440', },
],
template: `const LAYOUT_WIDTH = \${maxDesignLayoutWidth}`,
}),
new VariableTemplate({
snippetName: 'some name',
message: chalk.cyanBright(
`Set the values for the ${chalk.red('content-inline-padding')} variable, which is intended to center the content.`
+ `\n${chalk.yellow.underline(styles.normalize)}`),
variableFilePath: styles.normalize,
fields: [
{ name: 'largePaddings', initial: '15vw', },
],
template:
`
@media (width > 1440px) {
--content-inline-padding: \${largePaddings};
}`,
}),
new VariableTemplate({
snippetName: 'some name 2',
message: chalk.cyan('PC screen...'),
variableFilePath: styles.normalize,
fields: [
{ name: 'defaultPaddings', initial: '13vw', },
],
template:
`
@media (1024px <= width <= 1440px) {
--content-inline-padding: \${defaultPaddings};
}`,
}),
new VariableTemplate({
snippetName: 'some name 2.1',
message: chalk.cyan('Small PC screens...'),
variableFilePath: styles.normalize,
fields: [
{ name: 'smallPcPaddings', initial: '10vw', },
],
template:
`
@media (769px <= width <= 1024px) {
--content-inline-padding: \${smallPcPaddings};
}`,
}),
new VariableTemplate({
snippetName: 'some name 3',
message: chalk.cyan('Tablets...'),
variableFilePath: styles.normalize,
fields: [
{ name: 'tabletPaddings', initial: '5vw', },
],
template:
`
@media (426px <= width <= 769px) {
--content-inline-padding: \${tabletPaddings};
}`,
}),
new VariableTemplate({
snippetName: 'some name 4',
message: chalk.cyan('Mobiles...'),
variableFilePath: styles.normalize,
fields: [
{ name: 'mobilePaddings', initial: '3vw', },
],
template:
`
@media (width <= 426px) {
--content-inline-padding: \${mobilePaddings};
}`,
}),
)
deleteUnnecessaryFilesAndFolders()
writeCompletelyPhrase()
async function includeModuleByQuestion(title, ...moduleObjects) {
let selectedModules = await enquirer.multiselect({
name: 'value',
message: chalk.greenBright(title),
limit: 5,
choices: moduleObjects.map(module => {
return {
name: module.moduleName, value: module.moduleName,
}
}),
footer: () => chalk.gray.italic('use ↑ and ↓ to switch, you can "scroll" this list')
})
for (let module of moduleObjects) {
let confirmedModuleName = selectedModules.find(answer => answer == module.moduleName)
if (!confirmedModuleName) module.deleteFilesAndFolders()
}
}
async function setVariables(...variableTemplates) {
for (let variableTemplate of variableTemplates) {
let result = await enquirer.snippet({
name: variableTemplate.snippetName,
message: variableTemplate.message + '\n',
required: true,
fields: variableTemplate.fields,
template: variableTemplate.template,
footer: () => chalk.gray.italic("use tab to move, when you're done, press enter")
})
let formattedTemplate = replaceEnquirerTemplateValues(
variableTemplate.template,
variableTemplate.fields,
result.values,
true
)
let newTemplate = replaceEnquirerTemplateValues(
variableTemplate.template,
variableTemplate.fields,
result.values
)
let templateStrings = formattedTemplate.split('\n')
let newTemplateStrings = newTemplate.split('\n')
for (let i = 0; i < templateStrings.length; i++) {
await replaceInFile({
files: variableTemplate.variableFilePath,
from: templateStrings[i],
to: newTemplateStrings[i],
})
}
}
}
async function setMainFont() {
await enquirer.toggle({
message: chalk.italic(`Now i will analyze your folder ${chalk.underline.yellow(globalThis.paths.sources.fontsFolder)}, make sure that you have added font files there.\n`),
enabled: chalk.greenBright(
`I added the font files to this folder.`
),
disabled: chalk.greenBright(
`I added the font files to this folder.`
),
})
let filesSources = fs.readdirSync(globalThis.paths.sources.fontsFolder)
if (filesSources.indexOf('.gitkeep') != -1) {
filesSources.splice(filesSources.indexOf('.gitkeep'), 1)
}
for (let i = 0; i < filesSources.length; i++) {
filesSources[i] +=
chalk.magenta(` (as ${filesSources[i].split('-')[0]})`)
}
if (filesSources?.length <= 0) return
let selectedFont = await enquirer.select({
name: 'set font',
message: 'Select the font that will be preloaded, and it will also be in the --main-font-family variable.' + '\n',
required: true,
choices: filesSources,
footer: () => chalk.gray.italic("use ↑↓ to move, when you're done, press enter")
})
replaceInFileSync({
files: styles.normalize,
from: `--main-font-family: arial;`,
to: `--main-font-family: '${selectedFont.split('-')[0]}'; `,
})
replaceInFileSync({
files: html.layout,
from: `href="./fonts/your_preloadedFontName.woff2"`,
to: `href="./fonts/${selectedFont.split('.')[0]}.woff2"`,
})
}
function deleteUnnecessaryFilesAndFolders() {
deleteFolder(globalThis.paths.root + 'README.md')
fs.createFileSync('README.md')
log(chalk.gray.italic('✅ The readme file are clean.'))
deleteFolder(globalThis.paths.output.root, 'Dist have been deleted.')
deleteFolder(globalThis.paths.root + 'snippets/', 'Snippets have been deleted.')
deleteFolder(globalThis.paths.sources.fontsFolder + '.gitkeep', 'Gitkeep in fonts have been deleted.')
if (!fs.readdirSync(globalThis.paths.sources.assets)?.length)
deleteFolder(globalThis.paths.sources.assets, 'assets have been deleted.')
}
function deleteFolder(folderPath, messageOnSuccessful) {
try {
fs.removeSync(folderPath)
if (messageOnSuccessful)
log(chalk.gray.italic('✅ ' + messageOnSuccessful))
}
catch (error) {
log(chalk.red('❌ ' + error))
}
}
function replaceEnquirerTemplateValues(template, fields, values, replaceNamesToDefaults) {
let newTemplate = template
if (replaceNamesToDefaults) {
for (let field of fields) {
newTemplate = newTemplate.replaceAll(
'${' + field.name + '}',
field.initial ?? ''
)
}
}
else {
for (let field of fields) {
newTemplate = newTemplate.replaceAll(
'${' + field.name + '}',
values[field.name] ?? field.initial
)
}
}
return newTemplate
}
function logSomeImportantInConsole(message, chalkColor) {
log(chalkColor(message))
}
function writeCompletelyPhrase() {
let
topPhrase = 'The setup is completely complete!',
middlePhrase = 'I wish You a successful job.',
bottomPhrase = '🎆🎆🎆',
positionOfTop = Math.round(process.stdout.columns / 2) - Math.round(topPhrase.length / 2),
positionOfMiddle = Math.round(process.stdout.columns / 2) - Math.round(middlePhrase.length / 2),
positionOfBottom = Math.round(process.stdout.columns / 2) - Math.round(bottomPhrase.length / 2)
topPhrase = ' '.repeat(positionOfTop) + topPhrase
middlePhrase = ' '.repeat(positionOfMiddle) + middlePhrase
bottomPhrase = ' '.repeat(positionOfBottom) + bottomPhrase
logSomeImportantInConsole(
'\n'
+ topPhrase + '\n'
+ middlePhrase + '\n'
+ bottomPhrase + '\n'
, chalk.greenBright
)
// An empty line to correct one error in the visualization
console.log('')
}