23
23
24
24
let ora
25
25
let closest
26
+ let minimist
26
27
try {
27
28
require ( 'zx/globals' )
28
29
ora = require ( 'ora' )
29
30
const fl = require ( 'fastest-levenshtein' )
30
31
closest = fl . closest
32
+ minimist = require ( 'minimist' )
31
33
} catch ( err ) {
32
34
console . log ( 'It looks like you didn\'t install the project dependencies, please run \'make setup\'' )
33
35
process . exit ( 1 )
@@ -56,30 +58,34 @@ const apis = require('../output/schema/schema.json')
56
58
. map ( endpoint => endpoint . name )
57
59
58
60
async function run ( ) {
61
+ const options = minimist ( process . argv . slice ( 2 ) , {
62
+ string : [ 'api' , 'type' , 'branch' ] ,
63
+ boolean : [ 'cache' ]
64
+ } )
65
+
59
66
spinner . text = 'Checking requirements'
60
67
61
- const noCache = argv . cache === false
68
+ const noCache = options . cache === false
62
69
const metadata = await readMetadata ( )
63
70
const lastRun = metadata . lastRun ? new Date ( metadata . lastRun ) : new Date ( 0 )
64
71
const isStale = lastRun . getTime ( ) + DAY < Date . now ( )
65
72
66
- if ( typeof argv . api !== 'string ') {
73
+ if ( options . api === ' ') {
67
74
spinner . fail ( 'You must specify the api, for example: \'make validate api=index type=request branch=main\'' )
68
75
process . exit ( 1 )
69
76
}
70
77
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 ) } '?` )
73
80
process . exit ( 1 )
74
81
}
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' ) {
78
84
spinner . fail ( 'You must specify the type (request or response), for example: \'make validate api=index type=request branch=main\'' )
79
85
process . exit ( 1 )
80
86
}
81
87
82
- if ( typeof argv . branch !== 'string' && typeof argv . branch !== 'number ') {
88
+ if ( options . branch === ' ') {
83
89
spinner . fail ( 'You must specify the branch, for example: \'make validate api=index type=request branch=main\'' )
84
90
process . exit ( 1 )
85
91
}
@@ -166,8 +172,7 @@ async function run () {
166
172
167
173
spinner . text = 'Running validations'
168
174
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
171
176
172
177
if ( noCache || isStale || metadata . branchName !== branchName ) {
173
178
metadata . lastRun = new Date ( )
@@ -177,20 +182,20 @@ async function run () {
177
182
await $ `node ${ path . join ( uploadRecordingsPath , 'download.js' ) } --branch ${ branchName } --git`
178
183
179
184
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 } `
181
186
}
182
187
183
188
cd ( tsValidationPath )
184
189
spinner . text = 'Validating endpoints'
185
190
// the ts validator will copy types.ts and schema.json autonomously
186
191
const flags = [ '--verbose' ]
187
- if ( argv . type === true ) {
192
+ if ( options . type === '' ) {
188
193
flags . push ( '--request' )
189
194
flags . push ( '--response' )
190
195
} else {
191
- flags . push ( `--${ argv . type } ` )
196
+ flags . push ( `--${ options . type } ` )
192
197
}
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 } `
194
199
195
200
cd ( path . join ( compilerPath , '..' ) )
196
201
if ( output . exitCode === 0 ) {
0 commit comments