UNPKG

@oblique/cli

Version:

Command Line Interface to manage Oblique projects

83 lines (81 loc) 3.83 kB
/** * @file Oblique, The front-end framework for your Swiss branded UI. * @copyright 2020 - 2025 Federal Office of Information Technology, Systems and Telecommunication FOITT {@link https://www.bit.admin.ch} * @version 14.1.2 (released on 2025-12-01, supported at least until 2026-09-30) * @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.configureOption = configureOption; const extra_typings_1 = require("@commander-js/extra-typings"); //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); command.addOption(configureOption(value, key)); } } else { throw new Error(`Schema for command ob ${command.name()} not found!`); } return command; } function configureOption(config, longFlag) { if (isEmpty(config.shortFlag) && isEmpty(longFlag)) { throw new Error('At least one of shortFlag or longFlag must be provided.'); } const shortFlag = (config.shortFlag ?? '').trim(); const longFlagClean = (longFlag || '').trim(); const flagValuePlaceholder = config.flagValuePlaceholder?.trim() ?? ''; const flags = [shortFlag ? `-${shortFlag}` : '', longFlagClean ? `--${longFlagClean}` : ''] .filter(flag => Boolean(flag)) .join(', '); const option = new extra_typings_1.Option([flags, flagValuePlaceholder].filter(Boolean).join(' ').trim(), config.description); const optionWithMandatory = addMandatory(option, config.mandatory); const optionWithDefault = addDefaultValue(optionWithMandatory, config.defaultValue, config.defaultValueDescription); return addChoices(optionWithDefault, config.choices); } function isEmpty(flag) { if (flag === undefined || flag === null) { return true; } return flag.trim() === ''; } 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); }