Skip to content

Commit

Permalink
Stop script and throw error if used same cli parameter twice
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-karatsiuba committed Apr 1, 2023
1 parent 0018c9f commit df9cbe7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/sanitize-argument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as QueryStringParser from 'querystring'
import { isValidEmail } from './util/index.js'
import * as path from 'path'
import { fileURLToPath } from 'url'
import { parameterDescriptions } from './parameterList.js'

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
Expand All @@ -18,6 +19,8 @@ export async function sanitize_all(argv: any) {
// extracting all arguments
const { articleList, addNamespaces, speed: _speed, adminEmail, mwUrl, customZimFavicon, optimisationCacheUrl, verbose, customZimLongDescription, customZimDescription } = argv

sanitizeDoubleUsedParameters(argv)

sanitize_articlesList_addNamespaces(articleList, addNamespaces)

// sanitizing verbose
Expand Down Expand Up @@ -99,6 +102,16 @@ export function sanitize_articlesList_addNamespaces(articlesList: string, addNam
}
}

export function sanitizeDoubleUsedParameters(options: object) {
const parameterKeys = Object.keys(parameterDescriptions)
const parametersWithArrayType = ['articleList', 'articleListToIgnore', 'addNamespaces']
for (const [optionKey, optionValue] of Object.entries(options)) {
if (parameterKeys.includes(optionKey) && !parametersWithArrayType.includes(optionKey) && Array.isArray(optionValue)) {
throw new Error(`Parameter ${optionKey} can't take value "${JSON.stringify(optionValue)}"`)
}
}
}

export function sanitize_speed(_speed: any) {
if (_speed && isNaN(_speed)) {
throw new Error('speed is not a number, please give a number value to --speed.')
Expand Down
9 changes: 9 additions & 0 deletions test/e2e/cmd.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,14 @@ describe('Exec Command With Bash', () => {
/verbose should be empty or one of \[info, log, warn, error, quiet\]/,
)
})

test('Exec Command With doubled options', async () => {
await expect(execa(`${mwo} --verbose --mwUrl="https://en.wikipedia.org" --adminEmail="[email protected]" --verbose=info`, { shell: true })).rejects.toThrow(
/Parameter verbose can't take value \"\[true,\"info\"\]\"/,
)
await expect(execa(`${mwo} --verbose --mwUrl="https://en.wikipedia.org" --adminEmail="[email protected]" --mwUrl="https://en.wikipedia.org"`, { shell: true })).rejects.toThrow(
/Parameter mwUrl can't take value \"\[\"https:\/\/en.wikipedia.org\",\"https:\/\/en.wikipedia.org\"\]\"/,
)
})
})
})

0 comments on commit df9cbe7

Please sign in to comment.