UNPKG

@salesforce/plugin-settings

Version:
86 lines 3.25 kB
/* * Copyright 2025, Salesforce, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { SfError, Config } from '@salesforce/core'; import { toHelpSection } from '@salesforce/sf-plugins-core'; import Levenshtein from 'fast-levenshtein'; export const CONFIG_HELP_SECTION = toHelpSection('CONFIGURATION VARIABLES', ...new Set(Config.getAllowedProperties().map((k) => k.newKey ?? k.key))); export const calculateSuggestion = (userEnteredConfig) => { // we'll use this array to keep track of which key is the closest to the users entered value. // keys closer to the index 0 will be a closer guess than keys indexed further from 0 // an entry at 0 would be a direct match, an entry at 1 would be a single character off, etc. const index = []; Config.getAllowedProperties() .map((k) => k.newKey ?? k.key) .map((k) => (index[Levenshtein.get(userEnteredConfig, k)] = k)); return index.find((item) => item !== undefined) ?? ''; }; export const buildFailureMsg = (name, err, value) => { const error = SfError.wrap(typeof err !== 'string' && !(err instanceof Error) ? 'Unknown error' : err); return { name, success: false, value, error, message: error.message.replace(/\.\.$/, '.'), }; }; export const buildSuccessMsg = (configInfo) => { if (Array.isArray(configInfo.value)) { throw new SfError(`Config ${configInfo.key} is an Array. It should be a primitive.`); } if (typeof configInfo.value === 'object') { throw new SfError(`Config ${configInfo.key} is an Object. It should be a primitive.`); } return { name: configInfo.key, key: configInfo.key, value: configInfo.value, path: configInfo.path, success: true, location: configInfo.location, }; }; export const output = (ux, responses, command, verbose = false) => { if (!ux.outputEnabled) { return; } if (responses.length === 0) { ux.log('No results found'); return; } const data = responses.map((response) => ({ name: response.name, ...(verbose ? { location: response.location ?? '' } : {}), ...(command === 'unset' ? {} : { value: response.value }), ...(command === 'list' ? {} : { success: response.success }), ...(responses.some((msg) => msg.error) ? { message: response.error?.message ?? '', } : {}), })); ux.table({ data, title: commandToTitleMapping[command], }); }; const commandToTitleMapping = { set: 'Set Config', unset: 'Unset Config', list: 'List Config', get: 'Get Config', }; //# sourceMappingURL=config.js.map