mirror of
https://github.com/Llewellynvdm/nativefier.git
synced 2024-09-22 01:29:02 +00:00
Verifies short arguments do not contain an extra dash ( right: `-h`, error: `--h` ) Verifies long arguments contain at least two dashes ( right: `--help`, error: `-help` )
This commit is contained in:
parent
9b608d4d24
commit
27ecfcbeca
@ -6,6 +6,7 @@ import * as dns from 'dns';
|
|||||||
import * as log from 'loglevel';
|
import * as log from 'loglevel';
|
||||||
|
|
||||||
import { buildNativefierApp } from './main';
|
import { buildNativefierApp } from './main';
|
||||||
|
import { isArgFormatValid } from './helpers/helpers';
|
||||||
import { isWindows } from './helpers/helpers';
|
import { isWindows } from './helpers/helpers';
|
||||||
|
|
||||||
// package.json is `require`d to let tsc strip the `src` folder by determining
|
// 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) {
|
if (require.main === module) {
|
||||||
const sanitizedArgs = [];
|
const sanitizedArgs = [];
|
||||||
process.argv.forEach((arg) => {
|
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) {
|
if (sanitizedArgs.length > 0) {
|
||||||
const previousArg = sanitizedArgs[sanitizedArgs.length - 1];
|
const previousArg = sanitizedArgs[sanitizedArgs.length - 1];
|
||||||
|
|
||||||
|
39
src/helpers/helpers.test.ts
Normal file
39
src/helpers/helpers.test.ts
Normal file
@ -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);
|
||||||
|
});
|
||||||
|
});
|
@ -139,3 +139,10 @@ export function getAllowedIconFormats(platform: string): string[] {
|
|||||||
log.debug(`Allowed icon formats when building for ${platform}:`, formats);
|
log.debug(`Allowed icon formats when building for ${platform}:`, formats);
|
||||||
return 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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user