Skip to content

Commit a93f4d7

Browse files
authored
Switch to minimist in run-validations.js (#3825)
This avoids parsing the `9.0` branch as a number, which is treated as `9`.
1 parent c2e8f05 commit a93f4d7

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

compiler/package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
"@types/node": "^17.0.12",
2727
"ava": "^4.1.0",
2828
"c8": "^7.11.0",
29-
"minimist": "^1.2.6",
3029
"prettier": "2.5.1",
3130
"prettier-plugin-organize-imports": "^4.0.0",
3231
"ts-node": "^10.4.0",
@@ -41,7 +40,8 @@
4140
"safe-stable-stringify": "^2.5.0",
4241
"semver": "^7.5.2",
4342
"ts-morph": "^13.0.3",
44-
"zx": "^4.3.0"
43+
"zx": "^4.3.0",
44+
"minimist": "^1.2.6"
4545
},
4646
"engines": {
4747
"node": ">=14"

compiler/run-validations.js

+19-14
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323

2424
let ora
2525
let closest
26+
let minimist
2627
try {
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

5860
async 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} --git`
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

Comments
 (0)