archieve-projects/微信机器人/node_modules/cmd-ts/dist/cjs/option.js

151 lines
6.2 KiB
JavaScript
Executable File

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.option = void 0;
const findOption_1 = require("./newparser/findOption");
const chalk_1 = __importDefault(require("chalk"));
const Result = __importStar(require("./Result"));
const types_1 = require("./types");
function fullOption(config) {
var _a;
return {
description: (_a = config.description) !== null && _a !== void 0 ? _a : config.type.description,
helpTopics() {
var _a, _b, _c, _d, _e;
const displayName = (_a = config.type.displayName) !== null && _a !== void 0 ? _a : 'value';
let usage = `--${config.long}`;
if (config.short) {
usage += `, -${config.short}`;
}
usage += ` <${displayName}>`;
const defaults = [];
if (config.env) {
const env = process.env[config.env] === undefined
? ''
: `=${chalk_1.default.italic(process.env[config.env])}`;
defaults.push(`env: ${config.env}${env}`);
}
const defaultValueFn = (_b = config.defaultValue) !== null && _b !== void 0 ? _b : config.type.defaultValue;
if (defaultValueFn) {
try {
const defaultValue = defaultValueFn();
if ((_c = config.defaultValueIsSerializable) !== null && _c !== void 0 ? _c : config.type.defaultValueIsSerializable) {
defaults.push('default: ' + chalk_1.default.italic(defaultValue));
}
else {
defaults.push('optional');
}
}
catch (e) { }
}
return [
{
category: 'options',
usage,
defaults,
description: (_e = (_d = config.description) !== null && _d !== void 0 ? _d : config.type.description) !== null && _e !== void 0 ? _e : 'self explanatory',
},
];
},
register(opts) {
opts.forceOptionLongNames.add(config.long);
if (config.short) {
opts.forceOptionShortNames.add(config.short);
}
},
async parse({ nodes, visitedNodes, }) {
var _a, _b;
const options = findOption_1.findOption(nodes, {
longNames: [config.long],
shortNames: config.short ? [config.short] : [],
}).filter((x) => !visitedNodes.has(x));
options.forEach((opt) => visitedNodes.add(opt));
if (options.length > 1) {
const error = {
message: 'Too many times provided. Expected 1, got: ' + options.length,
nodes: options,
};
return Result.err({ errors: [error] });
}
const valueFromEnv = config.env ? process.env[config.env] : undefined;
const option = options[0];
let rawValue;
let envPrefix = '';
const defaultValueFn = (_a = config.defaultValue) !== null && _a !== void 0 ? _a : config.type.defaultValue;
if (option === null || option === void 0 ? void 0 : option.value) {
rawValue = option.value.node.raw;
}
else if (valueFromEnv !== undefined) {
rawValue = valueFromEnv;
envPrefix = `env[${chalk_1.default.italic(config.env)}]: `;
}
else if (!option && typeof defaultValueFn === 'function') {
try {
return Result.ok(defaultValueFn());
}
catch (e) {
const message = `Default value not found for '--${config.long}': ${e.message}`;
return Result.err({
errors: [
{
nodes: [],
message,
},
],
});
}
}
else {
const raw = (option === null || option === void 0 ? void 0 : option.type) === 'shortOption'
? `-${option === null || option === void 0 ? void 0 : option.key}`
: `--${(_b = option === null || option === void 0 ? void 0 : option.key) !== null && _b !== void 0 ? _b : config.long}`;
return Result.err({
errors: [
{
nodes: options,
message: `No value provided for ${raw}`,
},
],
});
}
const decoded = await Result.safeAsync(config.type.from(rawValue));
if (Result.isErr(decoded)) {
return Result.err({
errors: [
{ nodes: options, message: envPrefix + decoded.error.message },
],
});
}
return Result.ok(decoded.value);
},
};
}
function option(config) {
return fullOption({
type: types_1.string,
...config,
});
}
exports.option = option;
//# sourceMappingURL=option.js.map