UNPKG

@oblique/cli

Version:

Command Line Interface to manage Oblique projects

120 lines (118 loc) 5.26 kB
/** * @file Oblique, The front-end framework for your Swiss branded UI. * @copyright 2020 - 2026 Federal Office of Information Technology, Systems and Telecommunication FOITT {@link https://www.bit.admin.ch} * @version 15.4.1 (released on 2026-07-09, supported at least until 2027-02-28) * @author Oblique team, FOITT, BS-BSC-EN4 <oblique@bit.admin.ch> * @license MIT {@link https://github.com/oblique-bit/oblique/blob/master/LICENSE} * @see https://oblique.bit.admin.ch */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.convertOptionPropertyNames = convertOptionPropertyNames; exports.addObNewCommandOptions = addObNewCommandOptions; exports.addObUpdateCommandOptions = addObUpdateCommandOptions; exports.configureOption = configureOption; const extra_typings_1 = require("@commander-js/extra-typings"); const flagPrefixLength = 2; //this is needed because commander sometimes converts the option to firstUpperCase function convertOptionPropertyNames(options) { const optionRecord = {}; for (const [key, value] of Object.entries(options)) { // this is needed because Commander adds the property name sometimes with first letter uppercase optionRecord[convertFirstLetterLowerCase(key)] = value; } return optionRecord; } function addObNewCommandOptions(schema, command) { if (Object.prototype.hasOwnProperty.call(schema, 'properties')) { for (const [key, value] of Object.entries(schema.properties)) { value.description = createOptionDescription(value.description, value.resources); const options = configureOption(value, key); for (const option of options) { command.addOption(option); } } } else { throw new Error(`Schema for command ob ${command.name()} not found!`); } return command; } function addObUpdateCommandOptions(schema, command) { if (!Object.hasOwn(schema, 'properties')) { throw new Error(`Schema for command ob ${command.name()} not found!`); } for (const [key, value] of Object.entries(schema.properties)) { value.description = createOptionDescription(value.description, value.resources); const options = configureOption(value, key); for (const option of options) { command.addOption(option); } } return command; } function configureOption(config, longFlag) { validateFlags(config, longFlag); const flags = buildFlags(config.shortFlag, longFlag); const options = createCommanderOption(config, flags); return options .map(option => addMandatory(option, config.mandatory)) .map(option => addDefaultValue(option, config.defaultValue, config.defaultValueDescription)) .map(option => addChoices(option, config.choices)); } function validateFlags(config, longFlag) { if (isBlank(config.shortFlag) && isBlank(longFlag)) { throw new Error('Either a shortFlag or a longFlag must be provided.'); } } function isBlank(value) { return value === undefined || value === null || value.trim().length === 0; } function buildFlags(shortFlag, longFlag) { const shortFlagClean = (shortFlag ?? '').trim(); const longFlagClean = (longFlag ?? '').trim(); return [shortFlagClean ? `-${shortFlagClean}` : '', longFlagClean ? `--${longFlagClean}` : ''] .filter(Boolean) .join(', '); } function createCommanderOption(config, longFlag) { if (config.type === 'boolean') { return [...createBooleanCommanderOptions(config, longFlag.slice(flagPrefixLength))]; } validateFlags(config, longFlag); return [createValueCommanderOption(config, longFlag)]; } function createBooleanCommanderOptions(config, flag) { return [ new extra_typings_1.Option(`--${flag} [boolean]`, config.description).conflicts(`--no-${flag}`), new extra_typings_1.Option(`--no-${flag}`, config.description).hideHelp(true).conflicts(`--${flag}`), ]; } function createValueCommanderOption(config, flags) { return new extra_typings_1.Option(`${flags} ${config.flagValuePlaceholder}`.trim(), config.description); } function createOptionDescription(description, resources) { let resourceDescription = resources && resources.length > 0 ? resources.join(' and ') : ''; resourceDescription = resources && resources.length > 0 ? ` See more information at ${resourceDescription}` : ''; return [description, resourceDescription].join(''); } function addMandatory(option, mandatory) { return mandatory === true ? option.makeOptionMandatory(true) : option; } function addDefaultValue(option, defaultValue, defaultValueDescription) { if (defaultValue !== undefined) { option.defaultValue = defaultValue; option.default(defaultValue, defaultValueDescription); } if (defaultValueDescription !== undefined) { option.defaultValueDescription = defaultValueDescription; option.default(defaultValue, defaultValueDescription); } return option; } function addChoices(option, choices) { return choices && choices.length > 0 ? option.choices(choices) : option; } function convertFirstLetterLowerCase(input) { return input.charAt(0).toLowerCase() + input.slice(1); }