2323
2424let ora
2525let closest
26+ let minimist
2627try {
2728 require ( 'zx/globals' )
2829 ora = require ( 'ora' )
2930 const fl = require ( 'fastest-levenshtein' )
3031 closest = fl . closest
32+ minimist = require ( 'minimist' )
3133} catch ( err ) {
3234 console . log ( 'It looks like you didn\'t install the project dependencies, please run \'make setup\'' )
3335 process . exit ( 1 )
@@ -56,30 +58,34 @@ const apis = require('../output/schema/schema.json')
5658 . map ( endpoint => endpoint . name )
5759
5860async function run ( ) {
61+ const options = minimist ( process . argv . slice ( 2 ) , {
62+ string : [ 'api' , 'type' , 'branch' ] ,
63+ boolean : [ 'cache' ]
64+ } )
65+
5966 spinner . text = 'Checking requirements'
6067
61- const noCache = argv . cache === false
68+ const noCache = options . cache === false
6269 const metadata = await readMetadata ( )
6370 const lastRun = metadata . lastRun ? new Date ( metadata . lastRun ) : new Date ( 0 )
6471 const isStale = lastRun . getTime ( ) + DAY < Date . now ( )
6572
66- if ( typeof argv . api !== 'string ') {
73+ if ( options . api === ' ') {
6774 spinner . fail ( 'You must specify the api, for example: \'make validate api=index type=request branch=main\'' )
6875 process . exit ( 1 )
6976 }
7077
71- if ( ! apis . includes ( argv . api ) ) {
72- spinner . fail ( `The api '${ argv . api } ' does not exists, did you mean '${ closest ( argv . api , apis ) } '?` )
78+ if ( ! apis . includes ( options . api ) ) {
79+ spinner . fail ( `The api '${ options . api } ' does not exists, did you mean '${ closest ( options . api , apis ) } '?` )
7380 process . exit ( 1 )
7481 }
75-
76- // if true it's because the make target wasn't configured with a type argument
77- if ( argv . type !== true && argv . type !== 'request' && argv . type !== 'response' ) {
82+ // if the empty string it's because the make target wasn't configured with a type argument
83+ if ( options . type !== '' && options . type !== 'request' && options . type !== 'response' ) {
7884 spinner . fail ( 'You must specify the type (request or response), for example: \'make validate api=index type=request branch=main\'' )
7985 process . exit ( 1 )
8086 }
8187
82- if ( typeof argv . branch !== 'string' && typeof argv . branch !== 'number ') {
88+ if ( options . branch === ' ') {
8389 spinner . fail ( 'You must specify the branch, for example: \'make validate api=index type=request branch=main\'' )
8490 process . exit ( 1 )
8591 }
@@ -166,8 +172,7 @@ async function run () {
166172
167173 spinner . text = 'Running validations'
168174
169- const branchArg = argv . branch . toString ( )
170- const branchName = branchArg . startsWith ( '7.' ) ? '7.x' : branchArg
175+ const branchName = options . branch . startsWith ( '7.' ) ? '7.x' : options . branch
171176
172177 if ( noCache || isStale || metadata . branchName !== branchName ) {
173178 metadata . lastRun = new Date ( )
@@ -177,20 +182,20 @@ async function run () {
177182 await $ `node ${ path . join ( uploadRecordingsPath , 'download.js' ) } --branch ${ branchName } `
178183
179184 spinner . text = 'Fetching artifacts'
180- await $ `node ${ path . join ( cloneEsPath , 'index.js' ) } --branch ${ argv [ 'branch' ] } `
185+ await $ `node ${ path . join ( cloneEsPath , 'index.js' ) } --branch ${ branchName } `
181186 }
182187
183188 cd ( tsValidationPath )
184189 spinner . text = 'Validating endpoints'
185190 // the ts validator will copy types.ts and schema.json autonomously
186191 const flags = [ '--verbose' ]
187- if ( argv . type === true ) {
192+ if ( options . type === '' ) {
188193 flags . push ( '--request' )
189194 flags . push ( '--response' )
190195 } else {
191- flags . push ( `--${ argv . type } ` )
196+ flags . push ( `--${ options . type } ` )
192197 }
193- const output = await $ `node ${ path . join ( tsValidationPath , 'index.js' ) } --api ${ argv . api } --branch ${ branchName } ${ flags } `
198+ const output = await $ `node ${ path . join ( tsValidationPath , 'index.js' ) } --api ${ options . api } --branch ${ branchName } ${ flags } `
194199
195200 cd ( path . join ( compilerPath , '..' ) )
196201 if ( output . exitCode === 0 ) {
0 commit comments