From df9cbe7c7ae033e958067e0d3c5ad907fce63d1e Mon Sep 17 00:00:00 2001 From: Pavlo Karatsiuba Date: Sat, 1 Apr 2023 13:04:54 +0200 Subject: [PATCH] Stop script and throw error if used same cli parameter twice --- src/sanitize-argument.ts | 13 +++++++++++++ test/e2e/cmd.e2e.test.ts | 9 +++++++++ 2 files changed, 22 insertions(+) diff --git a/src/sanitize-argument.ts b/src/sanitize-argument.ts index 3b3e741e9..b657783f3 100644 --- a/src/sanitize-argument.ts +++ b/src/sanitize-argument.ts @@ -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) @@ -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 @@ -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.') diff --git a/test/e2e/cmd.e2e.test.ts b/test/e2e/cmd.e2e.test.ts index 969a6ef36..8914ff8be 100644 --- a/test/e2e/cmd.e2e.test.ts +++ b/test/e2e/cmd.e2e.test.ts @@ -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="test@test.test" --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="test@test.test" --mwUrl="https://en.wikipedia.org"`, { shell: true })).rejects.toThrow( + /Parameter mwUrl can't take value \"\[\"https:\/\/en.wikipedia.org\",\"https:\/\/en.wikipedia.org\"\]\"/, + ) + }) }) })