@h3ravel/console 11.0.1 → 11.0.3
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/bin/run.cjs +882 -0
- package/bin/run.js +874 -0
- package/dist/chunk-CTQ6DUT2.js +8 -0
- package/dist/index.cjs +33 -0
- package/dist/index.js +1 -1
- package/dist/run.cjs +931 -0
- package/dist/run.js +14 -1
- package/package.json +3 -2
package/bin/run.cjs
ADDED
|
@@ -0,0 +1,882 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var events = require('events');
|
|
5
|
+
var core = require('@h3ravel/core');
|
|
6
|
+
var promises = require('fs/promises');
|
|
7
|
+
var chalk = require('chalk');
|
|
8
|
+
var nodepath = require('path');
|
|
9
|
+
var fs = require('fs');
|
|
10
|
+
var dayjs = require('dayjs');
|
|
11
|
+
var arquebus = require('@h3ravel/arquebus');
|
|
12
|
+
var child_process = require('child_process');
|
|
13
|
+
var commander = require('commander');
|
|
14
|
+
var config = require('@h3ravel/config');
|
|
15
|
+
var database = require('@h3ravel/database');
|
|
16
|
+
|
|
17
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
18
|
+
|
|
19
|
+
var chalk__default = /*#__PURE__*/_interopDefault(chalk);
|
|
20
|
+
var nodepath__default = /*#__PURE__*/_interopDefault(nodepath);
|
|
21
|
+
var dayjs__default = /*#__PURE__*/_interopDefault(dayjs);
|
|
22
|
+
|
|
23
|
+
var __defProp = Object.defineProperty;
|
|
24
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
25
|
+
function sync_default(start, callback) {
|
|
26
|
+
let dir = nodepath.resolve(".", start);
|
|
27
|
+
let tmp, stats = fs.statSync(dir);
|
|
28
|
+
if (!stats.isDirectory()) {
|
|
29
|
+
dir = nodepath.dirname(dir);
|
|
30
|
+
}
|
|
31
|
+
while (true) {
|
|
32
|
+
tmp = callback(dir, fs.readdirSync(dir));
|
|
33
|
+
if (tmp) return nodepath.resolve(dir, tmp);
|
|
34
|
+
dir = nodepath.dirname(tmp = dir);
|
|
35
|
+
if (tmp === dir) break;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
__name(sync_default, "default");
|
|
39
|
+
var join = nodepath__default.default.join;
|
|
40
|
+
var Utils = class {
|
|
41
|
+
static {
|
|
42
|
+
__name(this, "Utils");
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Wraps text with chalk
|
|
46
|
+
*
|
|
47
|
+
* @param txt
|
|
48
|
+
* @param color
|
|
49
|
+
* @returns
|
|
50
|
+
*/
|
|
51
|
+
static textFormat(txt, color) {
|
|
52
|
+
return String(txt).split(":").map((e, i, a) => i == 0 && a.length > 1 ? color(" " + e + ": ") : e).join("");
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Ouput formater object
|
|
56
|
+
*
|
|
57
|
+
* @returns
|
|
58
|
+
*/
|
|
59
|
+
static output() {
|
|
60
|
+
return {
|
|
61
|
+
success: /* @__PURE__ */ __name((msg, exit = false) => {
|
|
62
|
+
console.log(chalk__default.default.green("\u2713"), this.textFormat(msg, chalk__default.default.bgGreen), "\n");
|
|
63
|
+
if (exit) process.exit(0);
|
|
64
|
+
}, "success"),
|
|
65
|
+
info: /* @__PURE__ */ __name((msg, exit = false) => {
|
|
66
|
+
console.log(chalk__default.default.blue("\u2139"), this.textFormat(msg, chalk__default.default.bgBlue), "\n");
|
|
67
|
+
if (exit) process.exit(0);
|
|
68
|
+
}, "info"),
|
|
69
|
+
error: /* @__PURE__ */ __name((msg, exit = true) => {
|
|
70
|
+
if (msg instanceof Error) {
|
|
71
|
+
if (msg.message) {
|
|
72
|
+
console.error(chalk__default.default.red("\u2716"), this.textFormat("ERROR:" + msg.message, chalk__default.default.bgRed));
|
|
73
|
+
}
|
|
74
|
+
console.error(chalk__default.default.red(`${msg.detail ? `${msg.detail}
|
|
75
|
+
` : ""}${msg.stack}`), "\n");
|
|
76
|
+
} else {
|
|
77
|
+
console.error(chalk__default.default.red("\u2716"), this.textFormat(msg, chalk__default.default.bgRed), "\n");
|
|
78
|
+
}
|
|
79
|
+
if (exit) process.exit(1);
|
|
80
|
+
}, "error"),
|
|
81
|
+
split: /* @__PURE__ */ __name((name, value, status, exit = false) => {
|
|
82
|
+
status ??= "info";
|
|
83
|
+
const color = {
|
|
84
|
+
success: chalk__default.default.bgGreen,
|
|
85
|
+
info: chalk__default.default.bgBlue,
|
|
86
|
+
error: chalk__default.default.bgRed
|
|
87
|
+
};
|
|
88
|
+
const regex = /\x1b\[\d+m/g;
|
|
89
|
+
const width = Math.min(process.stdout.columns, 100);
|
|
90
|
+
const dots = Math.max(width - name.replace(regex, "").length - value.replace(regex, "").length - 10, 0);
|
|
91
|
+
console.log(this.textFormat(name, color[status]), chalk__default.default.gray(".".repeat(dots)), value);
|
|
92
|
+
if (exit) process.exit(0);
|
|
93
|
+
}, "split"),
|
|
94
|
+
quiet: /* @__PURE__ */ __name(() => {
|
|
95
|
+
process.exit(0);
|
|
96
|
+
}, "quiet")
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
static findModulePkg(moduleId, cwd) {
|
|
100
|
+
const parts = moduleId.replace(/\\/g, "/").split("/");
|
|
101
|
+
let packageName = "";
|
|
102
|
+
if (parts.length > 0 && parts[0][0] === "@") {
|
|
103
|
+
packageName += parts.shift() + "/";
|
|
104
|
+
}
|
|
105
|
+
packageName += parts.shift();
|
|
106
|
+
const packageJson = nodepath__default.default.join(cwd ?? process.cwd(), "node_modules", packageName);
|
|
107
|
+
const resolved = this.findUpConfig(packageJson, "package", [
|
|
108
|
+
"json"
|
|
109
|
+
]);
|
|
110
|
+
if (!resolved) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
return nodepath__default.default.join(nodepath__default.default.dirname(resolved), parts.join("/"));
|
|
114
|
+
}
|
|
115
|
+
static async getMigrationPaths(cwd, migrator, defaultPath, path3) {
|
|
116
|
+
if (path3) {
|
|
117
|
+
return [
|
|
118
|
+
join(cwd, path3)
|
|
119
|
+
];
|
|
120
|
+
}
|
|
121
|
+
return [
|
|
122
|
+
...migrator.getPaths(),
|
|
123
|
+
join(cwd, defaultPath)
|
|
124
|
+
];
|
|
125
|
+
}
|
|
126
|
+
static twoColumnDetail(name, value) {
|
|
127
|
+
const regex = /\x1b\[\d+m/g;
|
|
128
|
+
const width = Math.min(process.stdout.columns, 100);
|
|
129
|
+
const dots = Math.max(width - name.replace(regex, "").length - value.replace(regex, "").length - 10, 0);
|
|
130
|
+
return console.log(name, chalk__default.default.gray(".".repeat(dots)), value);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Check if file exists
|
|
134
|
+
*
|
|
135
|
+
* @param path
|
|
136
|
+
* @returns
|
|
137
|
+
*/
|
|
138
|
+
static async fileExists(path3) {
|
|
139
|
+
try {
|
|
140
|
+
await promises.access(path3);
|
|
141
|
+
return true;
|
|
142
|
+
} catch {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
static findUpConfig(cwd, name, extensions) {
|
|
147
|
+
return sync_default(cwd, (_dir, names) => {
|
|
148
|
+
for (const ext of extensions) {
|
|
149
|
+
const filename = `${name}.${ext}`;
|
|
150
|
+
if (names.includes(filename)) {
|
|
151
|
+
return filename;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return false;
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
var TableGuesser = class TableGuesser2 {
|
|
159
|
+
static {
|
|
160
|
+
__name(this, "TableGuesser");
|
|
161
|
+
}
|
|
162
|
+
static CREATE_PATTERNS = [
|
|
163
|
+
/^create_(\w+)_table$/,
|
|
164
|
+
/^create_(\w+)$/
|
|
165
|
+
];
|
|
166
|
+
static CHANGE_PATTERNS = [
|
|
167
|
+
/.+_(to|from|in)_(\w+)_table$/,
|
|
168
|
+
/.+_(to|from|in)_(\w+)$/
|
|
169
|
+
];
|
|
170
|
+
static guess(migration) {
|
|
171
|
+
for (const pattern of TableGuesser2.CREATE_PATTERNS) {
|
|
172
|
+
const matches = migration.match(pattern);
|
|
173
|
+
if (matches) {
|
|
174
|
+
return [
|
|
175
|
+
matches[1],
|
|
176
|
+
true
|
|
177
|
+
];
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
for (const pattern of TableGuesser2.CHANGE_PATTERNS) {
|
|
181
|
+
const matches = migration.match(pattern);
|
|
182
|
+
if (matches) {
|
|
183
|
+
return [
|
|
184
|
+
matches[2],
|
|
185
|
+
false
|
|
186
|
+
];
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return [];
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
// src/Commands/Command.ts
|
|
194
|
+
var Command = class {
|
|
195
|
+
static {
|
|
196
|
+
__name(this, "Command");
|
|
197
|
+
}
|
|
198
|
+
app;
|
|
199
|
+
kernel;
|
|
200
|
+
constructor(app, kernel) {
|
|
201
|
+
this.app = app;
|
|
202
|
+
this.kernel = kernel;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* The name and signature of the console command.
|
|
206
|
+
*
|
|
207
|
+
* @var string
|
|
208
|
+
*/
|
|
209
|
+
signature;
|
|
210
|
+
/**
|
|
211
|
+
* A dictionary of signatures or what not.
|
|
212
|
+
*
|
|
213
|
+
* @var object
|
|
214
|
+
*/
|
|
215
|
+
dictionary = {};
|
|
216
|
+
/**
|
|
217
|
+
* The console command description.
|
|
218
|
+
*
|
|
219
|
+
* @var string
|
|
220
|
+
*/
|
|
221
|
+
description;
|
|
222
|
+
/**
|
|
223
|
+
* The console command input.
|
|
224
|
+
*
|
|
225
|
+
* @var object
|
|
226
|
+
*/
|
|
227
|
+
input = {
|
|
228
|
+
options: {},
|
|
229
|
+
arguments: {}
|
|
230
|
+
};
|
|
231
|
+
/**
|
|
232
|
+
* Execute the console command.
|
|
233
|
+
*/
|
|
234
|
+
async handle(..._args) {
|
|
235
|
+
}
|
|
236
|
+
setApplication(app) {
|
|
237
|
+
this.app = app;
|
|
238
|
+
}
|
|
239
|
+
setInput(options, args, regArgs, dictionary) {
|
|
240
|
+
this.dictionary = dictionary;
|
|
241
|
+
this.input.options = options;
|
|
242
|
+
this.input.arguments = regArgs.map((e, i) => ({
|
|
243
|
+
[e.name()]: args[i]
|
|
244
|
+
})).reduce((e, x) => Object.assign(e, x), {});
|
|
245
|
+
}
|
|
246
|
+
getSignature() {
|
|
247
|
+
return this.signature;
|
|
248
|
+
}
|
|
249
|
+
getDescription() {
|
|
250
|
+
return this.description;
|
|
251
|
+
}
|
|
252
|
+
option(key, def) {
|
|
253
|
+
return this.input.options[key] ?? def;
|
|
254
|
+
}
|
|
255
|
+
options(key) {
|
|
256
|
+
if (key) {
|
|
257
|
+
return this.input.options[key];
|
|
258
|
+
}
|
|
259
|
+
return this.input.options;
|
|
260
|
+
}
|
|
261
|
+
argument(key, def) {
|
|
262
|
+
return this.input.arguments[key] ?? def;
|
|
263
|
+
}
|
|
264
|
+
arguments() {
|
|
265
|
+
return this.input.arguments;
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
var MakeCommand = class extends Command {
|
|
269
|
+
static {
|
|
270
|
+
__name(this, "MakeCommand");
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* The name and signature of the console command.
|
|
274
|
+
*
|
|
275
|
+
* @var string
|
|
276
|
+
*/
|
|
277
|
+
signature = `#make:
|
|
278
|
+
{controller : Generates a new controller class. | {--a|api : Generate an API resource controller} | {--force : Overide existing controller.} }
|
|
279
|
+
{resource : Generates a new API resource class.}
|
|
280
|
+
{migration : Generates a new database migration class. | {--l|type=ts : The file type to generate} | {--t|table : The table to migrate} | {--c|create : The table to be created} }
|
|
281
|
+
{factory : Generates a new database factory class.}
|
|
282
|
+
{seeder : Generates a new database seeder class.}
|
|
283
|
+
{model : Generates a new Arquebus model class. | {--t|type=ts : The file type to generate}}
|
|
284
|
+
{^name : The name of the [name] to generate}
|
|
285
|
+
`;
|
|
286
|
+
/**
|
|
287
|
+
* The console command description.
|
|
288
|
+
*
|
|
289
|
+
* @var string
|
|
290
|
+
*/
|
|
291
|
+
description = "Generate component classes";
|
|
292
|
+
async handle() {
|
|
293
|
+
const command = this.dictionary.baseCommand;
|
|
294
|
+
const methods = {
|
|
295
|
+
controller: "makeController",
|
|
296
|
+
resource: "makeResource",
|
|
297
|
+
migration: "makeMigration",
|
|
298
|
+
factory: "makeFactory",
|
|
299
|
+
seeder: "makeSeeder",
|
|
300
|
+
model: "makeModel"
|
|
301
|
+
};
|
|
302
|
+
try {
|
|
303
|
+
await this?.[methods[command]]();
|
|
304
|
+
} catch (e) {
|
|
305
|
+
this.kernel.output.error(e);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Generate a new controller class.
|
|
310
|
+
*/
|
|
311
|
+
async makeController() {
|
|
312
|
+
const type = this.option("api") ? "-resource" : "";
|
|
313
|
+
const name = this.argument("name");
|
|
314
|
+
const force = this.option("force");
|
|
315
|
+
const path3 = nodepath__default.default.join(app_path("Http/Controllers"), name + ".ts");
|
|
316
|
+
const dbPath = Utils.findModulePkg("@h3ravel/http", this.kernel.cwd) ?? "";
|
|
317
|
+
const stubPath = nodepath__default.default.join(dbPath, `dist/stubs/controller${type}.stub`);
|
|
318
|
+
if (!force && fs.existsSync(path3)) {
|
|
319
|
+
this.kernel.output.error(`ERORR: ${name} controller already exists`);
|
|
320
|
+
}
|
|
321
|
+
let stub = await promises.readFile(stubPath, "utf-8");
|
|
322
|
+
stub = stub.replace(/{{ name }}/g, name);
|
|
323
|
+
await promises.writeFile(path3, stub);
|
|
324
|
+
this.kernel.output.split(`INFO: Controller Created`, chalk__default.default.gray(nodepath__default.default.basename(path3)));
|
|
325
|
+
}
|
|
326
|
+
makeResource() {
|
|
327
|
+
this.kernel.output.success(`Resource support is not yet available`);
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Generate a new database migration class
|
|
331
|
+
*/
|
|
332
|
+
async makeMigration() {
|
|
333
|
+
const name = this.argument("name");
|
|
334
|
+
const datePrefix = dayjs__default.default().format("YYYY_MM_DD_HHmmss");
|
|
335
|
+
const path3 = nodepath__default.default.join(database_path("migrations"), `${datePrefix}_${name}.ts`);
|
|
336
|
+
const dbPath = Utils.findModulePkg("@h3ravel/database", this.kernel.cwd) ?? "";
|
|
337
|
+
let create = this.option("create", false);
|
|
338
|
+
let table = this.option("table");
|
|
339
|
+
if (!table && typeof create === "string") {
|
|
340
|
+
table = create;
|
|
341
|
+
create = true;
|
|
342
|
+
}
|
|
343
|
+
if (!table) {
|
|
344
|
+
const guessed = TableGuesser.guess(name);
|
|
345
|
+
table = guessed[0];
|
|
346
|
+
create = !!guessed[1];
|
|
347
|
+
}
|
|
348
|
+
const stubPath = nodepath__default.default.join(dbPath, this.getMigrationStubName(table, create));
|
|
349
|
+
let stub = await promises.readFile(stubPath, "utf-8");
|
|
350
|
+
if (table !== null) {
|
|
351
|
+
stub = stub.replace(/DummyTable|{{\s*table\s*}}/g, table);
|
|
352
|
+
}
|
|
353
|
+
this.kernel.output.info("INFO: Creating Migration");
|
|
354
|
+
await this.kernel.ensureDirectoryExists(nodepath__default.default.dirname(path3));
|
|
355
|
+
await promises.writeFile(path3, stub);
|
|
356
|
+
this.kernel.output.split(`INFO: Migration Created`, chalk__default.default.gray(nodepath__default.default.basename(path3)));
|
|
357
|
+
}
|
|
358
|
+
makeFactory() {
|
|
359
|
+
this.kernel.output.success(`Factory support is not yet available`);
|
|
360
|
+
}
|
|
361
|
+
makeSeeder() {
|
|
362
|
+
this.kernel.output.success(`Seeder support is not yet available`);
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Generate a new Arquebus model class
|
|
366
|
+
*/
|
|
367
|
+
async makeModel() {
|
|
368
|
+
const type = this.option("type", "ts");
|
|
369
|
+
const name = this.argument("name");
|
|
370
|
+
const path3 = nodepath__default.default.join(app_path("Models"), name.toLowerCase() + "." + type);
|
|
371
|
+
const dbPath = Utils.findModulePkg("@h3ravel/database", this.kernel.cwd) ?? "";
|
|
372
|
+
const stubPath = nodepath__default.default.join(dbPath, `dist/stubs/model-${type}.stub`);
|
|
373
|
+
let stub = await promises.readFile(stubPath, "utf-8");
|
|
374
|
+
stub = stub.replace(/{{ name }}/g, name);
|
|
375
|
+
await promises.writeFile(path3, stub);
|
|
376
|
+
this.kernel.output.split(`INFO: Model Created`, chalk__default.default.gray(nodepath__default.default.basename(path3)));
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Ge the database migration file name
|
|
380
|
+
*
|
|
381
|
+
* @param table
|
|
382
|
+
* @param create
|
|
383
|
+
* @param type
|
|
384
|
+
* @returns
|
|
385
|
+
*/
|
|
386
|
+
getMigrationStubName(table, create = false, type = "ts") {
|
|
387
|
+
let stub;
|
|
388
|
+
if (!table) {
|
|
389
|
+
stub = `migration-${type}.stub`;
|
|
390
|
+
} else if (create) {
|
|
391
|
+
stub = `migration.create-${type}.stub`;
|
|
392
|
+
} else {
|
|
393
|
+
stub = `migration.update-${type}.stub`;
|
|
394
|
+
}
|
|
395
|
+
return "dist/stubs/" + stub;
|
|
396
|
+
}
|
|
397
|
+
};
|
|
398
|
+
var MigrateCommand = class extends Command {
|
|
399
|
+
static {
|
|
400
|
+
__name(this, "MigrateCommand");
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* The name and signature of the console command.
|
|
404
|
+
*
|
|
405
|
+
* @var string
|
|
406
|
+
*/
|
|
407
|
+
signature = `migrate:
|
|
408
|
+
{fresh : Drop all tables and re-run all migrations.}
|
|
409
|
+
{install : Create the migration repository.}
|
|
410
|
+
{refresh : Reset and re-run all migrations.}
|
|
411
|
+
{reset : Rollback all database migrations.}
|
|
412
|
+
{rollback : Rollback the last database migration.}
|
|
413
|
+
{status : Show the status of each migration.}
|
|
414
|
+
{publish : Publish any migration files from installed packages.}
|
|
415
|
+
{^--s|seed : Seed the database}
|
|
416
|
+
`;
|
|
417
|
+
/**
|
|
418
|
+
* The console command description.
|
|
419
|
+
*
|
|
420
|
+
* @var string
|
|
421
|
+
*/
|
|
422
|
+
description = "Run all pending migrations.";
|
|
423
|
+
/**
|
|
424
|
+
* Execute the console command.
|
|
425
|
+
*/
|
|
426
|
+
async handle() {
|
|
427
|
+
const command = this.dictionary.name ?? this.dictionary.baseCommand;
|
|
428
|
+
const methods = {
|
|
429
|
+
migrate: "migrateRun",
|
|
430
|
+
fresh: "migrateFresh",
|
|
431
|
+
install: "migrateInstall",
|
|
432
|
+
refresh: "migrateRefresh",
|
|
433
|
+
reset: "migrateReset",
|
|
434
|
+
rollback: "migrateRollback",
|
|
435
|
+
status: "migrateStatus",
|
|
436
|
+
publish: "migratePublish"
|
|
437
|
+
};
|
|
438
|
+
await this?.[methods[command]]();
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* Run all pending migrations.
|
|
442
|
+
*/
|
|
443
|
+
async migrateRun() {
|
|
444
|
+
this.kernel.output.success(`Running migrations are not yet supported.`);
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* Drop all tables and re-run all migrations.
|
|
448
|
+
*/
|
|
449
|
+
async migrateFresh() {
|
|
450
|
+
this.kernel.output.success(`Drop all tables and re-run all migrations.`);
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Create the migration repository.
|
|
454
|
+
*/
|
|
455
|
+
async migrateInstall() {
|
|
456
|
+
this.kernel.output.success(`Create the migration repository.`);
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* Reset and re-run all migrations.
|
|
460
|
+
*/
|
|
461
|
+
async migrateRefresh() {
|
|
462
|
+
this.kernel.output.success(`Resetting and re-running migrations is not yet supported.`);
|
|
463
|
+
}
|
|
464
|
+
/**
|
|
465
|
+
* Rollback all database migrations.
|
|
466
|
+
*/
|
|
467
|
+
async migrateReset() {
|
|
468
|
+
this.kernel.output.success(`Rolling back all migration is not yet supported.`);
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* Rollback the last database migration.
|
|
472
|
+
*/
|
|
473
|
+
async migrateRollback() {
|
|
474
|
+
this.kernel.output.success(`Rolling back the last migration is not yet supported.`);
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* Show the status of each migration.
|
|
478
|
+
*/
|
|
479
|
+
async migrateStatus() {
|
|
480
|
+
app_path();
|
|
481
|
+
console.log(arquebus.arquebus.connection());
|
|
482
|
+
this.kernel.output.success(`Show the status of each migration.`);
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* Publish any migration files from installed packages.
|
|
486
|
+
*/
|
|
487
|
+
async migratePublish() {
|
|
488
|
+
this.kernel.output.success(`Publish any migration files from installed packages.`);
|
|
489
|
+
}
|
|
490
|
+
};
|
|
491
|
+
var ServeCommand = class extends Command {
|
|
492
|
+
static {
|
|
493
|
+
__name(this, "ServeCommand");
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* The name and signature of the console command.
|
|
497
|
+
*
|
|
498
|
+
* @var string
|
|
499
|
+
*/
|
|
500
|
+
signature = "serve";
|
|
501
|
+
/**
|
|
502
|
+
* The console command description.
|
|
503
|
+
*
|
|
504
|
+
* @var string
|
|
505
|
+
*/
|
|
506
|
+
description = "Start the Developement Server";
|
|
507
|
+
async handle() {
|
|
508
|
+
try {
|
|
509
|
+
await this.serve();
|
|
510
|
+
} catch (e) {
|
|
511
|
+
this.kernel.output.error(e);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
async serve() {
|
|
515
|
+
const child = child_process.spawn("tsup-node", {
|
|
516
|
+
stdio: "inherit",
|
|
517
|
+
shell: true,
|
|
518
|
+
env: Object.assign({}, process.env, {
|
|
519
|
+
NODE_ENV: "development"
|
|
520
|
+
}),
|
|
521
|
+
detached: true
|
|
522
|
+
});
|
|
523
|
+
const cleanup = /* @__PURE__ */ __name(() => {
|
|
524
|
+
console.log(111);
|
|
525
|
+
if (child.pid) {
|
|
526
|
+
process.kill(child.pid, "SIGTERM");
|
|
527
|
+
}
|
|
528
|
+
}, "cleanup");
|
|
529
|
+
process.on("SIGINT", () => child.kill("SIGINT"));
|
|
530
|
+
process.on("SIGTERM", () => child.kill("SIGTERM"));
|
|
531
|
+
process.on("SIGINT", () => {
|
|
532
|
+
cleanup();
|
|
533
|
+
process.exit(0);
|
|
534
|
+
});
|
|
535
|
+
process.on("SIGTERM", () => {
|
|
536
|
+
cleanup();
|
|
537
|
+
process.exit(0);
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
};
|
|
541
|
+
|
|
542
|
+
// src/Signature.ts
|
|
543
|
+
var Signature = class _Signature {
|
|
544
|
+
static {
|
|
545
|
+
__name(this, "Signature");
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
548
|
+
* Helper to parse options inside a block of text
|
|
549
|
+
*
|
|
550
|
+
* @param block
|
|
551
|
+
* @returns
|
|
552
|
+
*/
|
|
553
|
+
static parseOptions(block) {
|
|
554
|
+
const options = [];
|
|
555
|
+
const regex = /\{([^{}]+(?:\{[^{}]*\}[^{}]*)*)\}/g;
|
|
556
|
+
let match;
|
|
557
|
+
while ((match = regex.exec(block)) !== null) {
|
|
558
|
+
const shared = "^" === match[1][0] || /:[#^]/.test(match[1]);
|
|
559
|
+
const isHidden = ([
|
|
560
|
+
"#",
|
|
561
|
+
"^"
|
|
562
|
+
].includes(match[1][0]) || /:[#^]/.test(match[1])) && !shared;
|
|
563
|
+
const content = match[1].trim().replace(/[#^]/, "");
|
|
564
|
+
const colonIndex = content.indexOf(":");
|
|
565
|
+
if (colonIndex === -1) {
|
|
566
|
+
options.push({
|
|
567
|
+
name: content
|
|
568
|
+
});
|
|
569
|
+
continue;
|
|
570
|
+
}
|
|
571
|
+
const namePart = content.substring(0, colonIndex).trim();
|
|
572
|
+
let rest = content.substring(colonIndex + 1).trim();
|
|
573
|
+
let description = rest;
|
|
574
|
+
let nestedOptions;
|
|
575
|
+
const pipeIndex = rest.indexOf("|");
|
|
576
|
+
if (pipeIndex !== -1) {
|
|
577
|
+
description = rest.substring(0, pipeIndex).trim();
|
|
578
|
+
const nestedText = rest.substring(pipeIndex + 1).trim();
|
|
579
|
+
const cleanedNestedText = nestedText.replace(/^\{/, "").trim();
|
|
580
|
+
nestedOptions = _Signature.parseOptions("{" + cleanedNestedText + "}");
|
|
581
|
+
} else {
|
|
582
|
+
description = description.trim();
|
|
583
|
+
}
|
|
584
|
+
let name = namePart;
|
|
585
|
+
let required = /[^a-zA-Z0-9_|-]/.test(name);
|
|
586
|
+
let multiple = false;
|
|
587
|
+
if (name.endsWith("?*")) {
|
|
588
|
+
required = false;
|
|
589
|
+
multiple = true;
|
|
590
|
+
name = name.slice(0, -2);
|
|
591
|
+
} else if (name.endsWith("*")) {
|
|
592
|
+
multiple = true;
|
|
593
|
+
name = name.slice(0, -1);
|
|
594
|
+
} else if (name.endsWith("?")) {
|
|
595
|
+
required = false;
|
|
596
|
+
name = name.slice(0, -1);
|
|
597
|
+
}
|
|
598
|
+
const isFlag = name.startsWith("--");
|
|
599
|
+
let flags;
|
|
600
|
+
let defaultValue;
|
|
601
|
+
if (isFlag) {
|
|
602
|
+
const flagParts = name.split("|").map((s) => s.trim());
|
|
603
|
+
flags = [];
|
|
604
|
+
for (let part of flagParts) {
|
|
605
|
+
if (part.startsWith("--") && part.slice(2).length === 1) {
|
|
606
|
+
part = "-" + part.slice(2);
|
|
607
|
+
} else if (part.startsWith("-") && !part.startsWith("--") && part.slice(1).length > 1) {
|
|
608
|
+
part = "--" + part.slice(1);
|
|
609
|
+
} else if (!part.startsWith("-") && part.slice(1).length > 1) {
|
|
610
|
+
part = "--" + part;
|
|
611
|
+
}
|
|
612
|
+
const eqIndex = part.indexOf("=");
|
|
613
|
+
if (eqIndex !== -1) {
|
|
614
|
+
flags.push(part.substring(0, eqIndex));
|
|
615
|
+
const val = part.substring(eqIndex + 1);
|
|
616
|
+
if (val === "*") {
|
|
617
|
+
defaultValue = [];
|
|
618
|
+
} else if (val === "true" || val === "false" || !val && !required) {
|
|
619
|
+
defaultValue = val === "true";
|
|
620
|
+
} else if (!isNaN(Number(val))) {
|
|
621
|
+
defaultValue = Number(val);
|
|
622
|
+
} else {
|
|
623
|
+
defaultValue = val;
|
|
624
|
+
}
|
|
625
|
+
} else {
|
|
626
|
+
flags.push(part);
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
options.push({
|
|
631
|
+
name: isFlag ? flags[flags.length - 1] : name,
|
|
632
|
+
required,
|
|
633
|
+
multiple,
|
|
634
|
+
description,
|
|
635
|
+
flags,
|
|
636
|
+
shared,
|
|
637
|
+
isFlag,
|
|
638
|
+
isHidden,
|
|
639
|
+
defaultValue,
|
|
640
|
+
nestedOptions
|
|
641
|
+
});
|
|
642
|
+
}
|
|
643
|
+
return options;
|
|
644
|
+
}
|
|
645
|
+
/**
|
|
646
|
+
* Helper to parse a command's signature
|
|
647
|
+
*
|
|
648
|
+
* @param signature
|
|
649
|
+
* @param commandClass
|
|
650
|
+
* @returns
|
|
651
|
+
*/
|
|
652
|
+
static parseSignature(signature, commandClass) {
|
|
653
|
+
const lines = signature.split("\n").map((l) => l.trim()).filter((l) => l.length > 0);
|
|
654
|
+
const isHidden = [
|
|
655
|
+
"#",
|
|
656
|
+
"^"
|
|
657
|
+
].includes(lines[0][0]) || /:[#^]/.test(lines[0]);
|
|
658
|
+
const baseCommand = lines[0].replace(/[^\w=:-]/g, "");
|
|
659
|
+
const description = commandClass.getDescription();
|
|
660
|
+
const isNamespaceCommand = baseCommand.endsWith(":");
|
|
661
|
+
const rest = lines.slice(1).join(" ");
|
|
662
|
+
const allOptions = _Signature.parseOptions(rest);
|
|
663
|
+
if (isNamespaceCommand) {
|
|
664
|
+
return {
|
|
665
|
+
baseCommand: baseCommand.slice(0, -1),
|
|
666
|
+
isNamespaceCommand,
|
|
667
|
+
subCommands: allOptions.filter((e) => !e.flags && !e.isHidden),
|
|
668
|
+
description,
|
|
669
|
+
commandClass,
|
|
670
|
+
options: allOptions.filter((e) => !!e.flags),
|
|
671
|
+
isHidden
|
|
672
|
+
};
|
|
673
|
+
} else {
|
|
674
|
+
return {
|
|
675
|
+
baseCommand,
|
|
676
|
+
isNamespaceCommand,
|
|
677
|
+
options: allOptions,
|
|
678
|
+
description,
|
|
679
|
+
commandClass,
|
|
680
|
+
isHidden
|
|
681
|
+
};
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
};
|
|
685
|
+
var Musket = class _Musket {
|
|
686
|
+
static {
|
|
687
|
+
__name(this, "Musket");
|
|
688
|
+
}
|
|
689
|
+
app;
|
|
690
|
+
kernel;
|
|
691
|
+
output = Utils.output();
|
|
692
|
+
commands = [];
|
|
693
|
+
constructor(app, kernel) {
|
|
694
|
+
this.app = app;
|
|
695
|
+
this.kernel = kernel;
|
|
696
|
+
}
|
|
697
|
+
async build() {
|
|
698
|
+
this.loadBaseCommands();
|
|
699
|
+
await this.loadDiscoveredCommands();
|
|
700
|
+
return this.initialize();
|
|
701
|
+
}
|
|
702
|
+
loadBaseCommands() {
|
|
703
|
+
const commands = [
|
|
704
|
+
new ServeCommand(this.app, this.kernel),
|
|
705
|
+
new MakeCommand(this.app, this.kernel),
|
|
706
|
+
new MigrateCommand(this.app, this.kernel)
|
|
707
|
+
];
|
|
708
|
+
commands.forEach((e) => this.addCommand(e));
|
|
709
|
+
}
|
|
710
|
+
async loadDiscoveredCommands() {
|
|
711
|
+
const commands = [];
|
|
712
|
+
commands.forEach((e) => this.addCommand(e));
|
|
713
|
+
}
|
|
714
|
+
addCommand(command) {
|
|
715
|
+
this.commands.push(Signature.parseSignature(command.getSignature(), command));
|
|
716
|
+
}
|
|
717
|
+
initialize() {
|
|
718
|
+
const cliVersion = [
|
|
719
|
+
"H3ravel Version:",
|
|
720
|
+
chalk__default.default.green(this.kernel.consolePackage.version)
|
|
721
|
+
].join(" ");
|
|
722
|
+
const localVersion = [
|
|
723
|
+
"Musket Version:",
|
|
724
|
+
chalk__default.default.green(this.kernel.modulePackage.version || "None")
|
|
725
|
+
].join(" ");
|
|
726
|
+
commander.program.name("musket").version(`${cliVersion}
|
|
727
|
+
${localVersion}`);
|
|
728
|
+
commander.program.command("init").description("Initialize H3ravel.").action(async () => {
|
|
729
|
+
this.output.success(`Initialized: H3ravel has been initialized!`);
|
|
730
|
+
});
|
|
731
|
+
for (let i = 0; i < this.commands.length; i++) {
|
|
732
|
+
const command = this.commands[i];
|
|
733
|
+
const instance = command.commandClass;
|
|
734
|
+
if (command.isNamespaceCommand && command.subCommands) {
|
|
735
|
+
const cmd = command.isHidden ? commander.program : commander.program.command(command.baseCommand).description(command.description ?? "").action(async () => {
|
|
736
|
+
instance.setInput(cmd.opts(), cmd.args, cmd.registeredArguments, command);
|
|
737
|
+
await instance.handle();
|
|
738
|
+
});
|
|
739
|
+
if ((command.options?.length ?? 0) > 0) {
|
|
740
|
+
command.options?.filter((v, i2, a) => a.findIndex((t) => t.name === v.name) === i2).forEach((opt) => {
|
|
741
|
+
this.makeOption(opt, cmd);
|
|
742
|
+
});
|
|
743
|
+
}
|
|
744
|
+
command.subCommands.filter((v, i2, a) => !v.shared && a.findIndex((t) => t.name === v.name) === i2).forEach((sub) => {
|
|
745
|
+
const cmd2 = commander.program.command(`${command.baseCommand}:${sub.name}`).description(sub.description || "").action(async () => {
|
|
746
|
+
instance.setInput(cmd2.opts(), cmd2.args, cmd2.registeredArguments, sub);
|
|
747
|
+
await instance.handle();
|
|
748
|
+
});
|
|
749
|
+
command.subCommands?.filter((e) => e.shared).forEach((opt) => {
|
|
750
|
+
this.makeOption(opt, cmd2, false, sub);
|
|
751
|
+
});
|
|
752
|
+
command.options?.filter((e) => e.shared).forEach((opt) => {
|
|
753
|
+
this.makeOption(opt, cmd2, false, sub);
|
|
754
|
+
});
|
|
755
|
+
if (sub.nestedOptions) {
|
|
756
|
+
sub.nestedOptions.filter((v, i2, a) => a.findIndex((t) => t.name === v.name) === i2).forEach((opt) => {
|
|
757
|
+
this.makeOption(opt, cmd2);
|
|
758
|
+
});
|
|
759
|
+
}
|
|
760
|
+
});
|
|
761
|
+
} else {
|
|
762
|
+
const cmd = commander.program.command(command.baseCommand).description(command.description ?? "");
|
|
763
|
+
command?.options?.filter((v, i2, a) => a.findIndex((t) => t.name === v.name) === i2).forEach((opt) => {
|
|
764
|
+
this.makeOption(opt, cmd, true);
|
|
765
|
+
});
|
|
766
|
+
cmd.action(async () => {
|
|
767
|
+
instance.setInput(cmd.opts(), cmd.args, cmd.registeredArguments, command);
|
|
768
|
+
await instance.handle();
|
|
769
|
+
});
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
return commander.program;
|
|
773
|
+
}
|
|
774
|
+
makeOption(opt, cmd, parse, parent) {
|
|
775
|
+
const description = opt.description?.replace(/\[(\w+)\]/g, (_, k) => parent?.[k] ?? `[${k}]`) ?? "";
|
|
776
|
+
const type = opt.name.replaceAll("-", "");
|
|
777
|
+
if (opt.isFlag) {
|
|
778
|
+
if (parse) {
|
|
779
|
+
const flags = opt.flags?.map((f) => f.length === 1 ? `-${f}` : `--${f}`).join(", ");
|
|
780
|
+
cmd.option(flags || "", description, String(opt.defaultValue) || void 0);
|
|
781
|
+
} else {
|
|
782
|
+
cmd.option(opt.flags?.join(", ") + (opt.required ? ` <${type}>` : ""), description, opt.defaultValue);
|
|
783
|
+
}
|
|
784
|
+
} else {
|
|
785
|
+
cmd.argument(opt.required ? `<${opt.name}>` : `[${opt.name}]`, description, opt.defaultValue);
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
static async parse(kernel) {
|
|
789
|
+
return (await new _Musket(kernel.app, kernel).build()).parseAsync();
|
|
790
|
+
}
|
|
791
|
+
};
|
|
792
|
+
var Kernel = class _Kernel {
|
|
793
|
+
static {
|
|
794
|
+
__name(this, "Kernel");
|
|
795
|
+
}
|
|
796
|
+
app;
|
|
797
|
+
cwd;
|
|
798
|
+
output = Utils.output();
|
|
799
|
+
basePath = "";
|
|
800
|
+
modulePath;
|
|
801
|
+
consolePath;
|
|
802
|
+
modulePackage;
|
|
803
|
+
consolePackage;
|
|
804
|
+
constructor(app, basePath) {
|
|
805
|
+
this.app = app;
|
|
806
|
+
}
|
|
807
|
+
static init(app) {
|
|
808
|
+
const instance = new _Kernel(app);
|
|
809
|
+
Promise.all([
|
|
810
|
+
instance.loadRequirements()
|
|
811
|
+
]).then(([e]) => e.run());
|
|
812
|
+
}
|
|
813
|
+
async run() {
|
|
814
|
+
await Musket.parse(this);
|
|
815
|
+
process.exit(0);
|
|
816
|
+
}
|
|
817
|
+
async ensureDirectoryExists(dir) {
|
|
818
|
+
await promises.mkdir(dir, {
|
|
819
|
+
recursive: true
|
|
820
|
+
});
|
|
821
|
+
}
|
|
822
|
+
async loadRequirements() {
|
|
823
|
+
this.cwd = nodepath__default.default.join(process.cwd(), this.basePath);
|
|
824
|
+
this.modulePath = Utils.findModulePkg("@h3ravel/core", this.cwd) ?? "";
|
|
825
|
+
this.consolePath = Utils.findModulePkg("@h3ravel/console", this.cwd) ?? "";
|
|
826
|
+
try {
|
|
827
|
+
this.modulePackage = await import(nodepath__default.default.join(this.modulePath, "package.json"));
|
|
828
|
+
} catch {
|
|
829
|
+
this.modulePackage = {
|
|
830
|
+
version: "N/A"
|
|
831
|
+
};
|
|
832
|
+
}
|
|
833
|
+
try {
|
|
834
|
+
this.consolePackage = await import(nodepath__default.default.join(this.consolePath, "package.json"));
|
|
835
|
+
} catch {
|
|
836
|
+
this.consolePackage = {
|
|
837
|
+
version: "N/A"
|
|
838
|
+
};
|
|
839
|
+
}
|
|
840
|
+
return this;
|
|
841
|
+
}
|
|
842
|
+
};
|
|
843
|
+
var ConsoleServiceProvider = class extends core.ServiceProvider {
|
|
844
|
+
static {
|
|
845
|
+
__name(this, "ConsoleServiceProvider");
|
|
846
|
+
}
|
|
847
|
+
static priority = 992;
|
|
848
|
+
register() {
|
|
849
|
+
Kernel.init(this.app);
|
|
850
|
+
}
|
|
851
|
+
};
|
|
852
|
+
|
|
853
|
+
// src/IO/providers.ts
|
|
854
|
+
var providers_default = [
|
|
855
|
+
config.ConfigServiceProvider,
|
|
856
|
+
database.DatabaseServiceProvider,
|
|
857
|
+
ConsoleServiceProvider
|
|
858
|
+
];
|
|
859
|
+
|
|
860
|
+
// src/IO/app.ts
|
|
861
|
+
var app_default = class {
|
|
862
|
+
static {
|
|
863
|
+
__name(this, "default");
|
|
864
|
+
}
|
|
865
|
+
async bootstrap() {
|
|
866
|
+
const app = new core.Application(process.cwd());
|
|
867
|
+
app.registerProviders(providers_default);
|
|
868
|
+
await app.registerConfiguredProviders();
|
|
869
|
+
await app.boot();
|
|
870
|
+
new events.EventEmitter().once("SIGINT", () => process.exit(0));
|
|
871
|
+
process.on("SIGINT", () => {
|
|
872
|
+
process.exit(0);
|
|
873
|
+
});
|
|
874
|
+
process.on("SIGTERM", () => {
|
|
875
|
+
process.exit(0);
|
|
876
|
+
});
|
|
877
|
+
}
|
|
878
|
+
};
|
|
879
|
+
|
|
880
|
+
// src/run.ts
|
|
881
|
+
new app_default().bootstrap();
|
|
882
|
+
new events.EventEmitter().once("SIGINT", () => process.exit(0));
|