UNPKG

@ui5/cli

Version:
142 lines (134 loc) 5.11 kB
import chalk from "chalk"; import {isLogLevelEnabled} from "@ui5/logger"; import ConsoleWriter from "@ui5/logger/writers/Console"; export default function(cli) { cli.usage("Usage: ui5 <command> [options]") .demandCommand(1, "Command required") .option("config", { alias: "c", describe: "Path to project configuration file in YAML format", type: "string" }) .option("dependency-definition", { describe: "Path to a YAML file containing the project's dependency tree. " + "This option will disable resolution of node package dependencies.", type: "string" }) .option("workspace-config", { describe: "Path to workspace configuration file in YAML format", type: "string" }) .option("workspace", { alias: "w", describe: "Name of the workspace configuration to use", default: "default", type: "string" }) .option("loglevel", { alias: "log-level", describe: "Set the logging level", default: "info", type: "string", choices: ["silent", "error", "warn", "info", "perf", "verbose", "silly"] }) .option("verbose", { describe: "Enable verbose logging.", default: false, type: "boolean" }) .option("perf", { describe: "Enable performance measurements and related logging.", default: false, type: "boolean" }) .option("silent", { describe: "Disable all log output.", default: false, type: "boolean" }) .coerce([ // base.js "config", "dependency-definition", "workspace-config", "workspace", "log-level", // tree.js, build.js & serve.js "framework-version", "cache-mode", // build.js "dest", // serve.js "open", "port", "key", "cert", ], (arg) => { // If an option is specified multiple times, yargs creates an array for all the values, // independently of whether the option is of type "array" or "string". // This is unexpected for options listed above, which should all only have only one definitive value. // The yargs behavior could be disabled by using the parserConfiguration "duplicate-arguments-array": true // However, yargs would then cease to create arrays for those options where we *do* expect the // automatic creation of arrays in case the option is specified multiple times. Like "--include-task". // Also see https://github.com/yargs/yargs/issues/1318 // Note: This is not necessary for options of type "boolean" if (Array.isArray(arg)) { // If the option is specified multiple times, use the value of the last option return arg[arg.length - 1]; } return arg; }) .showHelpOnFail(true) .strict(true) .alias("help", "h") .alias("version", "v") .example("ui5 <command> --dependency-definition /path/to/projectDependencies.yaml", "Execute command using a static dependency tree instead of resolving node package dependencies") .example("ui5 <command> --config /path/to/ui5.yaml", "Execute command using a project configuration from custom path") .example("ui5 <command> --workspace dolphin", "Execute command using the 'dolphin' workspace of a ui5-workspace.yaml") .example("ui5 <command> --log-level silly", "Execute command with the maximum log output") .fail(function(msg, err, yargs) { if (err) { ConsoleWriter.stop(); // Exception if (isLogLevelEnabled("error")) { process.stderr.write("\n"); process.stderr.write(chalk.bold.red("⚠️ Process Failed With Error")); process.stderr.write("\n\n"); process.stderr.write(chalk.underline("Error Message:")); process.stderr.write("\n"); process.stderr.write(err.message); process.stderr.write("\n"); // Unexpected errors should always be logged with stack trace const unexpectedErrors = ["SyntaxError", "ReferenceError", "TypeError"]; if (unexpectedErrors.includes(err.name) || isLogLevelEnabled("verbose")) { process.stderr.write("\n\n"); process.stderr.write(chalk.underline("Stack Trace:")); process.stderr.write("\n"); process.stderr.write(err.stack); process.stderr.write("\n"); if (err.cause instanceof Error && err.cause.stack) { process.stderr.write(chalk.underline("Error Cause Stack Trace:\n")); process.stderr.write(err.cause.stack + "\n"); process.stderr.write("\n"); } process.stderr.write( chalk.dim( `If you think this is an issue of the UI5 CLI, you might report it using the ` + `following URL: `) + chalk.dim.bold.underline(`https://github.com/UI5/cli/issues/new/choose`)); process.stderr.write("\n"); } else { process.stderr.write("\n\n"); process.stderr.write(chalk.dim( `For details, execute the same command again with an additional '--verbose' parameter`)); process.stderr.write("\n"); } } } else { // Yargs error process.stderr.write(chalk.bold.yellow("Command Failed:")); process.stderr.write("\n"); process.stderr.write(`${msg}`); process.stderr.write("\n\n"); process.stderr.write(chalk.dim(`See 'ui5 --help'`)); process.stderr.write("\n"); } process.exit(1); }); }