UNPKG

easy-cli-framework

Version:

A framework for building CLI applications that are robust and easy to maintain. Supports theming, configuration files, interactive prompts, and more.

68 lines (64 loc) 2.31 kB
'use strict'; var command = require('./command.js'); /** * A command to add a configure command to the CLI that will save the configuration * * @template TParams The params for the command * @template TGlobalParams The global params for the CLI * @extends EasyCLICommand * * @example * ```typescript * new EasyCLIConfigureCommand(config, 'configure', { * globalKeysToUse: ['verbose'], // Use the verbose key * aliases: ['config', 'cfg', 'setup'] // Alias the command to 'config', 'cfg', and 'setup' * prompts: { * // Prompts to ask the user for an input for the env key * env: { * describe: 'What environent are you setting?', * type: 'string', * prompt: 'always', * demandOption: true, * }, * ... * }, * flags: { * // Add a flag to the command * profile: { * type: 'string', * describe: 'The profile to use', * demandOption: false, // Make the flag optional * default: 'my-profile', // Set a default value * } * }, * }); * ``` */ class EasyCLIConfigureCommand extends command.EasyCLICommand { /** * Creates a new configure command * @param {EasyCLIConfigFile} config The configuration file to use * @param {string} [name='configure'] The name of the command * @param {ConfigureCommandOptions<TGlobalParams, TParams>} [options={}] The options for the command */ constructor(config, name = 'configure', options = {}) { const { globalKeysToUse = [], callback, ...commandOptions } = options; const handler = async (params, theme) => { const logger = theme === null || theme === undefined ? undefined : theme.getLogger(); const keys = [...this.getKeys(), ...globalKeysToUse]; const clean = Object.entries(params).reduce((acc, [key, value]) => { if (keys.includes(key)) { acc[key] = value; } return acc; }, {}); logger === null || logger === undefined ? undefined : logger.success('Saving configuration'); await config.save(clean); if (callback) { await callback(params); } }; super(name, handler, commandOptions); } } exports.EasyCLIConfigureCommand = EasyCLIConfigureCommand;