@noravel/command 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/LICENSE +9 -0
  2. package/README.md +569 -0
  3. package/dist/Command.d.ts +27 -0
  4. package/dist/Command.js +86 -0
  5. package/dist/Contracts/Command.d.ts +10 -0
  6. package/dist/Contracts/Command.js +2 -0
  7. package/dist/Contracts/CommandDefinition.d.ts +7 -0
  8. package/dist/Contracts/CommandDefinition.js +2 -0
  9. package/dist/Contracts/InputArgument.d.ts +12 -0
  10. package/dist/Contracts/InputArgument.js +9 -0
  11. package/dist/Contracts/InputOption.d.ts +15 -0
  12. package/dist/Contracts/InputOption.js +10 -0
  13. package/dist/Detector.d.ts +4 -0
  14. package/dist/Detector.js +73 -0
  15. package/dist/Executor.d.ts +4 -0
  16. package/dist/Executor.js +9 -0
  17. package/dist/Inputs/InputArgument.d.ts +13 -0
  18. package/dist/Inputs/InputArgument.js +39 -0
  19. package/dist/Inputs/InputDefinition.d.ts +11 -0
  20. package/dist/Inputs/InputDefinition.js +25 -0
  21. package/dist/Inputs/InputOption.d.ts +18 -0
  22. package/dist/Inputs/InputOption.js +84 -0
  23. package/dist/Inputs/Prompt.d.ts +5 -0
  24. package/dist/Inputs/Prompt.js +178 -0
  25. package/dist/Kernel.d.ts +15 -0
  26. package/dist/Kernel.js +88 -0
  27. package/dist/MakerCommand.d.ts +27 -0
  28. package/dist/MakerCommand.js +68 -0
  29. package/dist/Outputs/Helper.d.ts +7 -0
  30. package/dist/Outputs/Helper.js +61 -0
  31. package/dist/Outputs/OutputColor.d.ts +18 -0
  32. package/dist/Outputs/OutputColor.js +53 -0
  33. package/dist/Outputs/OutputMessage.d.ts +11 -0
  34. package/dist/Outputs/OutputMessage.js +70 -0
  35. package/dist/Outputs/ProgressBar/ProgressBar.d.ts +22 -0
  36. package/dist/Outputs/ProgressBar/ProgressBar.js +98 -0
  37. package/dist/Outputs/ProgressBar/StatusBar.d.ts +11 -0
  38. package/dist/Outputs/ProgressBar/StatusBar.js +10 -0
  39. package/dist/Outputs/ProgressBar/StatusBarFactory.d.ts +5 -0
  40. package/dist/Outputs/ProgressBar/StatusBarFactory.js +20 -0
  41. package/dist/Outputs/ProgressBar/StatusBarNormal.d.ts +6 -0
  42. package/dist/Outputs/ProgressBar/StatusBarNormal.js +19 -0
  43. package/dist/Outputs/ProgressBar/StatusBarVerbose.d.ts +6 -0
  44. package/dist/Outputs/ProgressBar/StatusBarVerbose.js +20 -0
  45. package/dist/Outputs/ProgressBar.d.ts +22 -0
  46. package/dist/Outputs/ProgressBar.js +123 -0
  47. package/dist/Parser.d.ts +9 -0
  48. package/dist/Parser.js +79 -0
  49. package/dist/index.d.ts +5 -0
  50. package/dist/index.js +14 -0
  51. package/dist/src/Command.d.ts +23 -0
  52. package/dist/src/Command.js +61 -0
  53. package/dist/src/Contracts/Command.d.ts +9 -0
  54. package/dist/src/Contracts/Command.js +2 -0
  55. package/dist/src/Contracts/InputArgument.d.ts +11 -0
  56. package/dist/src/Contracts/InputArgument.js +9 -0
  57. package/dist/src/Contracts/InputOption.d.ts +12 -0
  58. package/dist/src/Contracts/InputOption.js +10 -0
  59. package/dist/src/Detector.d.ts +4 -0
  60. package/dist/src/Detector.js +50 -0
  61. package/dist/src/Inputs/InputArgument.d.ts +12 -0
  62. package/dist/src/Inputs/InputArgument.js +36 -0
  63. package/dist/src/Inputs/InputDefinition.d.ts +11 -0
  64. package/dist/src/Inputs/InputDefinition.js +25 -0
  65. package/dist/src/Inputs/InputOption.d.ts +18 -0
  66. package/dist/src/Inputs/InputOption.js +84 -0
  67. package/dist/src/Kernel.d.ts +15 -0
  68. package/dist/src/Kernel.js +87 -0
  69. package/dist/src/MakerCommand.d.ts +27 -0
  70. package/dist/src/MakerCommand.js +68 -0
  71. package/dist/src/Outputs/Helper.d.ts +7 -0
  72. package/dist/src/Outputs/Helper.js +61 -0
  73. package/dist/src/Outputs/OutputColor.d.ts +18 -0
  74. package/dist/src/Outputs/OutputColor.js +53 -0
  75. package/dist/src/Outputs/OutputMessage.d.ts +11 -0
  76. package/dist/src/Outputs/OutputMessage.js +144 -0
  77. package/dist/src/Parser.d.ts +9 -0
  78. package/dist/src/Parser.js +79 -0
  79. package/dist/src/index.d.ts +4 -0
  80. package/dist/src/index.js +12 -0
  81. package/dist/tsconfig.tsbuildinfo +1 -0
  82. package/package.json +35 -0
