Skip to content

Commit

Permalink
refactor: no need for Optional<ServerOptions> (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
fvsch authored Nov 25, 2024
1 parent b0d52f0 commit 5565d68
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 30 deletions.
34 changes: 17 additions & 17 deletions src/args.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CLI_OPTIONS, PORTS_CONFIG } from './constants.ts';
import type { HttpHeaderRule, OptionSpec, ServerOptions } from './types.d.ts';
import { intRange } from './utils.ts';
import { intRange, printValue } from './utils.ts';

export class CLIArgs {
#map: Array<[string, string]> = [];
Expand Down Expand Up @@ -101,31 +101,31 @@ function normalizeExt(value: string = ''): string {
}

export function parseArgs(args: CLIArgs, onError: (msg: string) => void): Partial<ServerOptions> {
const invalid = (optName = '', input = '') => {
const value =
typeof input === 'string' ? `'${input.replaceAll(`'`, `\'`)}'` : JSON.stringify(input);
onError(`invalid ${optName} value: ${value}`);
const invalid = (optName: string, input: any) => {
onError(`invalid ${optName} value: ${printValue(input)}`);
};

const getStr = ({ names: argNames, negate: negativeArg }: OptionSpec) => {
if (negativeArg && args.has(negativeArg)) return;
const input = args.get(argNames);
const getStr = ({ names, negate }: OptionSpec) => {
if (negate && args.has(negate)) return;
const input = args.get(names);
if (input != null) return input.trim();
};

const getList = ({ names: argNames, negate: negativeArg }: OptionSpec) => {
if (negativeArg && args.has(negativeArg)) return [];
const input = args.all(argNames);
const getList = ({ names, negate }: OptionSpec) => {
if (negate && args.has(negate)) return [];
const input = args.all(names);
if (input.length) return splitOptionValue(input);
};

const getBool = ({ names: argNames, negate: negativeArg }: OptionSpec, emptyValue?: boolean) => {
if (negativeArg && args.has(negativeArg)) return false;
const input = args.get(argNames);
if (input == null) return;
const getBool = ({ names, negate }: OptionSpec, emptyValue?: boolean) => {
if (negate && args.has(negate)) return false;
const input = args.get(names);
const value = strToBool(input, emptyValue);
if (value != null) return value;
else invalid(argNames.at(-1), input);
if (typeof value === 'boolean') {
return value;
} else if (typeof input === 'string' && input.length > 0) {
invalid(names.at(-1)!, input);
}
};

const options: Partial<ServerOptions> = {
Expand Down
10 changes: 2 additions & 8 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { isAbsolute, resolve } from 'node:path';

import { DEFAULT_OPTIONS, PORTS_CONFIG } from './constants.ts';
import type { HttpHeaderRule, ServerOptions } from './types.d.ts';
import { printValue } from './utils.ts';

export function serverOptions(
options: ServerOptions,
Expand Down Expand Up @@ -36,19 +37,12 @@ export function serverOptions(

export class OptionsValidator {
#errorCb;

constructor(onError: (msg: string) => void) {
this.#errorCb = onError;
}

#error(msg: string, input: any) {
let dbg = input;
if (typeof input === 'object') {
dbg = JSON.stringify(input);
} else if (typeof input === 'string') {
dbg = `'${dbg.replaceAll("'", "\\'")}'`;
}
this.#errorCb(`${msg}: ${dbg}`);
this.#errorCb(`${msg}: ${printValue(input)}`);
}

#arr<T>(input: T[] | undefined, msg: string, validFn: (item: T) => boolean): T[] | undefined {
Expand Down
2 changes: 1 addition & 1 deletion src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class FileResolver {
#dirList = false;
#excludeMatcher: PathMatcher;

constructor(options: { root: string } & Partial<ServerOptions>) {
constructor(options: ServerOptions) {
if (typeof options.root !== 'string') {
throw new Error('Missing root directory');
} else if (!isAbsolute(options.root)) {
Expand Down
9 changes: 9 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ export function once<T = any>(fn: () => T): () => T {
};
}

export function printValue(input: any) {
if (typeof input === 'object') {
return JSON.stringify(input);
} else if (typeof input === 'string') {
return `'${input.replaceAll("'", "\\'")}'`;
}
return String(input);
}

export function trimSlash(
input: string = '',
config: { start?: boolean; end?: boolean } = { start: true, end: true },
Expand Down
6 changes: 2 additions & 4 deletions test/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import {
import type { ServerOptions } from '../src/types.d.ts';
import { errorList } from '../src/utils.ts';

type InputOptions = Partial<ServerOptions> & { root: string };

function makeValidChecks(isValidFn: (input: any) => boolean) {
const msg = (expected: boolean, input: any) => {
return [
Expand Down Expand Up @@ -313,7 +311,7 @@ suite('serverOptions', () => {
test('preserves valid options', () => {
const onError = errorList();

const testOptions1: InputOptions = {
const testOptions1: ServerOptions = {
root: cwd(),
dirList: false,
gzip: false,
Expand All @@ -324,7 +322,7 @@ suite('serverOptions', () => {
...testOptions1,
});

const testOptions2: InputOptions = {
const testOptions2: ServerOptions = {
root: cwd(),
ext: ['.htm', '.TXT'],
dirFile: ['page.md', 'Index Page.html'],
Expand Down

0 comments on commit 5565d68

Please sign in to comment.