🎨 Refactoring CLI

This commit is contained in:
Tw93
2023-06-22 14:36:02 +08:00
parent 87e91ecbf5
commit b2d0d7a2ae
38 changed files with 1466 additions and 5120 deletions

18
bin/utils/validate.ts vendored
View File

@@ -1,23 +1,25 @@
import * as Commander from 'commander';
import { normalizeUrl } from './url.js';
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 Commander.InvalidArgumentError('Not a number.');
throw new InvalidArgumentError('Not a number.');
}
return parsedValue;
}
export function validateUrlInput(url: string) {
if(!fs.existsSync(url)) {
const isFile = fs.existsSync(url);
if (!isFile) {
try {
return normalizeUrl(url)
return normalizeUrl(url);
} catch (error) {
throw new Commander.InvalidArgumentError(error.message);
throw new InvalidArgumentError(error.message);
}
} else {
return url;
}
return url;
}