130 lines
4.9 KiB
JavaScript
Executable File
130 lines
4.9 KiB
JavaScript
Executable File
import { findOption } from './newparser/findOption';
|
|
import { extendType } from './type';
|
|
import chalk from 'chalk';
|
|
import * as Result from './Result';
|
|
import { boolean as booleanIdentity } from './types';
|
|
/**
|
|
* A decoder from `string` to `boolean`
|
|
* works for `true` and `false` only.
|
|
*/
|
|
export const boolean = {
|
|
async from(str) {
|
|
if (str === 'true')
|
|
return true;
|
|
if (str === 'false')
|
|
return false;
|
|
throw new Error(`expected value to be either "true" or "false". got: "${str}"`);
|
|
},
|
|
displayName: 'true/false',
|
|
defaultValue: () => false,
|
|
};
|
|
export function fullFlag(config) {
|
|
var _a;
|
|
const decoder = extendType(boolean, config.type);
|
|
return {
|
|
description: (_a = config.description) !== null && _a !== void 0 ? _a : config.type.description,
|
|
helpTopics() {
|
|
var _a, _b, _c, _d;
|
|
let usage = `--${config.long}`;
|
|
if (config.short) {
|
|
usage += `, -${config.short}`;
|
|
}
|
|
const defaults = [];
|
|
if (config.env) {
|
|
const env = process.env[config.env] === undefined
|
|
? ''
|
|
: `=${chalk.italic(process.env[config.env])}`;
|
|
defaults.push(`env: ${config.env}${env}`);
|
|
}
|
|
try {
|
|
const defaultValueFn = (_a = config.defaultValue) !== null && _a !== void 0 ? _a : config.type.defaultValue;
|
|
const defaultValueIsSerializable = (_b = config.defaultValueIsSerializable) !== null && _b !== void 0 ? _b : config.type.defaultValueIsSerializable;
|
|
if (defaultValueFn && defaultValueIsSerializable) {
|
|
const defaultValue = defaultValueFn();
|
|
defaults.push('default: ' + chalk.italic(defaultValue));
|
|
}
|
|
}
|
|
catch (e) { }
|
|
return [
|
|
{
|
|
category: 'flags',
|
|
usage,
|
|
defaults,
|
|
description: (_d = (_c = config.description) !== null && _c !== void 0 ? _c : config.type.description) !== null && _d !== void 0 ? _d : 'self explanatory',
|
|
},
|
|
];
|
|
},
|
|
register(opts) {
|
|
opts.forceFlagLongNames.add(config.long);
|
|
if (config.short) {
|
|
opts.forceFlagShortNames.add(config.short);
|
|
}
|
|
},
|
|
async parse({ nodes, visitedNodes, }) {
|
|
var _a, _b;
|
|
const options = 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) {
|
|
return Result.err({
|
|
errors: [
|
|
{
|
|
nodes: options,
|
|
message: 'Expected 1 occurence, got ' + options.length,
|
|
},
|
|
],
|
|
});
|
|
}
|
|
const valueFromEnv = config.env ? process.env[config.env] : undefined;
|
|
let rawValue;
|
|
let envPrefix = '';
|
|
if (options.length === 0 && valueFromEnv !== undefined) {
|
|
rawValue = valueFromEnv;
|
|
envPrefix = `env[${chalk.italic(config.env)}]: `;
|
|
}
|
|
else if (options.length === 0 &&
|
|
typeof config.type.defaultValue === 'function') {
|
|
try {
|
|
return Result.ok(config.type.defaultValue());
|
|
}
|
|
catch (e) {
|
|
const message = `Default value not found for '--${config.long}': ${e.message}`;
|
|
return Result.err({
|
|
errors: [{ message, nodes: [] }],
|
|
});
|
|
}
|
|
}
|
|
else if (options.length === 1) {
|
|
rawValue = (_b = (_a = options[0].value) === null || _a === void 0 ? void 0 : _a.node.raw) !== null && _b !== void 0 ? _b : 'true';
|
|
}
|
|
else {
|
|
return Result.err({
|
|
errors: [
|
|
{ nodes: [], message: `No value provided for --${config.long}` },
|
|
],
|
|
});
|
|
}
|
|
const decoded = await Result.safeAsync(decoder.from(rawValue));
|
|
if (Result.isErr(decoded)) {
|
|
return Result.err({
|
|
errors: [
|
|
{
|
|
nodes: options,
|
|
message: envPrefix + decoded.error.message,
|
|
},
|
|
],
|
|
});
|
|
}
|
|
return decoded;
|
|
},
|
|
};
|
|
}
|
|
export function flag(config) {
|
|
return fullFlag({
|
|
type: booleanIdentity,
|
|
...config,
|
|
});
|
|
}
|
|
//# sourceMappingURL=flag.js.map
|