Files
Pake/bin/utils/validate.ts
2023-06-22 14:36:02 +08:00

26 lines
557 B
TypeScript
Vendored

import fs from 'fs';
import { InvalidArgumentError } from 'commander';
import { normalizeUrl } from './url';
export function validateNumberInput(value: string) {
const parsedValue = Number(value);
if (isNaN(parsedValue)) {
throw new InvalidArgumentError('Not a number.');
}
return parsedValue;
}
export function validateUrlInput(url: string) {
const isFile = fs.existsSync(url);
if (!isFile) {
try {
return normalizeUrl(url);
} catch (error) {
throw new InvalidArgumentError(error.message);
}
}
return url;
}