forked from haxtheweb/create
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.js
More file actions
905 lines (874 loc) · 36.2 KB
/
create.js
File metadata and controls
905 lines (874 loc) · 36.2 KB
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
#!/usr/bin/env node
// forces middleware into CLI mode so we don't automatically perform certain operations like pathing context
process.env.haxcms_middleware = "node-cli";
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as p from '@clack/prompts';
import color from 'picocolors';
import { haxIntro, communityStatement } from "./lib/statements.js";
import { log, consoleTransport, logger } from "./lib/logging.js";
import { auditCommandDetected } from './lib/programs/audit.js';
import { partyCommandDetected } from './lib/programs/party.js';
import { webcomponentProcess, webcomponentCommandDetected, webcomponentActions } from "./lib/programs/webcomponent.js";
import { siteActions, siteNodeOperations, siteProcess, siteCommandDetected, siteThemeList } from "./lib/programs/site.js";
import { camelToDash, exec, interactiveExec, writeConfigFile, readConfigFile, getTimeDifference } from "./lib/utils.js";
import * as hax from "@haxtheweb/haxcms-nodejs";
const HAXCMS = hax.HAXCMS;
// check the last time this was run
let lastTime = readConfigFile('hax-cli-last-run');
// first run in the event it is null assume now
if (!lastTime) {
lastTime = new Date().toISOString();
}
// check time difference broken down by unit
const timeSince = getTimeDifference(new Date().toISOString(), lastTime);
// write last run so that we know last time they were here
writeConfigFile('hax-cli-last-run', new Date().toISOString());
import { program } from "commander";
import packageJson from '../package.json' with { type: 'json' };
let sysGit = true;
exec('which git', error => {
if (error) {
sysGit = false;
}
});
async function main() {
let wcReg = {};
let regPath = path.join(__dirname, './lib/wc-registry.json');
if (fs.existsSync(regPath)) {
try {
wcReg = JSON.parse(fs.readFileSync(regPath));
}
catch(e) {
// no registry for testing, probably busted package
}
}
var commandRun = {};
program
.option('--')
.option('--v', 'Verbose output')
.option('--debug', 'Output for developers')
.option('--format <char>', 'Output format; json (default), yaml')
.option('--path <char>', 'where to perform operation')
.option('--name <char>', 'name of the project/web component')
.option('--npm-client <char>', 'npm client to use (must be installed) npm, yarn, pnpm', 'npm')
.option('--y', 'yes to all questions')
.option('--skip', 'skip frills like animations')
.option('--quiet', 'remove console logging')
.option('--auto', 'yes to all questions, alias of y')
.option('--no-i', 'prevent interactions / sub-process, good for scripting')
.option('--to-file <char>', 'redirect command output to a file')
.option('--no-extras', 'skip all extra / automatic command processing')
.option('--root <char>', 'root location to execute the command from')
// options for webcomponent
.option('--org <char>', 'organization for package.json')
.option('--author <char>', 'author for site / package.json')
.option('--writeHaxProperties', 'Write haxProperties for the element')
// options for site
.option('--import-site <char>', 'URL of site to import')
.option('--import-structure <char>', `import method to use:\n\rpressbooksToSite\n\relmslnToSite\n\rhaxcmsToSite\n\rnotionToSite\n\rgitbookToSite\n\revolutionToSite\n\rhtmlToSite\n\rdocxToSite`)
.option('--node-op <char>', 'node operation to perform')
.option('--item-id <char>', 'node ID to operate on')
.option('--domain <char>', 'published domain name')
.option('--title-scrape <char>', 'CSS Selector for `title` in resource')
.option('--content-scrape <char>', 'CSS Selector for `body` in resource')
.option('--items-import <char>', 'import items from a file / site')
.option('--recipe <char>', 'path to recipe file')
.option('--custom-theme-name <char>', 'custom theme name')
.option('--custom-theme-template <char>', 'custom theme template; (options: base, polaris-flex, polaris-sidebar)')
// options for rsync
.option('--source <char>', 'rsync source directory or remote path')
.option('--destination <char>', 'rsync destination directory or remote path')
.option('--exclude <char>', 'comma-separated patterns to exclude from rsync')
.option('--dry-run', 'perform rsync dry run')
.option('--delete', 'delete extraneous files from destination')
// options for party
.option('--repos <char...>', 'repositories to clone')
.version(packageJson.version)
.helpCommand(true);
// default command which runs interactively
program
.command('start')
.description('Select which hax sub-program to run')
.action(() => {
commandRun = {
command: 'start',
arguments: {},
options: {}
};
});
program
.command('update')
.description('hax cli self update')
.option('--y', 'yes to all questions')
.action(() => {
commandRun = {
command: 'update',
arguments: {},
options: {}
};
});
program
.command('serve')
.description('Launch HAXsite in development mode (http://localhost)')
.action(() => {
commandRun = {
command: 'serve',
arguments: {
action: 'serve'
},
options: {}
};
});
// site operations and actions
let strActions = '';
siteActions().forEach(action => {
strActions+= `${action.value} - ${action.label}` + "\n\r";
});
let siteProg = program
.command('site')
.description('create or administer a HAXsite')
.argument('[action]', 'Actions to perform on site include:' + "\n\r" + strActions)
.action((action) => {
commandRun = {
command: 'site',
arguments: {},
options: {}
};
if (action) {
commandRun.arguments.action = action;
commandRun.options.skip = true;
}
})
.option('--v', 'Verbose output')
.option('--debug', 'Output for developers')
.option('--format <char>', 'Output format; json (default), yaml')
.option('--path <char>', 'where to perform operation')
.option('--npm-client <char>', 'npm client to use (must be installed) npm, yarn, pnpm', 'npm')
.option('--y', 'yes to all questions')
.option('--skip', 'skip frills like animations')
.option('--quiet', 'remove console logging')
.option('--auto', 'yes to all questions, alias of y')
.option('--no-i', 'prevent interactions / sub-process, good for scripting')
.option('--to-file <char>', 'redirect command output to a file')
.option('--no-extras', 'skip all extra / automatic command processing')
.option('--root <char>', 'root location to execute the command from')
.option('--import-site <char>', 'URL of site to import')
.option('--import-structure <char>', `import method to use:\n\rpressbooksToSite\n\relmslnToSite\n\rhaxcmsToSite\n\rnotionToSite\n\rgitbookToSite\n\revolutionToSite\n\rhtmlToSite\n\rdocxToSite`)
.option('--name <char>', 'name of the site (when creating a new one)')
.option('--domain <char>', 'published domain name')
.option('--node-op <char>', 'node operation to perform')
.option('--title-scrape <char>', 'CSS Selector for `title` in resource')
.option('--content-scrape <char>', 'CSS Selector for `body` in resource')
.option('--items-import <char>', 'import items from a file / site')
.option('--recipe <char>', 'path to recipe file')
.option('--custom-theme-name <char>', 'custom theme name')
.option('--custom-theme-template <char>', 'custom theme template (options: base, polaris-flex, polaris-sidebar)')
.option('--source <char>', 'rsync source directory or remote path')
.option('--destination <char>', 'rsync destination directory or remote path')
.option('--exclude <char>', 'comma-separated patterns to exclude from rsync')
.option('--dry-run', 'perform rsync dry run')
.option('--delete', 'delete extraneous files from destination')
.version(packageJson.version);
let siteNodeOps = siteNodeOperations();
for (var i in siteNodeOps) {
program.option(`--${camelToDash(siteNodeOps[i].value)} <char>`, `${siteNodeOps[i].label}`)
siteProg.option(`--${camelToDash(siteNodeOps[i].value)} <char>`, `${siteNodeOps[i].label}`)
}
// webcomponent program
let strWebcomponentActions = '';
webcomponentActions().forEach(action => {
strWebcomponentActions += `${action.value} - ${action.label}` + "\n\r";
});
program
.command('wc')
.alias('webcomponent')
.description('Create Lit based web components, with HAX recommendations')
.argument('[action]', 'Actions to perform on web component include:' + "\n\r" + strWebcomponentActions)
.action((action) => {
commandRun = {
command: 'webcomponent',
arguments: {},
options: {}
};
// if name set, populate
if (action) {
commandRun.arguments.action = action;
commandRun.options.skip = true;
}
})
.option('--path <char>', 'path the project should be created in')
.option('--org <char>', 'organization for package.json')
.option('--author <char>', 'author for site / package.json')
.option('--name <char>', 'name of the web component')
.option('--writeHaxProperties', 'Write haxProperties for the element')
.option('--to-file <char>', 'redirect command output to a file')
.option('--no-extras', 'skip all extra / automatic command processing')
.option('--no-i', 'prevent interactions / sub-process, good for scripting')
.option('--root <char>', 'root location to execute the command from')
.version(packageJson.version);
// audit program
program
.command('audit')
.description('Audits web components for compliance with DDD (HAX design system)')
.action(() => {
commandRun = {
command: 'audit',
arguments: {},
options: {}
};
})
.option('--debug', 'Output for developers')
.version(packageJson.version);
program
.command('party')
.description('Party time! Join the HAX community and get involved!')
.argument('[action]', 'Actions to perform on web component include:' + "\n\r" + "test")
.action((action) => {
commandRun = {
command: 'party',
arguments: {},
options: {}
};
if (action) {
commandRun.arguments.action = action;
commandRun.options.skip = true;
}
})
.option('--root <char>', 'root location to execute the command from')
.option('--y', 'yes to all questions')
.option('--auto', 'yes to all questions, alias of y')
.option('--repos <char...>', 'repositories to clone')
.version(packageJson.version);
// process program arguments
program.parse();
commandRun.options = {...commandRun.options, ...program.opts()};
// this is a false positive bc of the no-extras flag. no-extras does not imply true if not there
// but that's how the CLI works. This then bombs things downstream. Its good to be false but NOT
// good to be true since we need an array of options
if (commandRun.options.extras === true) {
delete commandRun.options.extras;
}
// allow execution of the command from a different location
if (commandRun.options.root) {
process.chdir(commandRun.options.root);
}
// bridge to log so we can respect this setting
if (commandRun.options.quiet) {
process.haxquiet = true;
// don't output to the console any messages on this run since told to be silent
// logging still happens to log files
logger.remove(consoleTransport);
}
if (commandRun.options.debug) {
log(commandRun, 'debug');
}
// auto and y assume same thing
if (commandRun.options.y || commandRun.options.auto) {
commandRun.options.y = true;
commandRun.options.auto = true;
}
let author = '';
// should be able to grab if not predefined
try {
let value = await exec(`git config user.name`);
author = value.stdout.trim();
}
catch(e) {
log(`
git user name not configured. Run the following to do this:\n
git config --global user.name "namehere"\n
git config --global user.email "email@here`, 'debug');
}
if (!commandRun.options.path && commandRun.options.skip) {
commandRun.options.path = process.cwd();
}
// if we skip stuff then set org/author automatically
if (commandRun.options.skip || commandRun.options.auto) {
if (!commandRun.options.org) {
commandRun.options.org = '';
}
if (!commandRun.options.author) {
commandRun.options.author = author;
}
}
// validate theme cli commands
if (commandRun.options.theme !== 'custom-theme' && (commandRun.options.customThemeName || commandRun.options.customThemeTemplate)) {
program.error(color.red('ERROR: You can only use the --custom-theme-name option with --theme custom-theme'));
}
let packageData = {};
let testPackages = [
path.join(process.cwd(), 'package.json'),
path.join(process.cwd(), '../', 'package.json'),
path.join(process.cwd(), '../', '../', 'package.json'),
]
// test within reason, for package.json files seeing if anything is available to suggest
// that we might be in a local package or a monorepo.
while (testPackages.length > 0) {
let packLoc = testPackages.shift();
if (fs.existsSync(packLoc)) {
try {
packageData = {...JSON.parse(fs.readFileSync(packLoc)), ...packageData};
// assume we are working on a web component / existing if we find this key
if (packageData.hax && packageData.hax.cli) {
commandRun.program = 'webcomponent';
}
// leverage these values if they exist downstream
if (packageData.npmClient) {
commandRun.options.npmClient = packageData.npmClient;
}
else {
commandRun.options.npmClient = 'npm';
}
// see if we're in a monorepo
if (packageData.useWorkspaces && packageData.workspaces && packageData.workspaces.packages && packageData.workspaces.packages[0]) {
if (!commandRun.options.quiet) {
p.intro(`${color.bgBlack(color.white(` Monorepo detected : Setting relative defaults `))}`);
}
commandRun.options.isMonorepo = true;
commandRun.options.auto = true;
// assumed if monorepo
if (commandRun.command === "audit") {
auditCommandDetected(commandRun);
}
else {
commandRun.command = 'webcomponent';
commandRun.options.path = path.join(process.cwd(), packageData.workspaces.packages[0].replace('/*',''));
if (packageData.orgNpm) {
commandRun.options.org = packageData.orgNpm;
}
commandRun.options.gitRepo = packageData.repository.url;
commandRun.options.author = packageData.author.name ? packageData.author.name : author;
}
}
} catch (err) {
console.error(err)
}
}
}
if (commandRun.options.debug) {
log(packageData, 'debug');
}
// test for updating to latest or just run the command
if (commandRun.command === "update") {
await testForUpdates(commandRun);
}
else if (commandRun.command === 'audit') {
let customPath = null;
// test for haxcms context
if (await hax.systemStructureContext() && fs.existsSync(`${process.cwd()}/custom`)) {
customPath = path.join(process.cwd(), 'custom');
}
auditCommandDetected(commandRun, customPath)
}
else if (commandRun.command === 'party') {
commandRun.options.author = author;
await partyCommandDetected(commandRun);
}
// CLI works within context of the site if one is detected, otherwise we can do other things
else if (await hax.systemStructureContext()) {
if (commandRun.command === 'serve'){
commandRun.program = 'serve';
commandRun.options.skip = true;
await siteCommandDetected(commandRun);
} else {
commandRun.program = 'site';
commandRun.options.skip = true;
await siteCommandDetected(commandRun);
}
}
else if (packageData && (packageData.customElements || packageData.hax && packageData.hax.cli) && packageData.scripts.start) {
if (commandRun.command === 'serve'){
commandRun.program = 'serve';
commandRun.options.skip = true;
await webcomponentCommandDetected(commandRun, packageData);
} else {
commandRun.program = 'webcomponent';
commandRun.options.skip = true;
await webcomponentCommandDetected(commandRun, packageData);
}
}
else {
if (commandRun.command === 'start' && !commandRun.options.y && !commandRun.options.auto && !commandRun.options.skip && !commandRun.options.quiet) {
await haxIntro();
}
// it's been 7 days since last check, do the check and offer command to resolve if needed
if (timeSince.days > 7) {
// check for updates
let execOut = await exec(`npm view @haxtheweb/create version`);
let updateCheck = execOut.stdout.trim();
if (updateCheck !== packageJson.version && !commandRun.options.quiet) {
p.intro(`${color.bgBlack(color.white(` HAX cli updates available! `))}`);
p.intro(`Current version: ${packageJson.version}`);
p.intro(`Latest version: ${updateCheck}`);
p.intro(`Run ${color.bold(color.black(color.bgGreen('hax update')))} to update to the latest version`);
}
}
let activeProject = null;
let project = { type: null };
while (project.type !== 'quit' && project.type !== 'update') {
if (activeProject) {
if (!commandRun.options.quiet) {
p.note(` 🧙🪄 BE GONE ${color.bold(color.black(color.bgGreen(activeProject)))} sub-process daemon! 🪄 + ✨ 👹 = 💀 `);
}
// ensure if we were automatically running the command we end
if (commandRun.options.y) {
if (!commandRun.options.quiet) {
communityStatement();
}
process.exit(0);
}
// otherwise null to reset the program to run again
commandRun = {
command: null,
arguments: {},
options: {}
};
}
if (['site', 'webcomponent', 'audit'].includes(commandRun.command)) {
project = {
type: commandRun.command
};
}
else if (commandRun.options.i) {
let buildOptions = [
{ value: 'webcomponent', label: '🏗️ Create a Web Component' },
{ value: 'site', label: '🏡 Create a HAXsite'},
{ value: 'party', label: '🎉 Join the HAX community' },
{ value: 'update', label: '🤓 Check for hax cli updates' },
{ value: 'quit', label: '🚪 Quit' },
];
project = await p.group(
{
type: ({ results }) =>
p.select({
message: !activeProject ? `What should we build?` : `Thirsty for more? What should we create now?`,
initialValue: 'webcomponent',
required: true,
options: buildOptions,
}),
},
{
onCancel: () => {
if (!commandRun.options.quiet) {
p.cancel('🧙🪄 Merlin: Canceling cli.. HAX ya later');
communityStatement();
}
process.exit(0);
},
}
);
}
else if (!commandRun.options.i) {
process.exit(0);
}
if (project.type === "update") {
await testForUpdates(commandRun);
} else if (project.type === "party") {
await partyCommandDetected(commandRun);
}
// detect being in a haxcms scaffold. easiest way is _sites being in this directory
// set the path automatically so we skip the question
else if ((commandRun.command === "site") && fs.existsSync(`${process.cwd()}/_sites`)) {
if (!commandRun.options.quiet) {
p.intro(`${color.bgBlack(color.white(` HAXcms detected : Path set automatically `))}`);
}
commandRun.options.path = `${process.cwd()}/_sites`;
}
activeProject = project.type;
// silly but this way we don't have to take options for quitting
if (['site', 'webcomponent'].includes(project.type)) {
// global spot for core themes list
let coreThemes = await siteThemeList(true);
project = await p.group(
{
type: ({ results }) => {
return new Promise((resolve, reject) => {
resolve( activeProject);
});
},
path: ({ results }) => {
let initialPath = `${process.cwd()}`;
if (!commandRun.options.path && !commandRun.options.auto && !commandRun.options.skip) {
return p.text({
message: `What folder will your ${(commandRun.command === "webcomponent" || results.type === "webcomponent") ? "project" : "site"} live in?`,
placeholder: initialPath,
required: true,
validate: (value) => {
if (!value) {
return "Path is required (tab writes default)";
}
if (!fs.existsSync(value)) {
return `${value} does not exist. Select a valid folder`;
}
}
});
}
},
name: ({ results }) => {
const reservedNames = ["annotation-xml", "color-profile", "font-face", "font-face-src", "font-face-uri", "font-face-format", "font-face-name", "missing-glyph"];
if (!commandRun.arguments.action) {
let placeholder = "mysite";
let message = "Site name:";
if (commandRun.command === "webcomponent" || results.type === "webcomponent") {
placeholder = "my-element";
message = "Element name:";
}
return p.text({
message: message,
placeholder: placeholder,
required: true,
validate: (value) => {
if (!value) {
return "Name is required (tab writes default)";
}
if(reservedNames.includes(value)) {
return `Reserved name ${color.bold(value)} cannot be used`
}
if (value.toLocaleLowerCase() !== value) {
return "Name must be lowercase";
}
if (/^\d/.test(value)) {
return "Name cannot start with a number";
}
if (/[`~!@#$%^&*()_=+\[\]{}|;:\'",<.>\/?\\]/.test(value)) {
return "No special characters allowed in name";
}
if (value.indexOf(' ') !== -1) {
return "No spaces allowed in name";
}
if (results.type === "webcomponent" && (value.indexOf('-') === -1 || value.replace('--', '') !== value || value[0] === '-' || value[value.length-1] === '-')) {
return "Name must include at least one `-` and must not start or end name.";
}
// test that this is not an existing element we use in the registry
if (results.type === "webcomponent" && wcReg[value]) {
return "Name is already a web component in the wc-registry published for HAX.";
}
// Check for any other syntax errors
if(results.type === "webcomponent" && !/^[a-z][a-z0-9.\-]*\-[a-z0-9.\-]*$/.test(value)){
return `Name must follow the syntax ${color.bold("my-component")}`;
}
// assumes auto was selected in CLI
let joint = process.cwd();
if (commandRun.options.path) {
joint = commandRun.options.path;
}
else if (results.path) {
joint = results.path;
}
if (fs.existsSync(path.join(joint, value))) {
return `${path.join(joint, value)} exists, rename this project`;
}
}
});
}
if (commandRun.arguments.action) {
let value = commandRun.arguments.action;
if (!value) {
program.error(color.red("Name is required (tab writes default)"));
process.exit(1);
}
if(reservedNames.includes(value)) {
program.error(color.red(`Reserved name ${color.bold(value)} cannot be used`));
process.exit(1);
}
if (value.toLocaleLowerCase() !== value) {
program.error(color.red("Name must be lowercase"));
process.exit(1);
}
if (/^\d/.test(value)) {
program.error(color.red("Name cannot start with a number"));
process.exit(1);
}
if (value.indexOf(' ') !== -1) {
program.error(color.red("No spaces allowed in name"));
process.exit(1);
}
if (results.type === "webcomponent" && (value.indexOf('-') === -1 || value.replace('--', '') !== value || value[0] === '-' || value[value.length-1] === '-')) {
program.error(color.red("Name must include at least one `-` and must not start or end name."));
process.exit(1);
}
// test that this is not an existing element we use in the registry
if (results.type === "webcomponent" && wcReg[value]) {
program.error(color.red("Name is already a web component in the wc-registry published for HAX."));
process.exit(1);
}
// Check for any other syntax errors
if(results.type === "webcomponent" && !/^[a-z][a-z0-9.\-]*\-[a-z0-9.\-]*$/.test(value)){
program.error(color.red(`Name must follow the syntax ${color.bold("my-component")}`));
process.exit(1);
}
// assumes auto was selected in CLI
let joint = process.cwd();
if (commandRun.options.path) {
joint = commandRun.options.path;
}
else if (results.path) {
joint = results.path;
}
if (fs.existsSync(path.join(joint, value))) {
program.error(color.red(`${path.join(joint, value)} exists, rename this project`));
process.exit(1);
}
}
},
org: ({ results }) => {
if (results.type === "webcomponent" && !commandRun.options.org && !commandRun.options.auto && !commandRun.options.skip) {
let initialOrg = '@yourOrganization';
return p.text({
message: 'Organization:',
placeholder: initialOrg,
required: false,
validate: (value) => {
if (value && !value.startsWith('@')) {
return "Organizations are not required, but organizations must start with @ if used";
}
}
});
}
},
author: ({ results }) => {
if (!commandRun.options.author && !commandRun.options.auto && !commandRun.options.skip) {
return p.text({
message: 'Author:',
required: false,
initialValue: author,
});
}
},
theme: async({ results }) => {
if (results.type === "site" && !commandRun.options.theme) {
// support having no theme but autoselecting
if (commandRun.options.auto && commandRun.options.skip) {
commandRun.options.theme = coreThemes[0].value;
}
else {
return p.select({
message: "Theme:",
required: false,
options: coreThemes,
initialValue: coreThemes[0]
})
}
}
else if (results.type === "site" && commandRun.options.theme) {
if (coreThemes.filter((item => item.value === commandRun.options.theme)).length === 0) {
program.error(color.red('Theme is not in the list of valid themes'));
process.exit(1);
}
}
},
customThemeName: async ({ results }) => {
if (results.theme === "custom-theme") {
let tmpCustomName = await p.text({
message: 'Theme Name:',
placeholder: `custom-${commandRun.arguments.action ? commandRun.arguments.action : results.name}-theme`,
required: false,
validate: (value) => {
if (!value) {
return "Theme name is required (tab writes default)";
}
if(coreThemes.some(theme => theme.value === value)) {
return "Theme name is already in use";
}
if (/^\d/.test(value)) {
return "Theme name cannot start with a number";
}
if (/[A-Z]/.test(value)) {
return "No uppercase letters allowed in theme name";
}
if (value.indexOf(' ') !== -1) {
return "No spaces allowed in theme name";
}
}
})
return tmpCustomName;
}
else if (results.type === "site") {
// need to validate theme from CLI arguments
let value = `${commandRun.options.customThemeName ? commandRun.options.customThemeName : (results.name ? results.name : commandRun.arguments.action)}`;
if (!value) {
program.error(color.red("Theme name is required (tab writes default)"));
}
if(coreThemes.some(theme => theme.value === value)) {
program.error(color.red("Theme name is already in use"));
}
if (/^\d/.test(value)) {
program.error(color.red("Theme name cannot start with a number"));
}
if (/[A-Z]/.test(value)) {
program.error(color.red("No uppercase letters allowed in theme name"));
}
if (value.indexOf(' ') !== -1) {
program.error(color.red("No spaces allowed in theme name"));
}
}
},
customThemeTemplate: ({ results }) => {
if (results.theme === "custom-theme") {
const options = [
{ value: 'base', label: 'Vanilla Theme with Hearty Documentation' },
{ value: 'polaris-flex', label: 'Minimalist Theme with Horizontal Nav' },
{ value: 'polaris-sidebar', label: 'Content-Focused Theme with Flexible Sidebar' },
]
return p.select({
message: 'Template:',
required: false,
options: options,
initialValue: `${results.theme}`
})
}
},
extras: ({ results }) => {
if (!commandRun.options.auto && commandRun.options.i) {
let options = [];
let initialValues = [];
if (commandRun.command === "webcomponent" || results.type === "webcomponent") {
options = [
{ value: 'launch', label: 'Launch project', hint: 'recommended' },
{ value: 'install', label: `Install dependencies via ${commandRun.options.npmClient}`, hint: 'recommended' },
{ value: 'git', label: 'Apply version control via git', hint: 'recommended' },
];
initialValues = ['launch', 'install', 'git']
if (!sysGit || commandRun.options.isMonorepo) {
options.pop();
initialValues.pop();
}
}
else {
options = [
{ value: 'launch', label: 'Launch project on creation', hint: 'recommended' },
];
initialValues = ['launch']
}
return p.multiselect({
message: 'Additional setup',
initialValues: initialValues,
options: options,
required: false,
})
}
},
},
{
onCancel: () => {
if (!commandRun.options.quiet) {
p.cancel('🧙🪄 Merlin: Canceling cli.. HAX ya later');
communityStatement();
}
process.exit(0);
},
}
);
// merge cli options with project options assume this is NOT a monorepo
// but spread will overwrite if needed
if (commandRun.command === 'webcomponent' && !commandRun.arguments.action) {
project = {
isMonorepo: false,
...project,
...commandRun.options,
};
}
else {
project = {
isMonorepo: false,
...project,
...commandRun.arguments,
...commandRun.options,
};
}
project.year = new Date().getFullYear();
project.version = packageJson.version;
if (!project.name && commandRun.arguments.action) {
project.name = commandRun.arguments.action;
}
// resolve site vs multi-site
switch (project.type) {
case 'site':
// only set path if not already set, normalize across clack and commander
if (commandRun.options.path && !project.path) {
project.path = commandRun.options.path;
}
if (!project.path) {
project.path = process.cwd();
}
if (!commandRun.options.path) {
commandRun.options.path = project.path;
}
await siteProcess(commandRun, project);
break;
case 'webcomponent':
await webcomponentProcess(commandRun, project);
break;
}
}
}
if (!commandRun.options.quiet && project.type !== 'update') {
communityStatement();
}
}
}
// check for updates
async function testForUpdates(commandRun) {
let execOut = await exec(`npm view @haxtheweb/create version`);
let latest = execOut.stdout.trim();
if (latest !== packageJson.version) {
if (!commandRun.options.quiet) {
p.intro(`${color.bgBlack(color.white(` HAX cli updates available! `))}`);
p.intro(`Current version: ${packageJson.version}`);
p.intro(`Latest version: ${latest}`);
}
let runUpdates = { answer: false };
if (!commandRun.options.y) {
runUpdates = await p.group(
{
answer: ({ results }) =>
p.confirm({
message: `Do you want to update the cli to the latest version? (${latest})`,
initialValue: true,
}),
},
{
onCancel: () => {
if (!commandRun.options.quiet) {
p.cancel('🧙🪄 Merlin: Leaving so soon? HAX ya later');
p.outro(`
🧙 Upgrade at any time: ${color.yellow('npm install --global @haxtheweb/create')}
💡 ${color.bold(color.white(`Never. Stop. Innovating.`))}
`);
}
process.exit(0);
},
}
);
}
else {
runUpdates.answer = true; // automatic
}
// ensure they wanted to run them
if (runUpdates.answer) {
await interactiveExec('npm', ['install', '--global', '@haxtheweb/create']);
if (!commandRun.options.quiet) {
p.outro(`
🔮 HAX CLI updated to : ${color.yellow(latest)}
🧙 Type ${color.yellow('hax help')} for latest commands
💡 ${color.bold(color.white(`Never. Stop. Innovating.`))}
`);
}
}
else {
if (!commandRun.options.quiet) {
p.outro(`
🧙 Upgrade at any time: ${color.yellow('npm install --global @haxtheweb/create')}
💡 ${color.bold(color.white(`Never. Stop. Innovating.`))}
`);
}
}
}
else {
if (!commandRun.options.quiet) {
p.intro(`${color.bgBlack(color.white(` HAX CLI (${packageJson.version}) is up to date `))}`);
}
}
}
main().catch(console.error);