diff --git a/src/cli.ts b/src/cli.ts index 0e4362c..0f740bb 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -6,6 +6,7 @@ import * as dns from 'dns'; import * as log from 'loglevel'; import { buildNativefierApp } from './main'; +import { isArgFormatValid } from './helpers/helpers'; import { isWindows } from './helpers/helpers'; // package.json is `require`d to let tsc strip the `src` folder by determining @@ -68,6 +69,12 @@ function checkInternet(): void { if (require.main === module) { const sanitizedArgs = []; process.argv.forEach((arg) => { + if (isArgFormatValid(arg) === false) { + log.error( + `Invalid argument passed: ${arg} .\nNativefier supports short options (like "-n") and long options (like "--name"), all lowercase. Run "nativefier --help" for help.\nAborting`, + ); + process.exit(1); + } if (sanitizedArgs.length > 0) { const previousArg = sanitizedArgs[sanitizedArgs.length - 1]; diff --git a/src/helpers/helpers.test.ts b/src/helpers/helpers.test.ts new file mode 100644 index 0000000..0781c7b --- /dev/null +++ b/src/helpers/helpers.test.ts @@ -0,0 +1,39 @@ +import { isArgFormatValid } from './helpers'; + +describe('isArgFormatValid', () => { + test('is true for short arguments', () => { + expect(isArgFormatValid('-t')).toBe(true); + }); + + test('is false for improperly formatted short arguments', () => { + expect(isArgFormatValid('--t')).toBe(false); + }); + + test('is true for long arguments', () => { + expect(isArgFormatValid('--test')).toBe(true); + }); + + test('is false for improperly formatted long arguments', () => { + expect(isArgFormatValid('---test')).toBe(false); + }); + + test('is false for improperly formatted long arguments', () => { + expect(isArgFormatValid('-test')).toBe(false); + }); + + test('is true for long arguments with dashes', () => { + expect(isArgFormatValid('--test-run')).toBe(true); + }); + + test('is false for improperly formatted long arguments with dashes', () => { + expect(isArgFormatValid('--test--run')).toBe(false); + }); + + test('is true for long arguments with many dashes', () => { + expect(isArgFormatValid('--test-run-with-many-dashes')).toBe(true); + }); + + test('is false for improperly formatted excessively long arguments', () => { + expect(isArgFormatValid('--test--run--with--many--dashes')).toBe(false); + }); +}); diff --git a/src/helpers/helpers.ts b/src/helpers/helpers.ts index cc93d80..ccec0c5 100644 --- a/src/helpers/helpers.ts +++ b/src/helpers/helpers.ts @@ -139,3 +139,10 @@ export function getAllowedIconFormats(platform: string): string[] { log.debug(`Allowed icon formats when building for ${platform}:`, formats); return formats; } + +export function isArgFormatValid(arg: string): boolean { + return ( + /^-[a-z]$/i.exec(arg) !== null || + /^--[a-z]{2,}(-[a-z]{2,})*$/i.exec(arg) !== null + ); +}