UNPKG

3.76 kBJavaScriptView Raw
1"use strict";
2/*
3 * @adonisjs/ace
4 *
5 * (c) Harminder Virk <virk@adonisjs.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10Object.defineProperty(exports, "__esModule", { value: true });
11exports.UnknownFlagException = exports.InvalidCommandException = exports.InvalidFlagException = exports.MissingArgumentException = void 0;
12const cliui_1 = require("@poppinss/cliui");
13const utils_1 = require("@poppinss/utils");
14/**
15 * Raised when a required argument is missing
16 */
17class MissingArgumentException extends utils_1.Exception {
18 /**
19 * A required argument is missing
20 */
21 static invoke(name, command) {
22 const exception = new this(`Missing required argument "${name}"`, 500, 'E_MISSING_ARGUMENT');
23 exception.argumentName = name;
24 exception.command = command;
25 return exception;
26 }
27 /**
28 * Handle itself
29 */
30 handle(error) {
31 cliui_1.logger.error(cliui_1.logger.colors.red(`Missing required argument "${error.argumentName}"`));
32 }
33}
34exports.MissingArgumentException = MissingArgumentException;
35/**
36 * Raised when an the type of a flag is not as one of the excepted type
37 */
38class InvalidFlagException extends utils_1.Exception {
39 /**
40 * Flag type validation failed.
41 */
42 static invoke(prop, expected, command) {
43 let article = 'a';
44 if (expected === 'number') {
45 expected = 'numeric';
46 }
47 if (expected === 'array') {
48 article = 'an';
49 expected = 'array of strings';
50 }
51 if (expected === 'numArray') {
52 article = 'an';
53 expected = 'array of numbers';
54 }
55 const exception = new this(`"${prop}" flag expects ${article} "${expected}" value`, 500, 'E_INVALID_FLAG');
56 exception.flagName = prop;
57 exception.command = command;
58 exception.expectedType = expected;
59 return exception;
60 }
61 /**
62 * Handle itself
63 */
64 handle(error) {
65 cliui_1.logger.error(cliui_1.logger.colors.red(`Expected "--${error.flagName}" to be a valid "${error.expectedType}"`));
66 }
67}
68exports.InvalidFlagException = InvalidFlagException;
69/**
70 * Raised when command is not registered with kernel
71 */
72class InvalidCommandException extends utils_1.Exception {
73 constructor() {
74 super(...arguments);
75 this.suggestions = [];
76 }
77 static invoke(commandName, suggestions) {
78 const exception = new this(`"${commandName}" is not a registered command`, 500, 'E_INVALID_COMMAND');
79 exception.commandName = commandName;
80 exception.suggestions = suggestions;
81 return exception;
82 }
83 /**
84 * Handle itself
85 */
86 handle(error) {
87 cliui_1.logger.error(`"${error.commandName}" command not found`);
88 if (!error.suggestions.length) {
89 return;
90 }
91 cliui_1.logger.log('');
92 const suggestionLog = (0, cliui_1.sticker)().heading('Did you mean one of these?');
93 error.suggestions.forEach((commandName) => {
94 suggestionLog.add(cliui_1.logger.colors.yellow(commandName));
95 });
96 suggestionLog.render();
97 }
98}
99exports.InvalidCommandException = InvalidCommandException;
100/**
101 * Raised when an unknown flag is defined
102 */
103class UnknownFlagException extends utils_1.Exception {
104 /**
105 * Unknown flag
106 */
107 static invoke(prop) {
108 const exception = new this(`Unknown flag "${prop}"`, 500, 'E_INVALID_FLAG');
109 return exception;
110 }
111 /**
112 * Handle itself
113 */
114 handle(error) {
115 cliui_1.logger.error(cliui_1.logger.colors.red(error.message));
116 }
117}
118exports.UnknownFlagException = UnknownFlagException;