kt-extendscript-builder
Version:
Vite based builder for transpile TypeScript to ExtendScript
54 lines (53 loc) • 2.26 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OptionsParser = void 0;
const yargs_1 = __importDefault(require("yargs"));
const helpers_1 = require("yargs/helpers");
const KTBuilderOptions_1 = require("./KTBuilderOptions");
/**
* A class responsible for collecting build options from command line arguments.
* It uses the yargs library to parse command line arguments based on predefined KTBuilderOptions.
*/
class OptionsParser {
/**
* Parses build options from the command line arguments.
*
* This method:
* 1. Creates a yargs instance with the process arguments (excluding the first two)
* 2. Registers all KTBuilderOptions with the yargs instance
* 3. Enables the help command and parses the arguments
*
* @returns A partial BuildOptions object containing the parsed command line options
*/
static parse() {
const userArgs = (0, helpers_1.hideBin)(process.argv);
// Modify arguments to include --preset if the first argument is positional
let modifiedArgs = [...userArgs];
if (userArgs.length > 0 && !userArgs[0].startsWith('-')) {
const presetValue = userArgs[0];
// Add --preset <value> at the beginning of the arguments and remove the positional argument
modifiedArgs = ['--preset', presetValue, ...userArgs.slice(1)];
}
// Create yargs instance with the modified arguments
const argv = (0, yargs_1.default)(modifiedArgs);
for (const option of KTBuilderOptions_1.KTBuilderOptions) {
argv.option(option.name, option);
}
return argv.help().parse();
}
static filter(options) {
const validOptions = KTBuilderOptions_1.KTBuilderOptions.map((option) => option.name);
const filteredOptions = {};
for (const key in options) {
const option = key;
if (validOptions.includes(option)) {
filteredOptions[option] = options[key];
}
}
return filteredOptions;
}
}
exports.OptionsParser = OptionsParser;