UNPKG

@tywalk/pcf-helper

Version:

Command line helper for building and publishing PCF controls to Dataverse.

162 lines (161 loc) 6.77 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getArg = getArg; exports.getArgValue = getArgValue; exports.preprocessArgs = preprocessArgs; exports.resolveEnvironment = resolveEnvironment; exports.applyArgumentPreprocessing = applyArgumentPreprocessing; exports.addCommonOptions = addCommonOptions; exports.addPathAndEnvironmentOptions = addPathAndEnvironmentOptions; exports.addProfileOption = addProfileOption; exports.resolvePathAndEnvironment = resolvePathAndEnvironment; exports.resolveProfileOnly = resolveProfileOnly; const color_logger_1 = __importDefault(require("@tywalk/color-logger")); const configUtil_1 = require("./configUtil"); function getArg(args, arg) { const index = args.indexOf(arg); if (index !== -1 && index + 1 < args.length) { return args[index + 1]; } return undefined; } function getArgValue(args, argOpts, defaultIfExists) { var _a; const arg = args.find(a => argOpts.includes(a)); if (typeof arg === 'undefined') { return undefined; } const argIndex = args.indexOf(arg) + 1; return (_a = args.at(argIndex)) !== null && _a !== void 0 ? _a : defaultIfExists; } /** * Preprocesses command line arguments to handle deprecated flags * @param args - Raw command line arguments * @returns Object containing processed args and deprecation flags */ function preprocessArgs(args) { const processed = [...args]; let hadDeprecatedEnv = false; // Handle deprecated -env flag (single dash) by converting to --env (double dash) for (let i = 0; i < processed.length; i++) { if (processed[i] === '-env') { hadDeprecatedEnv = true; processed[i] = '--env'; } } return { args: processed, hadDeprecatedEnv }; } /** * Resolves environment value from options with proper deprecation warnings * @param options - Parsed Commander.js options * @param hadDeprecatedEnv - Whether the deprecated -env flag was used * @returns Resolved environment string */ function resolveEnvironment(options, hadDeprecatedEnv) { // Check if deprecated --env flag was used if (options.env && options.environment) { color_logger_1.default.warn('⚠️ Both --env (deprecated) and --environment flags provided. Using --environment value.'); return options.environment; } else if (options.env) { if (hadDeprecatedEnv) { color_logger_1.default.warn('⚠️ The -env flag is DEPRECATED. Please use -e or --environment instead.'); } else { color_logger_1.default.warn('⚠️ The --env flag is DEPRECATED. Please use -e or --environment instead.'); } return options.env; } else { return options.environment || ''; } } /** * Applies argument preprocessing for backward compatibility * @param originalArgv - The original process.argv */ function applyArgumentPreprocessing(originalArgv) { const { args: processedArgs, hadDeprecatedEnv } = preprocessArgs(originalArgv.slice(2)); process.argv = [...originalArgv.slice(0, 2), ...processedArgs]; return { hadDeprecatedEnv }; } /** * Adds common CLI options to a Commander.js command * @param command - Commander.js command instance * @returns The command with common options added */ function addCommonOptions(command) { return command .option('-V, --verbose', 'enable verbose logging') .option('-t, --timeout <milliseconds>', 'timeout in milliseconds', (value) => { const num = Number(value); if (isNaN(num) || num <= 0) { throw new Error('Timeout must be a positive number'); } return value; }); } /** * Adds path and environment options to a Commander.js command. * * Note: `--path` is NOT marked required here even though most commands need * it, because profile values can supply it. Callers must validate after * `resolvePathEnvironmentOptions` that a path was ultimately resolved. */ function addPathAndEnvironmentOptions(command) { return addCommonOptions(command) .option('-p, --path <path>', 'path to solution folder') .option('-e, --environment <environment>', 'environment name') .option('--env <environment>', '[DEPRECATED: use -e/--environment] environment name (deprecated)') .option('-P, --profile <name>', 'named profile from pcf-helper.config.json to use for defaults'); } /** * Adds the `--profile` option to any command. Use when a command doesn't use * `addPathAndEnvironmentOptions` (for example, init and session). */ function addProfileOption(command) { return command.option('-P, --profile <name>', 'named profile from pcf-helper.config.json to use for defaults'); } /** * Resolves `--path` and `--environment` using the precedence: * CLI flag > profile value > '' (empty) * * Deprecated `--env` is still honored; see `resolveEnvironment`. * * Loads the pcf-helper config from ~/.pcf-helper/config.json and the project * working directory. Callers should validate that `path` is non-empty if their * command requires it. */ function resolvePathAndEnvironment(options, hadDeprecatedEnv) { var _a, _b, _c; const config = (0, configUtil_1.loadPcfHelperConfig)(); const { name: profileName, profile } = (0, configUtil_1.resolveProfile)(options.profile, config.merged); const explicitEnv = resolveEnvironment(options, hadDeprecatedEnv); const resolvedEnv = explicitEnv !== '' ? explicitEnv : (_a = profile === null || profile === void 0 ? void 0 : profile.environment) !== null && _a !== void 0 ? _a : ''; const resolvedPath = (_c = (_b = options.path) !== null && _b !== void 0 ? _b : profile === null || profile === void 0 ? void 0 : profile.path) !== null && _c !== void 0 ? _c : ''; if (profileName) { color_logger_1.default.log(`🧭 Using profile "${profileName}" from pcf-helper config.`); } return { path: resolvedPath, environment: resolvedEnv, profileName, config, }; } /** * Resolves just a profile (without path/env) for commands like init and * session where the option surface differs. Returns the profile object (or * undefined if no profile was requested/configured) plus the loaded config. */ function resolveProfileOnly(profileName) { const config = (0, configUtil_1.loadPcfHelperConfig)(); const { name, profile } = (0, configUtil_1.resolveProfile)(profileName, config.merged); if (name) { color_logger_1.default.log(`🧭 Using profile "${name}" from pcf-helper config.`); } return { profileName: name, profile, config }; }