wakitsu
Version:
Hobby project for managing anime watch list on Kitsu through CLI
124 lines • 4.14 kB
JavaScript
import { Printer } from '../printer/printer.js';
const _flags = [];
const rawArgs = process.argv;
const execPath = process.argv[0];
const sourcePath = process.argv[1];
const workingDir = process.cwd();
const userArgs = process.argv.slice(2);
const flagArgs = userArgs.filter((arg) => arg.indexOf('-') == 0 && arg[2] != '-');
const nonFlagArgs = userArgs.filter((arg) => !flagArgs.includes(arg)).map((arg) => arg.trim());
const cleanFlagArgs = flagArgs.map(removeLeadingDashes);
export class CLI {
static execPath = execPath;
static sourcePath = sourcePath;
static workingDir = workingDir;
static rawArgs = rawArgs;
static userArgs = userArgs;
static flagArgs = flagArgs;
static nonFlagArgs = nonFlagArgs;
static get flags() {
return _flags.slice(0);
}
static addFlag(flag) {
if (flag.type == 'simple' && flag.shortHelpDisplay == undefined) {
throw Error(`"--${flag.name[1]}" flag must have a "shortHelpDisplay"`);
}
const existingFlag = _flags.find((f) => {
for (const name of f.name) {
if (flag.name.includes(name)) {
return true;
}
}
return false;
});
if (existingFlag) {
throw Error(`One or more of these flag names already exists: ${flag.name
.map((f) => `"${f}"`)
.join(', ')}`);
}
_flags.push(flag);
}
static async tryExecFlags() {
if (!cleanFlagArgs[0]) {
const defaultFlag = _flags.find((f) => f.isDefault);
if (!defaultFlag)
throw Error('missing default flag');
defaultFlag.exec();
return true;
}
const flag = _flags.find((f) => f.name.includes(cleanFlagArgs[0]));
if (!validateFlag(flag)) {
process.exit(1);
}
flag.exec instanceof Promise ? await flag.exec() : flag.exec();
return true;
}
static validateSingleArg({ args, argHasArgs, flag }) {
argHasArgs = argHasArgs ?? false;
const argsLength = this.nonFlagArgs.length;
const errHeader = argsLength > 1 && !argHasArgs
? 'Too Many Arguments'
: args.every((a) => this.nonFlagArgs[0] != a)
? 'Invalid Arguments'
: argHasArgs && argsLength == 1
? 'Missing Arguments'
: null;
if (!errHeader)
return true;
Printer.printError('Make sure you use the correct syntax, as shown below:', errHeader);
flag.printSyntax();
return false;
}
}
function validateFlag(flag) {
if (!flag) {
Printer.printError(`;bc;Unknown Flag: ;by;${flagArgs[0]}`, 'Unknown Flag');
return false;
}
const { type } = flag;
return type == 'simple'
? isValidSingleFlag(0, flag)
: isValidSingleFlag(Infinity, flag) && isMultiArg(flag);
}
function isValidSingleFlag(numOfArgs, flag) {
if (nonFlagArgs.length > numOfArgs || flagArgs.length > 1) {
Printer.printError('Read the help below to learn the correct syntax', 'Invalid Flag Syntax');
flag.printHelp();
return false;
}
return true;
}
function isMultiArg(flag) {
if (!nonFlagArgs.length) {
Printer.printError('Read the help below to learn the correct syntax:', 'Missing Argument');
flag.printSyntax();
return false;
}
return true;
}
function removeLeadingDashes(str) {
if (str[0] == '-') {
return removeLeadingDashes(str.substring(1));
}
return str;
}
export class CLIFlag {
isDefault = false;
shortHelpDisplay = '';
printSyntax() {
Printer.print(this.getSyntaxHelpLogs() || this.getHelpLogs());
}
printHelp() {
const syntaxLogs = this.getSyntaxHelpLogs();
if (syntaxLogs) {
Printer.print([...this.getHelpLogs(), ...syntaxLogs]);
}
else {
Printer.print(this.getHelpLogs());
}
}
getSyntaxHelpLogs() {
return null;
}
}
//# sourceMappingURL=cli.js.map