@ipp/cli
Version:
An image build orchestrator for the modern web
85 lines (84 loc) • 3.23 kB
JavaScript
;
/**
* Image Processing Pipeline - Copyright (c) Marcus Cemes
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createDidYouMeanMessage = exports.validateArgs = exports.parseArgs = void 0;
const chalk_1 = __importDefault(require("chalk"));
const leven_1 = __importDefault(require("leven"));
const yargs_1 = __importDefault(require("yargs"));
const constants_1 = require("../constants");
const exception_1 = require("../lib/exception");
const schema = {
input: {
type: "string",
alias: "i",
description: "The folder containing source images",
},
output: {
type: "string",
alias: "o",
description: "The folder to output images to",
},
config: {
type: "string",
alias: "c",
description: "The path to the IPP config file",
},
text: {
type: "boolean",
description: "Use simple text output",
},
};
const usage = "Usage: ipp [--config=<pathToConfigFile>]";
/** Parse args using Yargs, check that all flags are valid and return the args */
async function parseArgs() {
const { argv } = yargs_1.default.options(schema).usage(usage).version(constants_1.VERSION).epilogue(constants_1.REPOSITORY);
return argv;
}
exports.parseArgs = parseArgs;
/** Check that all flags appear in the options schema */
function validateArgs(args) {
const allowedFlags = getAllowedFlags(schema);
for (const key of Object.keys(args)) {
if (key === "_" || key === "$0")
continue;
if (allowedFlags.indexOf(key) !== -1)
continue;
throw new exception_1.CliException(`Unrecognised CLI flag "${key}"`, exception_1.CliExceptionCode.ARG_VALIDATION, `Unrecognised CLI flag: "${key}"`, `Run ${chalk_1.default.bold("ipp --help")} to get a list of allowed parameters` +
(0, exports.createDidYouMeanMessage)(key, allowedFlags));
}
}
exports.validateArgs = validateArgs;
/** Read allowed flags from the options schema */
function getAllowedFlags(schema) {
const flags = [];
for (const [key, options] of Object.entries(schema)) {
// Add the flag itself
flags.push(key);
// Add alias
if (typeof options.alias === "string")
flags.push(options.alias);
// Also add kebabCase variant, supported by yargs ("a-flag" -> "aFlag")
if (key.indexOf("-") !== -1)
flags.push(kebabToCamelCase(key));
}
return flags;
}
const createDidYouMeanMessage = (unrecognised, allowedOptions) => {
const suggestion = allowedOptions.find((option) => {
const steps = (0, leven_1.default)(option, unrecognised);
return steps < 3;
});
return suggestion ? `\n\nDid you mean ${chalk_1.default.bold(suggestion)}?` : "";
};
exports.createDidYouMeanMessage = createDidYouMeanMessage;
function kebabToCamelCase(text) {
return text.replace(/-([a-z])/g, (match) => match[1].toUpperCase());
}