1+ /**
2+ * Example usage in root or frontend:
3+ * pnpm validate-json (npm run validate-json)
4+ * pnpm vaildate-json quotes others(npm run vaildate-json quotes others)
5+ * pnpm validate-json challenges fonts -p (npm run validate-json challenges fonts -- -p)
6+ */
7+
18// eslint-disable no-require-imports
29const fs = require ( "fs" ) ;
310const Ajv = require ( "ajv" ) ;
@@ -18,9 +25,8 @@ function findDuplicates(words) {
1825 return duplicates ;
1926}
2027
21- function validateOthers ( ) {
28+ function validateChallenges ( ) {
2229 return new Promise ( ( resolve , reject ) => {
23- //challenges
2430 const challengesSchema = {
2531 type : "array" ,
2632 items : {
@@ -105,7 +111,12 @@ function validateOthers() {
105111 console . log ( "Challenges list JSON schema is \u001b[31minvalid\u001b[0m" ) ;
106112 return reject ( new Error ( challengesValidator . errors [ 0 ] . message ) ) ;
107113 }
114+ resolve ( ) ;
115+ } ) ;
116+ }
108117
118+ function validateLayouts ( ) {
119+ return new Promise ( ( resolve , reject ) => {
109120 const charDefinitionSchema = {
110121 type : "array" ,
111122 minItems : 1 ,
@@ -118,7 +129,7 @@ function validateOthers() {
118129 maxItems : 2 ,
119130 items : { type : "string" , minLength : 1 , maxLength : 1 } ,
120131 } ;
121- //layouts
132+
122133 const layoutsSchema = {
123134 ansi : {
124135 type : "object" ,
@@ -469,13 +480,47 @@ function validateLanguages() {
469480 } ) ;
470481}
471482
472- function validateAll ( ) {
473- return Promise . all ( [ validateOthers ( ) , validateLanguages ( ) , validateQuotes ( ) ] ) ;
483+ function main ( ) {
484+ const args = process . argv . slice ( 2 ) ;
485+
486+ // oxlint-disable-next-line prefer-set-has this error doesnt make sense
487+ const flags = args . filter ( ( arg ) => arg . startsWith ( "-" ) ) ;
488+ const keys = args . filter ( ( arg ) => ! arg . startsWith ( "-" ) ) ;
489+
490+ const mainValidators = {
491+ quotes : validateQuotes ,
492+ languages : validateLanguages ,
493+ layouts : validateLayouts ,
494+ challenges : validateChallenges ,
495+ } ;
496+
497+ const validatorsIndex = {
498+ ...Object . fromEntries (
499+ Object . entries ( mainValidators ) . map ( ( [ k , v ] ) => [ k , [ v ] ] )
500+ ) ,
501+ // add arbitrary keys and validator groupings down here
502+ others : [ validateChallenges , validateLayouts ] ,
503+ } ;
504+
505+ // flags
506+ const validateAll =
507+ keys . length < 1 || flags . includes ( "--all" ) || flags . includes ( "-a" ) ;
508+ const passWithNoValidators =
509+ flags . includes ( "--pass-with-no-validators" ) || flags . includes ( "-p" ) ;
510+
511+ const tasks = new Set ( validateAll ? Object . values ( mainValidators ) : [ ] ) ;
512+ for ( const key of keys ) {
513+ if ( ! Object . keys ( validatorsIndex ) . includes ( key ) ) {
514+ console . error ( `There is no validator for key '${ key } '.` ) ;
515+ if ( ! passWithNoValidators ) process . exit ( 1 ) ;
516+ } else if ( ! validateAll ) {
517+ validatorsIndex [ key ] . forEach ( ( validator ) => tasks . add ( validator ) ) ;
518+ }
519+ }
520+
521+ if ( tasks . size > 0 ) {
522+ return Promise . all ( [ ...tasks ] . map ( ( validator ) => validator ( ) ) ) ;
523+ }
474524}
475525
476- module . exports = {
477- validateAll,
478- validateOthers,
479- validateLanguages,
480- validateQuotes,
481- } ;
526+ main ( ) ;
0 commit comments