@warlock.js/core 4.0.41 → 4.0.46
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/esm/cli/index.js +2 -219
- package/esm/cli/index.js.map +1 -1
- package/esm/cli/start.js +2 -14151
- package/esm/cli/start.js.map +1 -1
- package/esm/index.js +24 -13017
- package/esm/index.js.map +1 -1
- package/package.json +7 -7
package/esm/cli/index.js
CHANGED
|
@@ -1,221 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Constructor
|
|
5
|
-
*/
|
|
6
|
-
constructor(name, description) {
|
|
7
|
-
this.name = name;
|
|
8
|
-
if (description) {
|
|
9
|
-
this.commandDescription = description;
|
|
10
|
-
}
|
|
11
|
-
return this;
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Command source
|
|
15
|
-
*/
|
|
16
|
-
commandSource;
|
|
17
|
-
/**
|
|
18
|
-
* Command action
|
|
19
|
-
*/
|
|
20
|
-
commandAction;
|
|
21
|
-
/**
|
|
22
|
-
* Command pre action
|
|
23
|
-
* This will be executed before loading preloaders
|
|
24
|
-
*/
|
|
25
|
-
commandPreAction;
|
|
26
|
-
/**
|
|
27
|
-
* Command preload
|
|
28
|
-
*/
|
|
29
|
-
commandPreload;
|
|
30
|
-
/**
|
|
31
|
-
* Command description
|
|
32
|
-
*/
|
|
33
|
-
commandDescription;
|
|
34
|
-
/**
|
|
35
|
-
* Command options
|
|
36
|
-
*/
|
|
37
|
-
commandOptions = [];
|
|
38
|
-
/**
|
|
39
|
-
* Command relative path
|
|
40
|
-
* Available only for project commands
|
|
41
|
-
* Auto injected by the framework itself
|
|
42
|
-
*/
|
|
43
|
-
commandRelativePath;
|
|
44
|
-
/**
|
|
45
|
-
* Determine if the command is persistent
|
|
46
|
-
*/
|
|
47
|
-
isPersistent = false;
|
|
48
|
-
/**
|
|
49
|
-
* Command alias (short name)
|
|
50
|
-
*/
|
|
51
|
-
commandAlias;
|
|
52
|
-
/**
|
|
53
|
-
* Add command source
|
|
54
|
-
*/
|
|
55
|
-
source(source) {
|
|
56
|
-
this.commandSource = source;
|
|
57
|
-
return this;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Set command description
|
|
61
|
-
*/
|
|
62
|
-
description(description) {
|
|
63
|
-
this.commandDescription = description;
|
|
64
|
-
return this;
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Determine if the command is persistent
|
|
68
|
-
*/
|
|
69
|
-
persistent(isPersistent = true) {
|
|
70
|
-
this.isPersistent = isPersistent;
|
|
71
|
-
return this;
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Set command alias (short name)
|
|
75
|
-
* @example .alias("m") for "migrate"
|
|
76
|
-
*/
|
|
77
|
-
alias(alias) {
|
|
78
|
-
this.commandAlias = alias;
|
|
79
|
-
return this;
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Command action
|
|
83
|
-
*/
|
|
84
|
-
action(action) {
|
|
85
|
-
this.commandAction = action;
|
|
86
|
-
return this;
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Command pre action
|
|
90
|
-
* This will be executed before loading preloaders
|
|
91
|
-
*/
|
|
92
|
-
preAction(action) {
|
|
93
|
-
this.commandPreAction = action;
|
|
94
|
-
return this;
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Add command options
|
|
98
|
-
*/
|
|
99
|
-
options(options) {
|
|
100
|
-
options.map((option) => this.option(option));
|
|
101
|
-
return this;
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Add command relative path
|
|
105
|
-
*/
|
|
106
|
-
$relativePath(relativePath) {
|
|
107
|
-
this.commandRelativePath = relativePath;
|
|
108
|
-
return this;
|
|
109
|
-
}
|
|
110
|
-
option(...args) {
|
|
111
|
-
let option;
|
|
112
|
-
if (args.length === 1) {
|
|
113
|
-
option = args[0];
|
|
114
|
-
} else {
|
|
115
|
-
option = {
|
|
116
|
-
text: args[0],
|
|
117
|
-
description: args[1],
|
|
118
|
-
...args[2],
|
|
119
|
-
name: ""
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
this.commandOptions.push(this.parseOption(option));
|
|
123
|
-
return this;
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Parse option name and alias if exists
|
|
127
|
-
*
|
|
128
|
-
* Supports formats:
|
|
129
|
-
* - "--port, -p" → name: "port", alias: "p"
|
|
130
|
-
* - "-p, --port" → name: "port", alias: "p"
|
|
131
|
-
* - "--port" → name: "port", alias: undefined
|
|
132
|
-
* - "-p" → name: "p", alias: undefined
|
|
133
|
-
*/
|
|
134
|
-
parseOption(option) {
|
|
135
|
-
const text = option.text.trim();
|
|
136
|
-
const parts = text.split(",").map((part) => part.trim());
|
|
137
|
-
let name = "";
|
|
138
|
-
let alias = "";
|
|
139
|
-
if (parts.length === 1) {
|
|
140
|
-
name = this.extractOptionName(parts[0]);
|
|
141
|
-
} else if (parts.length === 2) {
|
|
142
|
-
const first = parts[0];
|
|
143
|
-
const second = parts[1];
|
|
144
|
-
if (first.startsWith("--")) {
|
|
145
|
-
name = this.extractOptionName(first);
|
|
146
|
-
alias = this.extractOptionName(second);
|
|
147
|
-
} else {
|
|
148
|
-
name = this.extractOptionName(second);
|
|
149
|
-
alias = this.extractOptionName(first);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
if (alias === "h" || name === "help") {
|
|
153
|
-
throw new Error("Help option is not allowed, it's reserved for displaying command help");
|
|
154
|
-
}
|
|
155
|
-
return {
|
|
156
|
-
...option,
|
|
157
|
-
name,
|
|
158
|
-
alias
|
|
159
|
-
};
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* Extract option name from text (removes -- or -)
|
|
163
|
-
*
|
|
164
|
-
* @example
|
|
165
|
-
* extractOptionName("--port") → "port"
|
|
166
|
-
* extractOptionName("-p") → "p"
|
|
167
|
-
* extractOptionName("--port=3000") → "port"
|
|
168
|
-
*/
|
|
169
|
-
extractOptionName(text) {
|
|
170
|
-
let name = text.replace(/^-+/, "");
|
|
171
|
-
const equalIndex = name.indexOf("=");
|
|
172
|
-
if (equalIndex !== -1) {
|
|
173
|
-
name = name.slice(0, equalIndex);
|
|
174
|
-
}
|
|
175
|
-
const spaceIndex = name.indexOf(" ");
|
|
176
|
-
if (spaceIndex !== -1) {
|
|
177
|
-
name = name.slice(0, spaceIndex);
|
|
178
|
-
}
|
|
179
|
-
return name.trim();
|
|
180
|
-
}
|
|
181
|
-
/**
|
|
182
|
-
* Command preload
|
|
183
|
-
*/
|
|
184
|
-
preload(options) {
|
|
185
|
-
this.commandPreload = options;
|
|
186
|
-
return this;
|
|
187
|
-
}
|
|
188
|
-
/**
|
|
189
|
-
* Execute the command
|
|
190
|
-
*/
|
|
191
|
-
async execute(data) {
|
|
192
|
-
if (!this.commandAction) {
|
|
193
|
-
throw new Error(`Command "${this.name}" has no action defined`);
|
|
194
|
-
}
|
|
195
|
-
await this.commandAction(data);
|
|
196
|
-
}
|
|
197
|
-
};
|
|
198
|
-
function command(options) {
|
|
199
|
-
const commandInstnace = new CLICommand(options.name, options.description);
|
|
200
|
-
if (options.preload) {
|
|
201
|
-
commandInstnace.preload(options.preload);
|
|
202
|
-
}
|
|
203
|
-
if (options.persistent) {
|
|
204
|
-
commandInstnace.persistent(options.persistent);
|
|
205
|
-
}
|
|
206
|
-
if (options.alias) {
|
|
207
|
-
commandInstnace.alias(options.alias);
|
|
208
|
-
}
|
|
209
|
-
commandInstnace.action(options.action);
|
|
210
|
-
if (options.options) {
|
|
211
|
-
commandInstnace.options(options.options);
|
|
212
|
-
}
|
|
213
|
-
if (options.preAction) {
|
|
214
|
-
commandInstnace.preAction(options.preAction);
|
|
215
|
-
}
|
|
216
|
-
return commandInstnace;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
export { CLICommand, command };
|
|
1
|
+
export * from './cli-command';
|
|
2
|
+
export * from './types';
|
|
220
3
|
//# sourceMappingURL=index.js.map
|
|
221
4
|
//# sourceMappingURL=index.js.map
|
package/esm/cli/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../../../../warlock.js/core/src/cli/cli-command.ts"],"names":[],"mappings":";AAUO,IAAM,aAAN,MAAiB;AAAA;AAAA;AAAA;AAAA,EAoDf,WAAA,CACE,MACP,WAAA,EACA;AAFO,IAAA,IAAA,CAAA,IAAA,GAAA,IAAA;AAGP,IAAA,IAAI,WAAA,EAAa;AACf,MAAA,IAAA,CAAK,kBAAA,GAAqB,WAAA;AAAA,IAC5B;AAEA,IAAA,OAAO,IAAA;AAAA,EACT;AAAA;AAAA;AAAA;AAAA,EAzDO,aAAA;AAAA;AAAA;AAAA;AAAA,EAKA,aAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,gBAAA;AAAA;AAAA;AAAA;AAAA,EAKA,cAAA;AAAA;AAAA;AAAA;AAAA,EAKA,kBAAA;AAAA;AAAA;AAAA;AAAA,EAKA,iBAA6C,EAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAO9C,mBAAA;AAAA;AAAA;AAAA;AAAA,EAKA,YAAA,GAAwB,KAAA;AAAA;AAAA;AAAA;AAAA,EAKxB,YAAA;AAAA;AAAA;AAAA;AAAA,EAmBA,OAAO,MAAA,EAAgC;AAC5C,IAAA,IAAA,CAAK,aAAA,GAAgB,MAAA;AACrB,IAAA,OAAO,IAAA;AAAA,EACT;AAAA;AAAA;AAAA;AAAA,EAKO,YAAY,WAAA,EAA2B;AAC5C,IAAA,IAAA,CAAK,kBAAA,GAAqB,WAAA;AAC1B,IAAA,OAAO,IAAA;AAAA,EACT;AAAA;AAAA;AAAA;AAAA,EAKO,UAAA,CAAW,eAAe,IAAA,EAAY;AAC3C,IAAA,IAAA,CAAK,YAAA,GAAe,YAAA;AACpB,IAAA,OAAO,IAAA;AAAA,EACT;AAAA;AAAA;AAAA;AA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A,EAAA,IAAI,QAAQ,KAAA,EAAO;AACjB,IAAA,eAAA,CAAgB,KAAA,CAAM,QAAQ,KAAK,CAAA;AAAA,EACrC;AAEA,EAAA,eAAA,CAAgB,MAAA,CAAO,QAAQ,MAAM,CAAA;AAErC,EAAA,IAAI,QAAQ,OAAA,EAAS;AACnB,IAAA,eAAA,CAAgB,OAAA,CAAQ,QAAQ,OAAO,CAAA;AAAA,EACzC;AAEA,EAAA,IAAI,QAAQ,SAAA,EAAW;AACrB,IAAA,eAAA,CAAgB,SAAA,CAAU,QAAQ,SAAS,CAAA;AAAA,EAC7C;AAEA,EAAA,OAAO,eAAA;AACT","file":"index.js","sourcesContent":["import type {\r\n CLICommandAction,\r\n CLICommandOption,\r\n CLICommandOptions,\r\n CLICommandPreload,\r\n CLICommandSource,\r\n CommandActionData,\r\n ResolvedCLICommandOption,\r\n} from \"./types\";\r\n\r\nexport class CLICommand {\r\n /**\r\n * Command source\r\n */\r\n public commandSource?: CLICommandSource;\r\n\r\n /**\r\n * Command action\r\n */\r\n public commandAction?: CLICommandAction;\r\n\r\n /**\r\n * Command pre action\r\n * This will be executed before loading preloaders\r\n */\r\n public commandPreAction?: CLICommandAction;\r\n\r\n /**\r\n * Command preload\r\n */\r\n public commandPreload?: CLICommandPreload;\r\n\r\n /**\r\n * Command description\r\n */\r\n public commandDescription?: string;\r\n\r\n /**\r\n * Command options\r\n */\r\n public commandOptions: ResolvedCLICommandOption[] = [];\r\n\r\n /**\r\n * Command relative path\r\n * Available only for project commands\r\n * Auto injected by the framework itself\r\n */\r\n public commandRelativePath?: string;\r\n\r\n /**\r\n * Determine if the command is persistent\r\n */\r\n public isPersistent: boolean = false;\r\n\r\n /**\r\n * Command alias (short name)\r\n */\r\n public commandAlias?: string;\r\n\r\n /**\r\n * Constructor\r\n */\r\n public constructor(\r\n public name: string,\r\n description?: string,\r\n ) {\r\n if (description) {\r\n this.commandDescription = description;\r\n }\r\n\r\n return this;\r\n }\r\n\r\n /**\r\n * Add command source\r\n */\r\n public source(source: CLICommandSource): this {\r\n this.commandSource = source;\r\n return this;\r\n }\r\n\r\n /**\r\n * Set command description\r\n */\r\n public description(description: string): this {\r\n this.commandDescription = description;\r\n return this;\r\n }\r\n\r\n /**\r\n * Determine if the command is persistent\r\n */\r\n public persistent(isPersistent = true): this {\r\n this.isPersistent = isPersistent;\r\n return this;\r\n }\r\n\r\n /**\r\n * Set command alias (short name)\r\n * @example .alias(\"m\") for \"migrate\"\r\n */\r\n public alias(alias: string): this {\r\n this.commandAlias = alias;\r\n return this;\r\n }\r\n\r\n /**\r\n * Command action\r\n */\r\n public action(action: CLICommandAction): this {\r\n this.commandAction = action;\r\n return this;\r\n }\r\n\r\n /**\r\n * Command pre action\r\n * This will be executed before loading preloaders\r\n */\r\n public preAction(action: CLICommandAction): this {\r\n this.commandPreAction = action;\r\n return this;\r\n }\r\n\r\n /**\r\n * Add command options\r\n */\r\n public options(options: CLICommandOption[]): this {\r\n options.map((option) => this.option(option));\r\n\r\n return this;\r\n }\r\n\r\n /**\r\n * Add command relative path\r\n */\r\n public $relativePath(relativePath: string) {\r\n this.commandRelativePath = relativePath;\r\n return this;\r\n }\r\n\r\n /**\r\n * Add command option\r\n */\r\n public option(option: CLICommandOption): this;\r\n public option(name: string, description?: string, options?: Omit<CLICommandOption, \"name\">): this;\r\n public option(\r\n ...args: [CLICommandOption] | [string, string?, Omit<CLICommandOption, \"name\">?]\r\n ): this {\r\n let option: CLICommandOption;\r\n if (args.length === 1) {\r\n option = args[0] as CLICommandOption;\r\n } else {\r\n option = {\r\n text: args[0],\r\n description: args[1],\r\n ...args[2],\r\n name: \"\",\r\n };\r\n }\r\n\r\n this.commandOptions.push(this.parseOption(option));\r\n\r\n return this;\r\n }\r\n\r\n /**\r\n * Parse option name and alias if exists\r\n *\r\n * Supports formats:\r\n * - \"--port, -p\" → name: \"port\", alias: \"p\"\r\n * - \"-p, --port\" → name: \"port\", alias: \"p\"\r\n * - \"--port\" → name: \"port\", alias: undefined\r\n * - \"-p\" → name: \"p\", alias: undefined\r\n */\r\n protected parseOption(option: CLICommandOption): ResolvedCLICommandOption {\r\n const text = option.text.trim();\r\n\r\n // Split by comma to check for alias\r\n const parts = text.split(\",\").map((part) => part.trim());\r\n\r\n let name = \"\";\r\n let alias = \"\";\r\n\r\n if (parts.length === 1) {\r\n // Single option: \"--port\" or \"-p\"\r\n name = this.extractOptionName(parts[0]);\r\n } else if (parts.length === 2) {\r\n // Two options: \"--port, -p\" or \"-p, --port\"\r\n const first = parts[0];\r\n const second = parts[1];\r\n\r\n // Determine which is the long form (name) and which is short (alias)\r\n if (first.startsWith(\"--\")) {\r\n name = this.extractOptionName(first);\r\n alias = this.extractOptionName(second);\r\n } else {\r\n name = this.extractOptionName(second);\r\n alias = this.extractOptionName(first);\r\n }\r\n }\r\n\r\n if (alias === \"h\" || name === \"help\") {\r\n throw new Error(\"Help option is not allowed, it's reserved for displaying command help\");\r\n }\r\n\r\n return {\r\n ...option,\r\n name,\r\n alias,\r\n };\r\n }\r\n\r\n /**\r\n * Extract option name from text (removes -- or -)\r\n *\r\n * @example\r\n * extractOptionName(\"--port\") → \"port\"\r\n * extractOptionName(\"-p\") → \"p\"\r\n * extractOptionName(\"--port=3000\") → \"port\"\r\n */\r\n private extractOptionName(text: string): string {\r\n // Remove leading dashes\r\n let name = text.replace(/^-+/, \"\");\r\n\r\n // Remove value assignment if exists (e.g., \"--port=3000\" → \"port\")\r\n const equalIndex = name.indexOf(\"=\");\r\n if (equalIndex !== -1) {\r\n name = name.slice(0, equalIndex);\r\n }\r\n\r\n // Remove angle brackets if exists (e.g., \"--port <number>\" → \"port\")\r\n const spaceIndex = name.indexOf(\" \");\r\n if (spaceIndex !== -1) {\r\n name = name.slice(0, spaceIndex);\r\n }\r\n\r\n return name.trim();\r\n }\r\n\r\n /**\r\n * Command preload\r\n */\r\n public preload(options: CLICommandPreload) {\r\n this.commandPreload = options;\r\n return this;\r\n }\r\n\r\n /**\r\n * Execute the command\r\n */\r\n public async execute(data: CommandActionData) {\r\n if (!this.commandAction) {\r\n throw new Error(`Command \"${this.name}\" has no action defined`);\r\n }\r\n\r\n await this.commandAction(data);\r\n }\r\n}\r\n\r\nexport function command(options: CLICommandOptions) {\r\n const commandInstnace = new CLICommand(options.name, options.description);\r\n\r\n if (options.preload) {\r\n commandInstnace.preload(options.preload);\r\n }\r\n\r\n if (options.persistent) {\r\n commandInstnace.persistent(options.persistent);\r\n }\r\n\r\n if (options.alias) {\r\n commandInstnace.alias(options.alias);\r\n }\r\n\r\n commandInstnace.action(options.action);\r\n\r\n if (options.options) {\r\n commandInstnace.options(options.options);\r\n }\r\n\r\n if (options.preAction) {\r\n commandInstnace.preAction(options.preAction);\r\n }\r\n\r\n return commandInstnace;\r\n}\r\n"]}
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"index.js","sourcesContent":[]}
|