@briklab/lib 1.2.1-test → 1.3.1
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.
- package/README.md +18 -1
- package/dist/cli-john/index.d.ts +45 -7
- package/dist/cli-john/index.js +19 -459
- package/dist/cli-john/index.js.map +2 -2
- package/dist/color/index.d.ts +4 -5
- package/dist/color/index.js +5 -205
- package/dist/color/index.js.map +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/jstc/index.d.ts +16 -1
- package/dist/jstc/index.js +3 -96
- package/dist/jstc/index.js.map +2 -2
- package/dist/stylesheet/index.d.ts +5 -4
- package/dist/stylesheet/index.js +12 -251
- package/dist/stylesheet/index.js.map +2 -2
- package/dist/warner/index.d.ts +11 -1
- package/dist/warner/index.js +4 -169
- package/dist/warner/index.js.map +2 -2
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -6,6 +6,23 @@
|
|
|
6
6
|
- **@briklab/cli-john:** Generate a [CLI](https://en.wikipedia.org/wiki/Command-line_interface) with ease.
|
|
7
7
|
- **@briklab/jstc:** Add Type Checking for JavaScript.
|
|
8
8
|
|
|
9
|
+
## Exports
|
|
10
|
+
|
|
11
|
+
The root package export currently exposes warning utilities:
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { warner, createWarner, Warner } from "@briklab/lib";
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Other modules are exposed through subpath imports:
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import JSTC from "@briklab/lib/jstc";
|
|
21
|
+
import Color from "@briklab/lib/color";
|
|
22
|
+
import InlineStyle, { StyleSheet } from "@briklab/lib/stylesheet";
|
|
23
|
+
import { CLI } from "@briklab/lib/cli-john";
|
|
24
|
+
```
|
|
25
|
+
|
|
9
26
|
## Installation
|
|
10
27
|
Normally, the format is:
|
|
11
28
|
```bash
|
|
@@ -13,4 +30,4 @@ Normally, the format is:
|
|
|
13
30
|
```
|
|
14
31
|
**Examples:**
|
|
15
32
|
- **pnpm:**`pnpm add @briklab/lib`
|
|
16
|
-
- **npm:**`npm i @briklab/lib`
|
|
33
|
+
- **npm:**`npm i @briklab/lib`
|
package/dist/cli-john/index.d.ts
CHANGED
|
@@ -52,6 +52,7 @@
|
|
|
52
52
|
*
|
|
53
53
|
* @module cli-john
|
|
54
54
|
*/
|
|
55
|
+
import { type ProtectionLevel } from "../jstc/index.js";
|
|
55
56
|
import InlineStyle, { StyleSheet } from "../stylesheet/index.js";
|
|
56
57
|
/**
|
|
57
58
|
* # CLI
|
|
@@ -67,15 +68,21 @@ export declare class CLI {
|
|
|
67
68
|
/**
|
|
68
69
|
* ## CLI: Constructor
|
|
69
70
|
* @param {NodeJS.Process} process - **The main process**
|
|
71
|
+
* @param {Object} options - Optional configuration
|
|
72
|
+
* @param {WarningLevel} options.warningLevel - Warning display level: 'silent', 'summary', or 'full'
|
|
73
|
+
* @param {ProtectionLevel} options.protectionLevel - Protection level for input validation
|
|
70
74
|
* @constructor
|
|
71
75
|
* @constructs CLI
|
|
72
76
|
* @example
|
|
73
77
|
* import * as process from "node:process"
|
|
74
78
|
* import {CLI} from "@briklab/lib/cli-john"
|
|
75
|
-
* const cli = new CLI(process)
|
|
79
|
+
* const cli = new CLI(process, { warningLevel: "full", protectionLevel: "hardened" })
|
|
76
80
|
* cli.run()
|
|
77
81
|
*/
|
|
78
|
-
constructor(process: NodeJS.Process
|
|
82
|
+
constructor(process: NodeJS.Process, options?: {
|
|
83
|
+
warningLevel?: "silent" | "summary" | "full";
|
|
84
|
+
protectionLevel?: ProtectionLevel;
|
|
85
|
+
});
|
|
79
86
|
/**
|
|
80
87
|
* ### CLI.command
|
|
81
88
|
* create a new command in a CLI.
|
|
@@ -83,7 +90,14 @@ export declare class CLI {
|
|
|
83
90
|
* @param {string} name
|
|
84
91
|
*/
|
|
85
92
|
command(name: string): CLI.Command;
|
|
86
|
-
on(event: CLI.ValidEvent, func:
|
|
93
|
+
on(event: CLI.ValidEvent, func: ({ commandArgs, command, options, }: {
|
|
94
|
+
commandArgs: string[];
|
|
95
|
+
command: string;
|
|
96
|
+
options: {
|
|
97
|
+
arguments: string[];
|
|
98
|
+
optionName: string;
|
|
99
|
+
}[];
|
|
100
|
+
}) => any): void;
|
|
87
101
|
run(): void;
|
|
88
102
|
}
|
|
89
103
|
export declare namespace CLI {
|
|
@@ -100,7 +114,17 @@ export declare namespace CLI {
|
|
|
100
114
|
* @param name The name of the command
|
|
101
115
|
* @constructor
|
|
102
116
|
*/
|
|
103
|
-
constructor(name: string);
|
|
117
|
+
constructor(name: string, commandcreatorfunction: Function);
|
|
118
|
+
/**
|
|
119
|
+
* What to do when an specific event happens
|
|
120
|
+
*/
|
|
121
|
+
on(event: CLI.ValidEvent, func: ({ commandArgs, options, }: {
|
|
122
|
+
commandArgs: string[];
|
|
123
|
+
options: {
|
|
124
|
+
arguments: string[];
|
|
125
|
+
optionName: string;
|
|
126
|
+
}[];
|
|
127
|
+
}) => any): void;
|
|
104
128
|
/**
|
|
105
129
|
* The name of the Command
|
|
106
130
|
* @returns {string}
|
|
@@ -110,8 +134,9 @@ export declare namespace CLI {
|
|
|
110
134
|
* the metadata of the Command
|
|
111
135
|
* @returns {object}
|
|
112
136
|
*/
|
|
113
|
-
|
|
137
|
+
metadata(): Object;
|
|
114
138
|
option(name: string): Command.Option;
|
|
139
|
+
command(...args: any[]): void;
|
|
115
140
|
}
|
|
116
141
|
}
|
|
117
142
|
export declare namespace CLI.Command {
|
|
@@ -121,11 +146,24 @@ export declare namespace CLI.Command {
|
|
|
121
146
|
*/
|
|
122
147
|
class Option {
|
|
123
148
|
#private;
|
|
149
|
+
constructor(name: string, optioncreatorfunc: Function, commandcreatorfunction: Function);
|
|
150
|
+
get name(): string;
|
|
124
151
|
get metadata(): {
|
|
125
152
|
name: string;
|
|
153
|
+
onCmdFunctions: Function[];
|
|
126
154
|
};
|
|
127
|
-
|
|
128
|
-
|
|
155
|
+
/**
|
|
156
|
+
* What to do when an specific event happens for this option
|
|
157
|
+
*/
|
|
158
|
+
on(event: CLI.ValidEvent, func: ({ commandArgs, options, }: {
|
|
159
|
+
commandArgs: string[];
|
|
160
|
+
options: {
|
|
161
|
+
arguments: string[];
|
|
162
|
+
optionName: string;
|
|
163
|
+
}[];
|
|
164
|
+
}) => any): void;
|
|
165
|
+
command(...args: any[]): void;
|
|
166
|
+
option(...args: any[]): void;
|
|
129
167
|
}
|
|
130
168
|
}
|
|
131
169
|
declare class UtilitiesClass {
|
package/dist/cli-john/index.js
CHANGED
|
@@ -1,459 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
* import { CLI as CLI } from "@briklab/lib/cli-john"
|
|
21
|
-
* import * as process from "node:process"
|
|
22
|
-
*
|
|
23
|
-
* const cli = new CLI(process);
|
|
24
|
-
*
|
|
25
|
-
* const cmd = cli.command("myCommand");
|
|
26
|
-
* const opt = cmd.option("force");
|
|
27
|
-
*
|
|
28
|
-
* cmd.on("run", ({commandArgs}) => {
|
|
29
|
-
* CJ.notice("Hey, this is my CLI!");
|
|
30
|
-
* CJ.message("Do you like it?");
|
|
31
|
-
* CJ.error("Invalid args:", ...commandArgs.map(a => a.name));
|
|
32
|
-
* });
|
|
33
|
-
*
|
|
34
|
-
* cli.run();
|
|
35
|
-
* ```
|
|
36
|
-
*
|
|
37
|
-
* ## Limitations
|
|
38
|
-
* - Only **one CLI per file** is allowed (CLI enforces this)
|
|
39
|
-
* - CLI file must be manually added to `bin` in package.json
|
|
40
|
-
*
|
|
41
|
-
* ## Hierarchy
|
|
42
|
-
* ```
|
|
43
|
-
* CLI
|
|
44
|
-
* ├─ Command
|
|
45
|
-
* │ └─ Option
|
|
46
|
-
* └─ CLIErrors
|
|
47
|
-
* ```
|
|
48
|
-
*
|
|
49
|
-
* ## Error Handling
|
|
50
|
-
* - All errors are instances of `CLIErrors`
|
|
51
|
-
* - Dynamic names allow easy identification of which part of the CLI threw the error
|
|
52
|
-
*
|
|
53
|
-
* @module cli-john
|
|
54
|
-
*/
|
|
55
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
56
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
57
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
58
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
59
|
-
};
|
|
60
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
61
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
62
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
63
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
64
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
65
|
-
};
|
|
66
|
-
var _CLI_instances, _CLI_commands, _CLI_onCmdFunctions, _CLI_figureOutCommand, _CLI_process, _CLI_ErrorClass, _CLI_createErr, _CLI_createWarn;
|
|
67
|
-
// -------------------------------------------------------------------------------------------------------
|
|
68
|
-
//#region Defination of custom JSTC handler
|
|
69
|
-
import JSTC from "../jstc/index.js";
|
|
70
|
-
import InlineStyle, { StyleSheet } from "../stylesheet/index.js";
|
|
71
|
-
import Color from "../color/index.js";
|
|
72
|
-
import { warner } from "../warner/index.js";
|
|
73
|
-
JSTC.addCustomHandler("NodeJS Process", (p) => {
|
|
74
|
-
return (p &&
|
|
75
|
-
typeof p === "object" &&
|
|
76
|
-
typeof p.pid === "number" &&
|
|
77
|
-
typeof p.cwd === "function" &&
|
|
78
|
-
typeof p.exit === "function");
|
|
79
|
-
});
|
|
80
|
-
//#endregion
|
|
81
|
-
// -------------------------------------------------------------------------------------------------------
|
|
82
|
-
//#region The Main Class
|
|
83
|
-
/**
|
|
84
|
-
* # CLI
|
|
85
|
-
* @classdesc The main class for **CLI**.
|
|
86
|
-
* @example
|
|
87
|
-
* import * as process from "node:process"
|
|
88
|
-
* import {CLI} from "@briklab/lib/cli-john"
|
|
89
|
-
* const cli = new CLI(process)
|
|
90
|
-
* cli.run()
|
|
91
|
-
*/
|
|
92
|
-
export class CLI {
|
|
93
|
-
/**
|
|
94
|
-
* ## CLI: Constructor
|
|
95
|
-
* @param {NodeJS.Process} process - **The main process**
|
|
96
|
-
* @constructor
|
|
97
|
-
* @constructs CLI
|
|
98
|
-
* @example
|
|
99
|
-
* import * as process from "node:process"
|
|
100
|
-
* import {CLI} from "@briklab/lib/cli-john"
|
|
101
|
-
* const cli = new CLI(process)
|
|
102
|
-
* cli.run()
|
|
103
|
-
*/
|
|
104
|
-
constructor(process) {
|
|
105
|
-
_CLI_instances.add(this);
|
|
106
|
-
_CLI_commands.set(this, []);
|
|
107
|
-
_CLI_onCmdFunctions.set(this, []);
|
|
108
|
-
_CLI_process.set(this, void 0);
|
|
109
|
-
_CLI_ErrorClass.set(this, class extends CLIErrors {
|
|
110
|
-
constructor(message) {
|
|
111
|
-
super(message);
|
|
112
|
-
this.setName = "CLI";
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
if (!JSTC.for([process]).check(["NodeJS Process"])) {
|
|
116
|
-
throw __classPrivateFieldGet(this, _CLI_instances, "m", _CLI_createErr).call(this, "Invalid First Argument!", "You must pass a valid NodeJS process (imported from node:process) while constructing a CLI Class!");
|
|
117
|
-
}
|
|
118
|
-
__classPrivateFieldSet(this, _CLI_process, process, "f");
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* ### CLI.command
|
|
122
|
-
* create a new command in a CLI.
|
|
123
|
-
*
|
|
124
|
-
* @param {string} name
|
|
125
|
-
*/
|
|
126
|
-
command(name) {
|
|
127
|
-
if (!JSTC.for([name]).check(["string"])) {
|
|
128
|
-
__classPrivateFieldGet(this, _CLI_instances, "m", _CLI_createWarn).call(this, "Invalid First Argument!", "CLI.option expects a string as the first argument.", "Using String(given argument) as fallback.");
|
|
129
|
-
name = String(name);
|
|
130
|
-
}
|
|
131
|
-
let c = new CLI.Command(name);
|
|
132
|
-
let f = __classPrivateFieldGet(this, _CLI_commands, "f").findIndex((a) => a.name === name);
|
|
133
|
-
if (f !== -1)
|
|
134
|
-
__classPrivateFieldGet(this, _CLI_commands, "f")[f] = c;
|
|
135
|
-
else
|
|
136
|
-
__classPrivateFieldGet(this, _CLI_commands, "f").push(c);
|
|
137
|
-
return c;
|
|
138
|
-
}
|
|
139
|
-
on(event, func) {
|
|
140
|
-
if (!JSTC.for([event, func]).check(["string", "function"]))
|
|
141
|
-
throw __classPrivateFieldGet(this, _CLI_instances, "m", _CLI_createErr).call(this, "Arguments in CLI.on are invalid!", "The first argument must be a string, and the second argument must be a function.");
|
|
142
|
-
switch (event.toLowerCase()) {
|
|
143
|
-
case "command":
|
|
144
|
-
__classPrivateFieldGet(this, _CLI_onCmdFunctions, "f").push(func);
|
|
145
|
-
break;
|
|
146
|
-
default:
|
|
147
|
-
__classPrivateFieldGet(this, _CLI_instances, "m", _CLI_createWarn).call(this, "Invalid event in CLI.on", "Please enter a valid event from CLI.ValidEvents (array)");
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
run() {
|
|
151
|
-
let { options, commandArgs, command, failed } = __classPrivateFieldGet(this, _CLI_instances, "m", _CLI_figureOutCommand).call(this);
|
|
152
|
-
if (failed)
|
|
153
|
-
return;
|
|
154
|
-
for (let i = 0; i < __classPrivateFieldGet(this, _CLI_onCmdFunctions, "f").length; i++) {
|
|
155
|
-
__classPrivateFieldGet(this, _CLI_onCmdFunctions, "f")[i]({ options, commandArgs, command });
|
|
156
|
-
}
|
|
157
|
-
warner.flush();
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
_CLI_commands = new WeakMap(), _CLI_onCmdFunctions = new WeakMap(), _CLI_process = new WeakMap(), _CLI_ErrorClass = new WeakMap(), _CLI_instances = new WeakSet(), _CLI_figureOutCommand = function _CLI_figureOutCommand() {
|
|
161
|
-
// for eg. we have nodepath filepath cli build
|
|
162
|
-
let [, , ...commands] = __classPrivateFieldGet(this, _CLI_process, "f").argv; // now its cli build. clear
|
|
163
|
-
if (commands.length === 0)
|
|
164
|
-
return { options: [], command: "", commandArgs: [], failed: true };
|
|
165
|
-
let command = __classPrivateFieldGet(this, _CLI_commands, "f").find((a) => a.name === commands[0]); // find the command
|
|
166
|
-
if (!command)
|
|
167
|
-
return { options: [], command: "", commandArgs: [], failed: true }; // command not found?
|
|
168
|
-
let commandArgs = [];
|
|
169
|
-
const args = commands.slice(1);
|
|
170
|
-
for (let i = 0; i < args.length; i++) {
|
|
171
|
-
let arg = args[i];
|
|
172
|
-
if (arg.startsWith("--"))
|
|
173
|
-
break;
|
|
174
|
-
commandArgs.push(arg);
|
|
175
|
-
}
|
|
176
|
-
let leftover = args.slice(commandArgs.length); // args = [1,2,3]; command args = [1,2] command args length is 2, therefore .slice(2) results in [3]
|
|
177
|
-
let options = [];
|
|
178
|
-
for (let i = 0; i < leftover.length; i++) {
|
|
179
|
-
const opt = leftover[i];
|
|
180
|
-
if (!opt.startsWith("--"))
|
|
181
|
-
continue;
|
|
182
|
-
const values = [];
|
|
183
|
-
for (let j = i + 1; j < leftover.length; j++) {
|
|
184
|
-
if (leftover[j].startsWith("--"))
|
|
185
|
-
break;
|
|
186
|
-
values.push(leftover[j]);
|
|
187
|
-
}
|
|
188
|
-
options.push({ option: opt, arguments: values });
|
|
189
|
-
}
|
|
190
|
-
return {
|
|
191
|
-
options,
|
|
192
|
-
commandArgs,
|
|
193
|
-
command,
|
|
194
|
-
failed: false,
|
|
195
|
-
};
|
|
196
|
-
}, _CLI_createErr = function _CLI_createErr(message, hint) {
|
|
197
|
-
return new (__classPrivateFieldGet(this, _CLI_ErrorClass, "f"))(`${message}
|
|
198
|
-
Hint: ${hint}`);
|
|
199
|
-
}, _CLI_createWarn = function _CLI_createWarn(message, hint, otherMessage) {
|
|
200
|
-
warner.warn({ message: `[Class CLI] ${message}
|
|
201
|
-
Hint: ${hint}
|
|
202
|
-
${otherMessage}` });
|
|
203
|
-
return;
|
|
204
|
-
};
|
|
205
|
-
//#endregion
|
|
206
|
-
// -------------------------------------------------------------------------------------------------------
|
|
207
|
-
//#region Error Class
|
|
208
|
-
class CLIErrors extends Error {
|
|
209
|
-
constructor(message) {
|
|
210
|
-
super(message);
|
|
211
|
-
this.name = `[] @briklab/lib/cli-john`;
|
|
212
|
-
Error.captureStackTrace(this, CLIErrors);
|
|
213
|
-
}
|
|
214
|
-
set setName(name) {
|
|
215
|
-
this.name = `[${name}] @briklab/lib/cli-john`;
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
(function (CLI) {
|
|
219
|
-
var _Command_instances, _Command_name, _Command_createWarn, _Command_options;
|
|
220
|
-
CLI.ValidEvents = ["command"];
|
|
221
|
-
/**
|
|
222
|
-
* ## CLI.Command
|
|
223
|
-
* A command in a CLI Command
|
|
224
|
-
*/
|
|
225
|
-
class Command {
|
|
226
|
-
/**
|
|
227
|
-
* ### Command Constructor
|
|
228
|
-
* @param name The name of the command
|
|
229
|
-
* @constructor
|
|
230
|
-
*/
|
|
231
|
-
constructor(name) {
|
|
232
|
-
_Command_instances.add(this);
|
|
233
|
-
_Command_name.set(this, void 0);
|
|
234
|
-
_Command_options.set(this, []);
|
|
235
|
-
__classPrivateFieldSet(this, _Command_name, name, "f");
|
|
236
|
-
return this;
|
|
237
|
-
}
|
|
238
|
-
/**
|
|
239
|
-
* The name of the Command
|
|
240
|
-
* @returns {string}
|
|
241
|
-
*/
|
|
242
|
-
get name() {
|
|
243
|
-
return __classPrivateFieldGet(this, _Command_name, "f");
|
|
244
|
-
}
|
|
245
|
-
/**
|
|
246
|
-
* the metadata of the Command
|
|
247
|
-
* @returns {object}
|
|
248
|
-
*/
|
|
249
|
-
get metadata() {
|
|
250
|
-
let metadata = {
|
|
251
|
-
options: __classPrivateFieldGet(this, _Command_options, "f").map((a) => a.metadata),
|
|
252
|
-
name: this.name,
|
|
253
|
-
};
|
|
254
|
-
return metadata;
|
|
255
|
-
}
|
|
256
|
-
option(name) {
|
|
257
|
-
if (!JSTC.for([name]).check(["string"])) {
|
|
258
|
-
__classPrivateFieldGet(this, _Command_instances, "m", _Command_createWarn).call(this, "First argument is invalid!", "The first argument must be a string", "Using String(argument) as fallback");
|
|
259
|
-
name = String(name);
|
|
260
|
-
}
|
|
261
|
-
let o = new CLI.Command.Option(name);
|
|
262
|
-
let f = __classPrivateFieldGet(this, _Command_options, "f").findIndex((a) => a.name === name);
|
|
263
|
-
if (f !== -1)
|
|
264
|
-
__classPrivateFieldGet(this, _Command_options, "f")[f] = o;
|
|
265
|
-
else
|
|
266
|
-
__classPrivateFieldGet(this, _Command_options, "f").push(o);
|
|
267
|
-
return o;
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
_Command_name = new WeakMap(), _Command_options = new WeakMap(), _Command_instances = new WeakSet(), _Command_createWarn = function _Command_createWarn(message, hint, otherMessage) {
|
|
271
|
-
warner.warn({ message: `[Class CLI.Command] ${message}
|
|
272
|
-
Hint: ${hint}
|
|
273
|
-
${otherMessage}` });
|
|
274
|
-
return;
|
|
275
|
-
};
|
|
276
|
-
CLI.Command = Command;
|
|
277
|
-
})(CLI || (CLI = {}));
|
|
278
|
-
//#endregion
|
|
279
|
-
// -------------------------------------------------------------------------------------------------------
|
|
280
|
-
//#region CLI.Command.Option
|
|
281
|
-
(function (CLI) {
|
|
282
|
-
var Command;
|
|
283
|
-
(function (Command) {
|
|
284
|
-
var _Option_name;
|
|
285
|
-
/**
|
|
286
|
-
* ## CLI.Command.Option
|
|
287
|
-
* A option for a CLI.Command
|
|
288
|
-
*/
|
|
289
|
-
class Option {
|
|
290
|
-
get metadata() {
|
|
291
|
-
let metadata = {
|
|
292
|
-
name: `${__classPrivateFieldGet(this, _Option_name, "f")}`, // <-- Templates TO NOT reference the actual variable
|
|
293
|
-
};
|
|
294
|
-
return metadata;
|
|
295
|
-
}
|
|
296
|
-
constructor(name) {
|
|
297
|
-
_Option_name.set(this, void 0);
|
|
298
|
-
__classPrivateFieldSet(this, _Option_name, name, "f");
|
|
299
|
-
return this;
|
|
300
|
-
}
|
|
301
|
-
get name() {
|
|
302
|
-
return __classPrivateFieldGet(this, _Option_name, "f");
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
_Option_name = new WeakMap();
|
|
306
|
-
Command.Option = Option;
|
|
307
|
-
})(Command = CLI.Command || (CLI.Command = {}));
|
|
308
|
-
})(CLI || (CLI = {}));
|
|
309
|
-
//#endregion
|
|
310
|
-
// -------------------------------------------------------------------------------------------------------
|
|
311
|
-
//#region CLI Utilities
|
|
312
|
-
JSTC.addCustomHandler("Utilities Tag Config", (p) => {
|
|
313
|
-
return (p &&
|
|
314
|
-
typeof p === "object" &&
|
|
315
|
-
typeof p.tag === "string" &&
|
|
316
|
-
typeof p.showErrorInTag === "boolean" &&
|
|
317
|
-
typeof p.paddingLeft === "number" &&
|
|
318
|
-
typeof p.paddingRight === "number" &&
|
|
319
|
-
(typeof p.styleName === "string" || p.styleName === undefined));
|
|
320
|
-
});
|
|
321
|
-
class UtilitiesClass {
|
|
322
|
-
constructor() {
|
|
323
|
-
this.styleSheet = new StyleSheet();
|
|
324
|
-
this.tags = {};
|
|
325
|
-
this.addTag("error", {
|
|
326
|
-
tag: "ERROR",
|
|
327
|
-
showErrorInTag: false,
|
|
328
|
-
paddingLeft: 0,
|
|
329
|
-
paddingRight: 0,
|
|
330
|
-
});
|
|
331
|
-
this.addTag("warning", {
|
|
332
|
-
tag: "WARNING",
|
|
333
|
-
showErrorInTag: true,
|
|
334
|
-
paddingLeft: 0,
|
|
335
|
-
paddingRight: 0,
|
|
336
|
-
});
|
|
337
|
-
this.addTag("info", {
|
|
338
|
-
tag: "INFO",
|
|
339
|
-
showErrorInTag: true,
|
|
340
|
-
paddingLeft: 0,
|
|
341
|
-
paddingRight: 0,
|
|
342
|
-
});
|
|
343
|
-
this.setTagStyle("error", new InlineStyle({ color: "red", fontWeight: "bold" }));
|
|
344
|
-
this.setTagStyle("warning", new InlineStyle({ color: "orange", fontWeight: "bold" }));
|
|
345
|
-
this.setTagStyle("info", new InlineStyle({ color: "blue" }));
|
|
346
|
-
}
|
|
347
|
-
/** Add a new tag */
|
|
348
|
-
addTag(name, config = {}) {
|
|
349
|
-
if (!JSTC.for([name, config]).check(["string", "object"])) {
|
|
350
|
-
warner.warn({ message: `[UtilitiesClass.addTag] @briklab/lib/cli-john: Invalid Arguments!
|
|
351
|
-
Hint: The first argument must be a string, and the second argument must be a object.
|
|
352
|
-
Using String(argument1) and {} as fallback.` });
|
|
353
|
-
name = String(name);
|
|
354
|
-
config = {};
|
|
355
|
-
}
|
|
356
|
-
const fullConfig = {
|
|
357
|
-
tag: name.toUpperCase(),
|
|
358
|
-
showErrorInTag: false,
|
|
359
|
-
paddingLeft: 0,
|
|
360
|
-
paddingRight: 0,
|
|
361
|
-
styleName: "",
|
|
362
|
-
...config,
|
|
363
|
-
};
|
|
364
|
-
if (!JSTC.for([fullConfig]).check(["Utilities Tag Config"])) {
|
|
365
|
-
warner.warn({ message: `[UtilitiesClass.addTag] @briklab/lib/cli-john: Invalid tag config passed for "${name}"
|
|
366
|
-
Hint: The config must be in format {tag?: string, showErrorInTag?:boolean, paddingLeft?:number, paddingRight?:number, styleName?:string}` });
|
|
367
|
-
warner.warn({ message: JSON.stringify(fullConfig, null, 2) });
|
|
368
|
-
return this;
|
|
369
|
-
}
|
|
370
|
-
this.tags[name] = fullConfig;
|
|
371
|
-
return this;
|
|
372
|
-
}
|
|
373
|
-
/** Set style for a tag */
|
|
374
|
-
setTagStyle(tagName, style) {
|
|
375
|
-
if (typeof tagName !== "string" || !(style instanceof InlineStyle)) {
|
|
376
|
-
warner.warn({ message: `[UtilitiesClass.setTagStyle] @briklab/lib/cli-john: Invalid arguments!
|
|
377
|
-
Hint: The first argument must be a string and the second argument must be a instance of InlineStyle
|
|
378
|
-
Using String(firstArgument) and new InlineStyle({}) as fallback` });
|
|
379
|
-
tagName = String(tagName);
|
|
380
|
-
style = new InlineStyle({});
|
|
381
|
-
}
|
|
382
|
-
if (!this.tags[tagName]) {
|
|
383
|
-
warner.warn({ message: `[UtilitiesClass.setTagStyle] @briklab/lib/cli-john: Tag "${tagName}" does not exist!
|
|
384
|
-
Hint: Use a valid tag that you have defined or use "error"|"warn"|"info"` });
|
|
385
|
-
return this;
|
|
386
|
-
}
|
|
387
|
-
const styleName = `${tagName} Tag Color`;
|
|
388
|
-
this.styleSheet.set(styleName, style);
|
|
389
|
-
this.tags[tagName].styleName = styleName;
|
|
390
|
-
return this;
|
|
391
|
-
}
|
|
392
|
-
log(tagName, ...messages) {
|
|
393
|
-
if (!JSTC.for([tagName]).check(["string"])) {
|
|
394
|
-
warner.warn({ message: `[UtilitiesClass.log] @briklab/lib/cli-john: Invalid Arguments!
|
|
395
|
-
Hint: The first argument must be a string
|
|
396
|
-
Using String(argument1) as fallback` });
|
|
397
|
-
tagName = String(tagName);
|
|
398
|
-
}
|
|
399
|
-
if (!messages || messages.length === 0)
|
|
400
|
-
messages = [""];
|
|
401
|
-
const tag = this.tags[tagName];
|
|
402
|
-
if (!tag) {
|
|
403
|
-
warner.warn({ message: `[UtilitiesClass.log] @briklab/lib/cli-john: Tag "${tagName}" does not exist!
|
|
404
|
-
Hint: Use a valid tag that you have defined or use "error"|"warn"|"info"` });
|
|
405
|
-
console.log(...messages);
|
|
406
|
-
return;
|
|
407
|
-
}
|
|
408
|
-
const inlineStyle = this.styleSheet.get(tag.styleName);
|
|
409
|
-
const style = inlineStyle?.text ?? "";
|
|
410
|
-
const leftPad = " ".repeat(tag.paddingLeft);
|
|
411
|
-
const rightPad = " ".repeat(tag.paddingRight);
|
|
412
|
-
const isNodeTTY = typeof process !== "undefined" &&
|
|
413
|
-
typeof process.stdout !== "undefined" &&
|
|
414
|
-
Boolean(process.stdout.isTTY);
|
|
415
|
-
if (isNodeTTY) {
|
|
416
|
-
const ansi = inlineStyle?.ansi ?? "";
|
|
417
|
-
const reset = Color.RESET;
|
|
418
|
-
if (tag.showErrorInTag) {
|
|
419
|
-
console.log("[" + ansi + leftPad + tag.tag + rightPad + reset + "]:", ...messages);
|
|
420
|
-
}
|
|
421
|
-
else {
|
|
422
|
-
console.log(ansi + "[" + leftPad + tag.tag + rightPad + "]" + reset + ":", ...messages);
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
else {
|
|
426
|
-
if (tag.showErrorInTag) {
|
|
427
|
-
console.log(`[%c${leftPad}${tag.tag}${rightPad}%c]:`, style, ...messages);
|
|
428
|
-
}
|
|
429
|
-
else {
|
|
430
|
-
console.log(`%c[${leftPad}${tag.tag}${rightPad}]%c:`, style, ...messages);
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
error(...msg) {
|
|
435
|
-
if (!msg || msg.length === 0)
|
|
436
|
-
msg = [""];
|
|
437
|
-
this.log("error", ...msg);
|
|
438
|
-
return this;
|
|
439
|
-
}
|
|
440
|
-
warning(...msg) {
|
|
441
|
-
if (!msg || msg.length === 0)
|
|
442
|
-
msg = [""];
|
|
443
|
-
this.log("warning", ...msg);
|
|
444
|
-
return this;
|
|
445
|
-
}
|
|
446
|
-
info(...msg) {
|
|
447
|
-
if (!msg || msg.length === 0)
|
|
448
|
-
msg = [""];
|
|
449
|
-
this.log("info", ...msg);
|
|
450
|
-
return this;
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
export const Utilities = new UtilitiesClass();
|
|
454
|
-
//#endregion
|
|
455
|
-
// -------------------------------------------------------------------------------------------------------
|
|
456
|
-
//#region TODO
|
|
457
|
-
// TODO: Wire Options to Commands
|
|
458
|
-
// TODO: Create metadata getter-s in both commands and options
|
|
459
|
-
//#endregion
|
|
1
|
+
var i=function(a,t,e,n){if(e==="a"&&!n)throw new TypeError("Private accessor was defined without a getter");if(typeof t=="function"?a!==t||!n:!t.has(a))throw new TypeError("Cannot read private member from an object whose class did not declare it");return e==="m"?n:e==="a"?n.call(a):n?n.value:t.get(a)},w=function(a,t,e,n,o){if(n==="m")throw new TypeError("Private method is not writable");if(n==="a"&&!o)throw new TypeError("Private accessor was defined without a setter");if(typeof t=="function"?a!==t||!o:!t.has(a))throw new TypeError("Cannot write private member to an object whose class did not declare it");return n==="a"?o.call(a,e):o?o.value=e:t.set(a,e),e},b,S,_,I,O,L,E,W,$;import u from"../jstc/index.js";import y,{StyleSheet as j}from"../stylesheet/index.js";import M from"../color/index.js";import{createWarner as P}from"../warner/index.js";const g=P("@briklab/lib/cli-john");u.addCustomHandler("NodeJS Process",a=>a&&typeof a=="object"&&typeof a.pid=="number"&&typeof a.cwd=="function"&&typeof a.exit=="function");class v{constructor(t,e){if(b.add(this),S.set(this,"boundary"),_.set(this,[]),I.set(this,[]),L.set(this,void 0),E.set(this,class extends T{constructor(n){super(n),this.setName="CLI"}}),!u.for([t]).check(["NodeJS Process"]))throw i(this,b,"m",W).call(this,"Invalid First Argument!","You must pass a valid NodeJS process (imported from node:process) while constructing a CLI Class!");w(this,L,t,"f"),w(this,S,e?.protectionLevel??"boundary","f"),e?.warningLevel&&g.setLevel(e.warningLevel),e?.protectionLevel&&(u.setProtectionLevel(e.protectionLevel),g.setProtectionLevel(e.protectionLevel))}command(t){u.for([t]).check(["string"])||(i(this,b,"m",$).call(this,"Invalid First Argument!","CLI.option expects a string as the first argument.","Using String(given argument) as fallback."),t=String(t));let e=new v.Command(t,this.command.bind(this)),n=i(this,_,"f").findIndex(o=>o.name===t);return n!==-1?i(this,_,"f")[n]=e:i(this,_,"f").push(e),e}on(t,e){if(!u.for([t,e]).check(["string","function"]))throw i(this,b,"m",W).call(this,"Arguments in CLI.on are invalid!","The first argument must be a string, and the second argument must be a function.");t.toLowerCase()==="command"?i(this,I,"f").push(e):i(this,b,"m",$).call(this,"Invalid event in CLI.on","Please enter a valid event from CLI.ValidEvents (array)")}run(){let{options:t,commandArgs:e,command:n,failed:o}=i(this,b,"m",O).call(this);if(o)return;for(let r=0;r<i(this,I,"f").length;r++)i(this,I,"f")[r]({options:t,commandArgs:e,command:n});let c=i(this,_,"f").find(r=>r.name===n);if(!c)return;const s=c?.metadata()?.onCmdFunctions??[];for(let r=0;r<s.length;r++)s[r]({options:t,commandArgs:e});g.flush()}}S=new WeakMap,_=new WeakMap,I=new WeakMap,L=new WeakMap,E=new WeakMap,b=new WeakSet,O=function(){let[,,...t]=i(this,L,"f").argv;if(t.length===0)return{options:[],command:"",commandArgs:[],failed:!0};let e=i(this,_,"f").find(s=>s.name===t[0]);if(!e)return{options:[],command:"",commandArgs:[],failed:!0};let n=[];const o=t.slice(1);for(let s=0;s<o.length;s++){let r=o[s];if(r.startsWith("--"))break;n.push(r)}let c=o.slice(n.length),h=[];for(let s=0;s<c.length;s++){const r=c[s];if(!r.startsWith("--"))continue;const p=[];for(let f=s+1;f<c.length&&!c[f].startsWith("--");f++)p.push(c[f]);h.push({option:r,arguments:p})}return{options:h,commandArgs:n,command:e,failed:!1}},W=function(t,e){return new(i(this,E,"f"))(`${t}
|
|
2
|
+
Hint: ${e}`)},$=function(t,e,n){g.warn({message:`[Class CLI] ${t}
|
|
3
|
+
Hint: ${e}
|
|
4
|
+
${n}`})};class T extends Error{constructor(t){super(t),this.name="[] @briklab/lib/cli-john",Error.captureStackTrace(this,T)}set setName(t){this.name=`[${t}] @briklab/lib/cli-john`}}(function(a){var t,e,n,o,c,h,s,r;a.ValidEvents=["command"];class p{constructor(d,m){return t.add(this),e.set(this,void 0),n.set(this,void 0),o.set(this,class extends T{constructor(l){super(l),this.setName="CLI.Command"}}),h.set(this,[]),r.set(this,[]),w(this,e,d,"f"),w(this,n,m,"f"),this}on(d,m){if(!u.for([d,m]).check(["string","function"]))throw i(this,t,"m",c).call(this,"Arguments in CLI.Command.on are invalid!","The first argument must be a string, and the second argument must be a function.");d.toLowerCase()==="command"?i(this,h,"f").push(m):i(this,t,"m",s).call(this,"Invalid event in CLI.Command.on","Please enter a valid event from CLI.ValidEvents (array)")}get name(){return`${i(this,e,"f")}`}metadata(){return{options:i(this,r,"f").map(m=>m.metadata),name:`${this.name}`,onCmdFunctions:[...i(this,h,"f")]}}option(d){u.for([d]).check(["string"])||(i(this,t,"m",s).call(this,"First argument is invalid!","The first argument must be a string","Using String(argument) as fallback"),d=String(d));let m=new a.Command.Option(d,this.option.bind(this),i(this,n,"f")),l=i(this,r,"f").findIndex(C=>C.name===d);return l!==-1?i(this,r,"f")[l]=m:i(this,r,"f").push(m),m}command(...d){i(this,n,"f").call(this,...d)}}e=new WeakMap,n=new WeakMap,o=new WeakMap,h=new WeakMap,r=new WeakMap,t=new WeakSet,c=function(d,m){return new(i(this,o,"f"))(`${d}
|
|
5
|
+
Hint: ${m}`)},s=function(d,m,l){g.warn({message:`[Class CLI.Command] ${d}
|
|
6
|
+
Hint: ${m}
|
|
7
|
+
${l}`})},a.Command=p})(v||(v={})),(function(a){var t;(function(e){var n,o,c,h,s,r,p,f;class d{constructor(l,C,k){return n.add(this),o.set(this,void 0),c.set(this,void 0),h.set(this,void 0),s.set(this,[]),r.set(this,class extends T{constructor(U){super(U),this.setName="CLI.Command.Option"}}),w(this,o,l,"f"),w(this,c,C,"f"),w(this,h,k,"f"),this}get name(){return i(this,o,"f")}get metadata(){return{name:`${i(this,o,"f")}`,onCmdFunctions:[...i(this,s,"f")]}}on(l,C){if(!u.for([l,C]).check(["string","function"]))throw i(this,n,"m",p).call(this,"Arguments in CLI.Command.Option.on are invalid!","The first argument must be a string, and the second argument must be a function.");l.toLowerCase()==="command"?i(this,s,"f").push(C):i(this,n,"m",f).call(this,"Invalid event in CLI.Command.Option.on","Please enter a valid event from CLI.ValidEvents (array)")}command(...l){i(this,h,"f").call(this,...l)}option(...l){i(this,c,"f").call(this,...l)}}o=new WeakMap,c=new WeakMap,h=new WeakMap,s=new WeakMap,r=new WeakMap,n=new WeakSet,p=function(l,C){return new(i(this,r,"f"))(`${l}
|
|
8
|
+
Hint: ${C}`)},f=function(l,C,k){g.warn({message:`[Class CLI.Command.Option] ${l}
|
|
9
|
+
Hint: ${C}
|
|
10
|
+
${k}`})},e.Option=d})(t=a.Command||(a.Command={}))})(v||(v={})),u.addCustomHandler("Utilities Tag Config",a=>a&&typeof a=="object"&&typeof a.tag=="string"&&typeof a.showErrorInTag=="boolean"&&typeof a.paddingLeft=="number"&&typeof a.paddingRight=="number"&&(typeof a.styleName=="string"||a.styleName===void 0));class F{constructor(){this.styleSheet=new j,this.tags={},this.addTag("error",{tag:"ERROR",showErrorInTag:!1,paddingLeft:0,paddingRight:0}),this.addTag("warning",{tag:"WARNING",showErrorInTag:!0,paddingLeft:0,paddingRight:0}),this.addTag("info",{tag:"INFO",showErrorInTag:!0,paddingLeft:0,paddingRight:0}),this.setTagStyle("error",new y({color:"red",fontWeight:"bold"})),this.setTagStyle("warning",new y({color:"orange",fontWeight:"bold"})),this.setTagStyle("info",new y({color:"blue"}))}addTag(t,e={}){u.for([t,e]).check(["string","object"])||(g.warn({message:`[UtilitiesClass.addTag] @briklab/lib/cli-john: Invalid Arguments!
|
|
11
|
+
Hint: The first argument must be a string, and the second argument must be a object.
|
|
12
|
+
Using String(argument1) and {} as fallback.`}),t=String(t),e={});const n={tag:t.toUpperCase(),showErrorInTag:!1,paddingLeft:0,paddingRight:0,styleName:"",...e};return u.for([n]).check(["Utilities Tag Config"])?(this.tags[t]=n,this):(g.warn({message:`[UtilitiesClass.addTag] @briklab/lib/cli-john: Invalid tag config passed for "${t}"
|
|
13
|
+
Hint: The config must be in format {tag?: string, showErrorInTag?:boolean, paddingLeft?:number, paddingRight?:number, styleName?:string}`}),g.warn({message:JSON.stringify(n,null,2)}),this)}setTagStyle(t,e){if((typeof t!="string"||!(e instanceof y))&&(g.warn({message:`[UtilitiesClass.setTagStyle] @briklab/lib/cli-john: Invalid arguments!
|
|
14
|
+
Hint: The first argument must be a string and the second argument must be a instance of InlineStyle
|
|
15
|
+
Using String(firstArgument) and new InlineStyle({}) as fallback`}),t=String(t),e=new y({})),!this.tags[t])return g.warn({message:`[UtilitiesClass.setTagStyle] @briklab/lib/cli-john: Tag "${t}" does not exist!
|
|
16
|
+
Hint: Use a valid tag that you have defined or use "error"|"warn"|"info"`}),this;const n=`${t} Tag Color`;return this.styleSheet.set(n,e),this.tags[t].styleName=n,this}log(t,...e){u.for([t]).check(["string"])||(g.warn({message:`[UtilitiesClass.log] @briklab/lib/cli-john: Invalid Arguments!
|
|
17
|
+
Hint: The first argument must be a string
|
|
18
|
+
Using String(argument1) as fallback`}),t=String(t)),(!e||e.length===0)&&(e=[""]);const n=this.tags[t];if(!n){g.warn({message:`[UtilitiesClass.log] @briklab/lib/cli-john: Tag "${t}" does not exist!
|
|
19
|
+
Hint: Use a valid tag that you have defined or use "error"|"warn"|"info"`}),console.log(...e);return}const o=this.styleSheet.get(n.styleName),c=o?.text??"",h=" ".repeat(n.paddingLeft),s=" ".repeat(n.paddingRight);if(typeof process<"u"&&typeof process.stdout<"u"&&!!process.stdout.isTTY){const p=o?.ansi??"",f=M.RESET;n.showErrorInTag?console.log("["+p+h+n.tag+s+f+"]:",...e):console.log(p+"["+h+n.tag+s+"]"+f+":",...e)}else n.showErrorInTag?console.log(`[%c${h}${n.tag}${s}%c]:`,c,...e):console.log(`%c[${h}${n.tag}${s}]%c:`,c,...e)}error(...t){return(!t||t.length===0)&&(t=[""]),this.log("error",...t),this}warning(...t){return(!t||t.length===0)&&(t=[""]),this.log("warning",...t),this}info(...t){return(!t||t.length===0)&&(t=[""]),this.log("info",...t),this}}const N=new F;export{v as CLI,N as Utilities};
|