@@ -0,0 +1,7 @@
1
+ import InputArgument from '../Inputs/InputArgument';
2
+ import InputOption from '../Inputs/InputOption';
3
+ export default interface CommandDefinition {
4
+ name: string;
5
+ arguments: InputArgument[];
6
+ options: InputOption[];
7
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,12 @@
1
+ export declare enum ARGUMENT_MODE {
2
+ REQUIRED = 1,
3
+ OPTIONAL = 2,
4
+ IS_ARRAY = 4
5
+ }
6
+ export interface InputArgument {
7
+ getName(): string;
8
+ getDescription(): string;
9
+ isRequired(): boolean;
10
+ isArray(): boolean;
11
+ getDefault(): string | string[] | undefined;
12
+ }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ARGUMENT_MODE = void 0;
4
+ var ARGUMENT_MODE;
5
+ (function (ARGUMENT_MODE) {
6
+ ARGUMENT_MODE[ARGUMENT_MODE["REQUIRED"] = 1] = "REQUIRED";
7
+ ARGUMENT_MODE[ARGUMENT_MODE["OPTIONAL"] = 2] = "OPTIONAL";
8
+ ARGUMENT_MODE[ARGUMENT_MODE["IS_ARRAY"] = 4] = "IS_ARRAY";
9
+ })(ARGUMENT_MODE || (exports.ARGUMENT_MODE = ARGUMENT_MODE = {}));
@@ -0,0 +1,15 @@
1
+ export declare enum OPTION_MODE {
2
+ NONE = 1,
3
+ REQUIRED = 2,
4
+ OPTIONAL = 4,
5
+ IS_ARRAY = 8
6
+ }
7
+ export interface InputOption {
8
+ getName(): string;
9
+ getDescription(): string;
10
+ isRequired(): boolean;
11
+ isArray(): boolean;
12
+ acceptValue(): boolean;
13
+ getShortcut(): string | string[] | undefined;
14
+ getDefault(): string | string[] | undefined;
15
+ }
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OPTION_MODE = void 0;
4
+ var OPTION_MODE;
5
+ (function (OPTION_MODE) {
6
+ OPTION_MODE[OPTION_MODE["NONE"] = 1] = "NONE";
7
+ OPTION_MODE[OPTION_MODE["REQUIRED"] = 2] = "REQUIRED";
8
+ OPTION_MODE[OPTION_MODE["OPTIONAL"] = 4] = "OPTIONAL";
9
+ OPTION_MODE[OPTION_MODE["IS_ARRAY"] = 8] = "IS_ARRAY";
10
+ })(OPTION_MODE || (exports.OPTION_MODE = OPTION_MODE = {}));
@@ -0,0 +1,4 @@
1
+ import InputDefinition from './Inputs/InputDefinition';
2
+ export default class Detector {
3
+ static detect(argv: string[], definition: InputDefinition): [Record<string, any>, Record<string, any>];
4
+ }
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class Detector {
4
+ static detect(argv, definition) {
5
+ const _arguments = {};
6
+ const _options = {};
7
+ let i = 0;
8
+ const argumentDefinitions = definition.getArguments();
9
+ const optionDefinitions = definition.getOptions();
10
+ while (i < argv.length) {
11
+ if (argv[i].startsWith('-')) {
12
+ let [key, value] = argv[i].split('=');
13
+ const optionDefinition = definition.getOptions(key.replace(/^-{1,2}/, ''));
14
+ const nextIndex = i + 1;
15
+ if (!optionDefinition) {
16
+ i = nextIndex;
17
+ continue;
18
+ }
19
+ if (/(.+)\=(.+)/.test(argv[i])) {
20
+ if (optionDefinition.isArray()) {
21
+ value = [...(_options[optionDefinition.getName()] ?? []), value];
22
+ }
23
+ }
24
+ else if (argv[nextIndex] === undefined || argv[nextIndex].startsWith('-')) {
25
+ i = nextIndex;
26
+ if (optionDefinition.acceptValue()) {
27
+ value = optionDefinition.getDefault();
28
+ }
29
+ else {
30
+ value = true;
31
+ }
32
+ }
33
+ else {
34
+ i = nextIndex;
35
+ value = argv[nextIndex];
36
+ if (optionDefinition.isArray()) {
37
+ value = [...(_options[optionDefinition.getName()] ?? []), argv[nextIndex]];
38
+ }
39
+ }
40
+ _options[optionDefinition.getName()] = value;
41
+ }
42
+ else {
43
+ const argumentDefinition = argumentDefinitions.shift();
44
+ _arguments[argumentDefinition?.getName()] = argv[i];
45
+ }
46
+ i++;
47
+ }
48
+ // Ensure that default values ​​are used for options that do not exist in the argv
49
+ for (const option of optionDefinitions) {
50
+ if (_options[option.getName()] === undefined) {
51
+ if (option.getDefault() !== undefined) {
52
+ _options[option.getName()] = option.getDefault();
53
+ }
54
+ else if (option.isRequired()) {
55
+ throw new Error(`Option ${option.getName()} is required`);
56
+ }
57
+ }
58
+ }
59
+ // Ensure that default values ​​are used for arguments that do not exist in the argv
60
+ for (const _argument of argumentDefinitions) {
61
+ if (_arguments[_argument.getName()] === undefined) {
62
+ if (_argument.getDefault() !== undefined) {
63
+ _arguments[_argument.getName()] = _argument.getDefault();
64
+ }
65
+ else if (_argument.isRequired()) {
66
+ throw new Error(`Argument ${_argument.getName()} is required`);
67
+ }
68
+ }
69
+ }
70
+ return [_arguments, _options];
71
+ }
72
+ }
73
+ exports.default = Detector;
@@ -0,0 +1,4 @@
1
+ import Command from './Contracts/Command';
2
+ export default class Executor {
3
+ static call(command: (new (argv: string[]) => Command), argv: string[]): Promise<void>;
4
+ }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class Executor {
4
+ static async call(command, argv) {
5
+ const commandInstance = new command(argv).bootstrap();
6
+ return await commandInstance.handle();
7
+ }
8
+ }
9
+ exports.default = Executor;
@@ -0,0 +1,13 @@
1
+ import { InputArgument as InputArgumentContract } from '../Contracts/InputArgument';
2
+ export default class InputArgument implements InputArgumentContract {
3
+ name: string;
4
+ description: string;
5
+ defaultValue?: string | string[] | undefined;
6
+ mode: number;
7
+ constructor(name: string, mode?: number, description?: string, defaultValue?: string | string[] | undefined);
8
+ getName(): string;
9
+ getDescription(): string;
10
+ isRequired(): boolean;
11
+ isArray(): boolean;
12
+ getDefault(): string | string[] | undefined;
13
+ }
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const InputArgument_1 = require("../Contracts/InputArgument");
4
+ class InputArgument {
5
+ name;
6
+ description;
7
+ defaultValue;
8
+ mode;
9
+ constructor(name, mode, description = '', defaultValue) {
10
+ this.name = name;
11
+ this.description = description;
12
+ this.defaultValue = defaultValue;
13
+ if (!mode) {
14
+ this.mode = InputArgument_1.ARGUMENT_MODE.OPTIONAL;
15
+ }
16
+ else if (mode >= 1 && mode <= 7) {
17
+ this.mode = mode;
18
+ }
19
+ else {
20
+ throw new Error('Invalid argument mode');
21
+ }
22
+ }
23
+ getName() {
24
+ return this.name;
25
+ }
26
+ getDescription() {
27
+ return this.description;
28
+ }
29
+ isRequired() {
30
+ return (this.mode & InputArgument_1.ARGUMENT_MODE.REQUIRED) === InputArgument_1.ARGUMENT_MODE.REQUIRED;
31
+ }
32
+ isArray() {
33
+ return (this.mode & InputArgument_1.ARGUMENT_MODE.IS_ARRAY) === InputArgument_1.ARGUMENT_MODE.IS_ARRAY;
34
+ }
35
+ getDefault() {
36
+ return this.defaultValue;
37
+ }
38
+ }
39
+ exports.default = InputArgument;
@@ -0,0 +1,11 @@
1
+ import InputArgument from './InputArgument';
2
+ import InputOption from './InputOption';
3
+ export default class InputDefinition {
4
+ _arguments: InputArgument[];
5
+ _options: InputOption[];
6
+ constructor();
7
+ setArguments(data: InputArgument[]): void;
8
+ getArguments(): InputArgument[];
9
+ setOptions(data: InputOption[]): void;
10
+ getOptions(name?: string): InputOption | InputOption[] | undefined;
11
+ }
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class InputDefinition {
4
+ _arguments;
5
+ _options;
6
+ constructor() {
7
+ this._arguments = [];
8
+ this._options = [];
9
+ }
10
+ setArguments(data) {
11
+ this._arguments = data;
12
+ }
13
+ getArguments() {
14
+ return this._arguments;
15
+ }
16
+ setOptions(data) {
17
+ this._options = data;
18
+ }
19
+ getOptions(name) {
20
+ return name
21
+ ? this._options.find((option) => option.getName() === name || option.getShortcut() === name)
22
+ : this._options;
23
+ }
24
+ }
25
+ exports.default = InputDefinition;
@@ -0,0 +1,18 @@
1
+ import { InputOption as InputOptionContract } from '../Contracts/InputOption';
2
+ export default class InputOption implements InputOptionContract {
3
+ description: string;
4
+ name: string;
5
+ mode: number;
6
+ shortcut?: string | string[];
7
+ defaultValue?: string | string[];
8
+ constructor(name: string, shortcut?: string | string[], mode?: number, description?: string, defaultValue?: string | string[]);
9
+ getName(): string;
10
+ getShortcut(): string | string[] | undefined;
11
+ getDescription(): string;
12
+ acceptValue(): boolean;
13
+ isRequired(): boolean;
14
+ isOptional(): boolean;
15
+ isArray(): boolean;
16
+ private setDefault;
17
+ getDefault(): string | string[] | undefined;
18
+ }
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const InputOption_1 = require("../Contracts/InputOption");
4
+ class InputOption {
5
+ description;
6
+ name;
7
+ mode;
8
+ shortcut;
9
+ defaultValue;
10
+ constructor(name, shortcut, mode, description = '', defaultValue) {
11
+ this.description = description;
12
+ if (name.startsWith('--')) {
13
+ name = name.substring(2);
14
+ }
15
+ if (name == '') {
16
+ throw new Error('An option name cannot be empty.');
17
+ }
18
+ if (shortcut === '' || (Array.isArray(shortcut) && shortcut.length === 0)) {
19
+ shortcut = undefined;
20
+ }
21
+ if (shortcut) {
22
+ if (Array.isArray(shortcut)) {
23
+ shortcut = shortcut.join('|');
24
+ }
25
+ const shortcuts = shortcut
26
+ .replace(/^\-/, '')
27
+ .split(/(\|)-?/)
28
+ .filter(item => item.length > 0);
29
+ shortcut = shortcuts.join('|');
30
+ if (shortcut === '') {
31
+ throw new Error('An option shortcut cannot be empty.');
32
+ }
33
+ }
34
+ if (!mode) {
35
+ mode = InputOption_1.OPTION_MODE.NONE;
36
+ }
37
+ else if (mode >= InputOption_1.OPTION_MODE.IS_ARRAY << 1 || mode < 1) {
38
+ throw new Error('Invalid option mode');
39
+ }
40
+ this.name = name;
41
+ this.shortcut = shortcut;
42
+ this.mode = mode;
43
+ this.setDefault(defaultValue);
44
+ }
45
+ getName() {
46
+ return this.name;
47
+ }
48
+ getShortcut() {
49
+ return this.shortcut;
50
+ }
51
+ getDescription() {
52
+ return this.description;
53
+ }
54
+ acceptValue() {
55
+ return this.isRequired() || this.isOptional();
56
+ }
57
+ isRequired() {
58
+ return (this.mode & InputOption_1.OPTION_MODE.REQUIRED) === InputOption_1.OPTION_MODE.REQUIRED;
59
+ }
60
+ isOptional() {
61
+ return (this.mode & InputOption_1.OPTION_MODE.OPTIONAL) === InputOption_1.OPTION_MODE.OPTIONAL;
62
+ }
63
+ isArray() {
64
+ return (this.mode & InputOption_1.OPTION_MODE.IS_ARRAY) === InputOption_1.OPTION_MODE.IS_ARRAY;
65
+ }
66
+ setDefault(defaultValue) {
67
+ if ((this.mode & InputOption_1.OPTION_MODE.NONE) === InputOption_1.OPTION_MODE.NONE && defaultValue !== undefined) {
68
+ throw new Error('Cannot set default value when using OPTION_MODE.NONE mode.');
69
+ }
70
+ if (this.isArray()) {
71
+ if (defaultValue === undefined) {
72
+ this.defaultValue = [];
73
+ }
74
+ else if (!Array.isArray(defaultValue)) {
75
+ throw new Error('A default value for an array option must be an array.');
76
+ }
77
+ }
78
+ this.defaultValue = this.acceptValue() ? defaultValue : undefined;
79
+ }
80
+ getDefault() {
81
+ return this.defaultValue;
82
+ }
83
+ }
84
+ exports.default = InputOption;
@@ -0,0 +1,5 @@
1
+ export default class Prompt {
2
+ ask(message: string): Promise<string>;
3
+ secret(message: string): Promise<string>;
4
+ choice(question: string, options: string[], defaultIndex?: number, allowMultipleSelections?: boolean): Promise<string | string[]>;
5
+ }
@@ -0,0 +1,178 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ const readline = __importStar(require("node:readline"));
37
+ class Prompt {
38
+ async ask(message) {
39
+ return new Promise(resolve => {
40
+ const rl = readline.createInterface({
41
+ input: process.stdin,
42
+ output: process.stdout,
43
+ });
44
+ rl.question(message + '\n', answer => {
45
+ rl.close();
46
+ resolve(answer);
47
+ });
48
+ });
49
+ }
50
+ async secret(message) {
51
+ const stdin = process.stdin;
52
+ if (!stdin.isTTY || typeof stdin.setRawMode !== 'function') {
53
+ return this.ask(message);
54
+ }
55
+ return new Promise((resolve, reject) => {
56
+ process.stdout.write(message + '\n');
57
+ stdin.setRawMode(true);
58
+ stdin.resume();
59
+ stdin.setEncoding('utf8');
60
+ let password = '';
61
+ const onData = (key) => {
62
+ if (key === '\r' || key === '\n') {
63
+ // Enter
64
+ stdin.setRawMode(false);
65
+ stdin.pause();
66
+ stdin.removeListener('data', onData);
67
+ process.stdout.write('\n');
68
+ resolve(password);
69
+ }
70
+ else if (key === '\u0003') {
71
+ // Ctrl+C
72
+ process.exit();
73
+ }
74
+ else if (key === '\u007f') {
75
+ // Backspace
76
+ password = password.slice(0, -1);
77
+ }
78
+ else {
79
+ password += key;
80
+ }
81
+ };
82
+ stdin.on('data', onData);
83
+ });
84
+ }
85
+ choice(question, options, defaultIndex = 0, allowMultipleSelections = false) {
86
+ return new Promise(resolve => {
87
+ readline.emitKeypressEvents(process.stdin);
88
+ if (process.stdin.isTTY) {
89
+ process.stdin.setRawMode(true);
90
+ }
91
+ let cursor = defaultIndex;
92
+ let selected = new Set();
93
+ let done = false;
94
+ // number of lines already rendered (to know how many lines to move the cursor up)
95
+ let renderedLines = 0;
96
+ function getLines() {
97
+ const lines = [];
98
+ lines.push(question, '');
99
+ options.forEach((opt, index) => {
100
+ const isCursor = index === cursor;
101
+ const isSelected = selected.has(index);
102
+ const prefix = isCursor ? '👉' : ' ';
103
+ let mark;
104
+ if (allowMultipleSelections) {
105
+ mark = isSelected ? '[x]' : '[ ]';
106
+ }
107
+ else {
108
+ mark = isCursor ? '(●)' : '( )';
109
+ }
110
+ lines.push(`${prefix} ${mark} ${opt}`);
111
+ });
112
+ lines.push('', allowMultipleSelections ? '(space để chọn, enter để submit)' : '(enter để chọn)');
113
+ return lines;
114
+ }
115
+ function render() {
116
+ const lines = getLines();
117
+ // 🧠 move cursor to the beginning of the rendered content
118
+ if (renderedLines > 0) {
119
+ process.stdout.write(`\x1b[${renderedLines}A`);
120
+ }
121
+ // 🧹 clear each line and rewrite
122
+ lines.forEach(line => {
123
+ process.stdout.write('\x1b[2K'); // clear line
124
+ process.stdout.write(line + '\n');
125
+ });
126
+ renderedLines = lines.length;
127
+ }
128
+ function cleanup(result) {
129
+ if (process.stdin.isTTY) {
130
+ process.stdin.setRawMode(false);
131
+ }
132
+ process.stdin.pause();
133
+ process.stdin.removeAllListeners('keypress');
134
+ process.stdin.unref();
135
+ resolve(result);
136
+ }
137
+ render();
138
+ const keypressHandler = (_str, key) => {
139
+ if (done)
140
+ return;
141
+ if (key.name === 'up') {
142
+ cursor = (cursor - 1 + options.length) % options.length;
143
+ }
144
+ if (key.name === 'down') {
145
+ cursor = (cursor + 1) % options.length;
146
+ }
147
+ if (allowMultipleSelections && key.name === 'space') {
148
+ if (selected.has(cursor)) {
149
+ selected.delete(cursor);
150
+ }
151
+ else {
152
+ selected.add(cursor);
153
+ }
154
+ }
155
+ if (key.name === 'return') {
156
+ done = true;
157
+ if (allowMultipleSelections) {
158
+ const result = Array.from(selected).map(i => options[i]);
159
+ cleanup(result);
160
+ }
161
+ else {
162
+ cleanup(options[cursor]);
163
+ }
164
+ process.stdin.off('keypress', keypressHandler);
165
+ return;
166
+ }
167
+ if (key.ctrl && key.name === 'c') {
168
+ done = true;
169
+ cleanup([]);
170
+ process.exit();
171
+ }
172
+ render();
173
+ };
174
+ process.stdin.on('keypress', keypressHandler);
175
+ });
176
+ }
177
+ }
178
+ exports.default = Prompt;
@@ -0,0 +1,15 @@
1
+ import CommandContract from './Contracts/Command';
2
+ export default class Kernel {
3
+ registeredCommands: (new (argv: string[]) => CommandContract)[];
4
+ argv: string[];
5
+ name: string;
6
+ private callbackVersion?;
7
+ private color;
8
+ private output;
9
+ constructor(argv?: string[]);
10
+ register(commands: (new (argv: string[]) => CommandContract)[]): this;
11
+ run(): void;
12
+ setCallbackVersion(callback: () => void): this;
13
+ showVersion(): void;
14
+ getVersion(): any;
15
+ }
package/dist/Kernel.js ADDED
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const node_fs_1 = __importDefault(require("node:fs"));
7
+ const node_path_1 = __importDefault(require("node:path"));
8
+ const didyoumean2_1 = __importDefault(require("didyoumean2"));
9
+ const Helper_1 = __importDefault(require("./Outputs/Helper"));
10
+ const OutputColor_1 = __importDefault(require("./Outputs/OutputColor"));
11
+ const OutputMessage_1 = __importDefault(require("./Outputs/OutputMessage"));
12
+ class Kernel {
13
+ registeredCommands = [];
14
+ argv;
15
+ name;
16
+ callbackVersion;
17
+ color;
18
+ output;
19
+ constructor(argv = []) {
20
+ this.argv = argv.slice(2);
21
+ this.name = this.argv[0];
22
+ this.color = OutputColor_1.default;
23
+ this.output = new OutputMessage_1.default();
24
+ }
25
+ register(commands) {
26
+ this.registeredCommands = [...this.registeredCommands, ...commands];
27
+ return this;
28
+ }
29
+ run() {
30
+ if (!this.name) {
31
+ const helper = new Helper_1.default();
32
+ helper.showHelpForMain(this.registeredCommands.map((command) => new command(this.argv).bootstrap()));
33
+ return;
34
+ }
35
+ let signatures = [];
36
+ if (['--help', '-h'].includes(this.name)) {
37
+ const helper = new Helper_1.default();
38
+ helper.showHelpForMain(this.registeredCommands.map((command) => new command(this.argv).bootstrap()));
39
+ return;
40
+ }
41
+ if (['--version', '-v'].includes(this.name)) {
42
+ this.showVersion();
43
+ return;
44
+ }
45
+ for (const command of this.registeredCommands) {
46
+ const commandInstance = new command(this.argv).bootstrap();
47
+ const commandName = commandInstance.getName();
48
+ signatures.push(commandName);
49
+ if (this.name === commandName) {
50
+ commandInstance.detect();
51
+ if (commandInstance.getOption('help')) {
52
+ commandInstance.showHelp();
53
+ return;
54
+ }
55
+ if (commandInstance.getOption('version')) {
56
+ this.showVersion();
57
+ return;
58
+ }
59
+ commandInstance.handle();
60
+ return;
61
+ }
62
+ }
63
+ const suggestions = (0, didyoumean2_1.default)(this.name, signatures);
64
+ if (suggestions) {
65
+ this.output.error(`Command "${this.name}" is not defined. Did you mean "${suggestions}"?`);
66
+ return;
67
+ }
68
+ this.output.error(`Command "${this.name}" is not defined. Did you mean one of these?`);
69
+ signatures.forEach((signature) => this.output.comment(` ${signature}`));
70
+ }
71
+ setCallbackVersion(callback) {
72
+ this.callbackVersion = callback.bind(this);
73
+ return this;
74
+ }
75
+ showVersion() {
76
+ if (this.callbackVersion) {
77
+ this.callbackVersion();
78
+ return;
79
+ }
80
+ console.log('Noravel Command ' + this.color.green(this.getVersion()));
81
+ }
82
+ getVersion() {
83
+ const packageJsonPath = node_path_1.default.join(__dirname, '../package.json');
84
+ const packageJson = JSON.parse(node_fs_1.default.readFileSync(packageJsonPath, 'utf-8'));
85
+ return packageJson.version;
86
+ }
87
+ }
88
+ exports.default = Kernel;
@@ -0,0 +1,27 @@
1
+ import Command from './Command';
2
+ export default class MakerCommand extends Command {
3
+ basePath(dirPath?: string): string;
4
+ fileName(question?: string): Promise<string>;
5
+ makeFile(fileName: string, dirPath: string): void;
6
+ /**
7
+ * Get the message that the file already exists.
8
+ *
9
+ * @returns {string}
10
+ */
11
+ protected fileAlreadyExists(filePath: string): string;
12
+ /**
13
+ * Get the message that the file created successfully.
14
+ *
15
+ * @param {string} filePath
16
+ * @returns {string}
17
+ */
18
+ protected fileCreatedSuccessfully(filePath: string): string;
19
+ /**
20
+ * Get the content of the file.
21
+ *
22
+ * @param {string} fileName
23
+ * @returns {string}
24
+ */
25
+ protected contentFile(fileName: string): string;
26
+ handle(): Promise<void>;
27
+ }