@nocobase/server 0.18.0-alpha.9 → 0.19.0-alpha.2

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 (40) hide show
  1. package/lib/app-command.d.ts +4 -0
  2. package/lib/app-command.js +14 -0
  3. package/lib/app-supervisor.js +1 -1
  4. package/lib/application.d.ts +29 -2
  5. package/lib/application.js +143 -41
  6. package/lib/commands/create-migration.d.ts +3 -0
  7. package/lib/commands/create-migration.js +67 -0
  8. package/lib/commands/db-clean.js +1 -1
  9. package/lib/commands/db-sync.js +1 -1
  10. package/lib/commands/destroy.js +1 -1
  11. package/lib/commands/index.js +3 -5
  12. package/lib/commands/install.js +4 -9
  13. package/lib/commands/migrator.js +1 -1
  14. package/lib/commands/pm.js +4 -4
  15. package/lib/commands/restart.js +5 -0
  16. package/lib/commands/start.js +19 -8
  17. package/lib/commands/stop.js +4 -0
  18. package/lib/commands/upgrade.js +3 -16
  19. package/lib/errors/handler.js +3 -0
  20. package/lib/gateway/index.d.ts +2 -2
  21. package/lib/gateway/index.js +9 -5
  22. package/lib/gateway/ipc-socket-client.js +2 -1
  23. package/lib/gateway/ipc-socket-server.js +11 -1
  24. package/lib/helpers/application-version.js +17 -25
  25. package/lib/migration.d.ts +5 -0
  26. package/lib/migration.js +6 -0
  27. package/lib/migrations/20230912193824-package-name-unique.d.ts +2 -0
  28. package/lib/migrations/20230912193824-package-name-unique.js +4 -28
  29. package/lib/migrations/20230912294620-update-pkg.d.ts +6 -0
  30. package/lib/migrations/20230912294620-update-pkg.js +55 -0
  31. package/lib/migrations/20240106082756-update-plugins.d.ts +6 -0
  32. package/lib/migrations/20240106082756-update-plugins.js +41 -0
  33. package/lib/plugin-manager/options/collection.js +2 -2
  34. package/lib/plugin-manager/plugin-manager-repository.d.ts +1 -2
  35. package/lib/plugin-manager/plugin-manager-repository.js +14 -26
  36. package/lib/plugin-manager/plugin-manager.d.ts +28 -1
  37. package/lib/plugin-manager/plugin-manager.js +168 -8
  38. package/lib/plugin.d.ts +10 -0
  39. package/lib/plugin.js +86 -13
  40. package/package.json +13 -13
@@ -1,7 +1,11 @@
1
1
  import { Command } from 'commander';
2
2
  export declare class AppCommand extends Command {
3
3
  private _handleByIPCServer;
4
+ _preload: boolean;
4
5
  ipc(): this;
6
+ auth(): this;
7
+ preload(): this;
8
+ hasCommand(name: string): boolean;
5
9
  isHandleByIPCServer(): boolean;
6
10
  createCommand(name?: string): AppCommand;
7
11
  parseHandleByIPCServer(argv: any, parseOptions?: any): Boolean;
@@ -24,10 +24,24 @@ module.exports = __toCommonJS(app_command_exports);
24
24
  var import_commander = require("commander");
25
25
  const _AppCommand = class _AppCommand extends import_commander.Command {
26
26
  _handleByIPCServer = false;
27
+ _preload = false;
27
28
  ipc() {
28
29
  this._handleByIPCServer = true;
29
30
  return this;
30
31
  }
32
+ auth() {
33
+ this["_authenticate"] = true;
34
+ return this;
35
+ }
36
+ preload() {
37
+ this["_authenticate"] = true;
38
+ this._preload = true;
39
+ return this;
40
+ }
41
+ hasCommand(name) {
42
+ const names = this.commands.map((c) => c.name());
43
+ return names.includes(name);
44
+ }
31
45
  isHandleByIPCServer() {
32
46
  return this._handleByIPCServer;
33
47
  }
@@ -213,7 +213,7 @@ const _AppSupervisor = class _AppSupervisor extends import_events.EventEmitter {
213
213
  });
214
214
  app.on("__started", async (_app, options) => {
215
215
  const { maintainingStatus, options: startOptions } = options;
216
- if (maintainingStatus && ["install", "upgrade", "pm.add", "pm.update", "pm.enable", "pm.disable", "pm.remove"].includes(
216
+ if (maintainingStatus && ["install", "upgrade", "pm.add", "pm.update", "pm.enable", "pm.disable", "pm.remove", "restore"].includes(
217
217
  maintainingStatus.command.name
218
218
  ) && !startOptions.recover) {
219
219
  this.setAppStatus(app.name, "running", {
@@ -5,8 +5,9 @@ import { ACL } from '@nocobase/acl';
5
5
  import { AuthManager, AuthManagerOptions } from '@nocobase/auth';
6
6
  import { Cache, CacheManager, CacheManagerOptions } from '@nocobase/cache';
7
7
  import Database, { CollectionOptions, IDatabaseOptions } from '@nocobase/database';
8
- import { SystemLogger, RequestLoggerOptions, LoggerOptions, SystemLoggerOptions } from '@nocobase/logger';
8
+ import { LoggerOptions, RequestLoggerOptions, SystemLogger, SystemLoggerOptions } from '@nocobase/logger';
9
9
  import { ResourceOptions, Resourcer } from '@nocobase/resourcer';
10
+ import { Telemetry, TelemetryOptions } from '@nocobase/telemetry';
10
11
  import { AsyncEmitter, ToposortOptions } from '@nocobase/utils';
11
12
  import { Command, CommandOptions, ParseOptions } from 'commander';
12
13
  import { IncomingMessage, Server, ServerResponse } from 'http';
@@ -20,7 +21,6 @@ import { ApplicationVersion } from './helpers/application-version';
20
21
  import { Locale } from './locale';
21
22
  import { Plugin } from './plugin';
22
23
  import { InstallOptions, PluginManager } from './plugin-manager';
23
- import { TelemetryOptions, Telemetry } from '@nocobase/telemetry';
24
24
  export type PluginType = string | typeof Plugin;
25
25
  export type PluginConfiguration = PluginType | [PluginType, any];
26
26
  export interface ResourcerOptions {
@@ -70,6 +70,8 @@ interface StartOptions {
70
70
  cliArgs?: any[];
71
71
  dbSync?: boolean;
72
72
  checkInstall?: boolean;
73
+ quickstart?: boolean;
74
+ reload?: boolean;
73
75
  recover?: boolean;
74
76
  }
75
77
  type MaintainingStatus = 'command_begin' | 'command_end' | 'command_running' | 'command_error';
@@ -143,6 +145,9 @@ export declare class Application<StateT = DefaultState, ContextT = DefaultContex
143
145
  setMaintaining(_maintainingCommandStatus: MaintainingCommandStatus): void;
144
146
  setMaintainingMessage(message: string): void;
145
147
  getVersion(): string;
148
+ /**
149
+ * @deprecated
150
+ */
146
151
  plugin<O = any>(pluginClass: any, options?: O): void;
147
152
  use<NewStateT = {}, NewContextT = {}>(middleware: Koa.Middleware<StateT & NewStateT, ContextT & NewContextT>, options?: ToposortOptions): this;
148
153
  callback(): (req: IncomingMessage, res: ServerResponse) => any;
@@ -151,13 +156,35 @@ export declare class Application<StateT = DefaultState, ContextT = DefaultContex
151
156
  actions(handlers: any, options?: ActionsOptions): void;
152
157
  command(name: string, desc?: string, opts?: CommandOptions): AppCommand;
153
158
  findCommand(name: string): Command;
159
+ preload(): Promise<void>;
160
+ reInit(): Promise<void>;
154
161
  load(options?: any): Promise<void>;
155
162
  reload(options?: any): Promise<void>;
163
+ /**
164
+ * @deprecated
165
+ */
156
166
  getPlugin<P extends Plugin>(name: string | typeof Plugin): P;
157
167
  parse(argv?: string[]): Promise<AppCommand>;
158
168
  authenticate(): Promise<void>;
159
169
  runCommand(command: string, ...args: any[]): Promise<AppCommand>;
160
170
  createCli(): AppCommand;
171
+ loadMigrations(options: any): Promise<{
172
+ beforeLoad: any[];
173
+ afterSync: any[];
174
+ afterLoad: any[];
175
+ }>;
176
+ loadCoreMigrations(): Promise<{
177
+ beforeLoad: {
178
+ up: () => Promise<void>;
179
+ };
180
+ afterSync: {
181
+ up: () => Promise<void>;
182
+ };
183
+ afterLoad: {
184
+ up: () => Promise<void>;
185
+ };
186
+ }>;
187
+ loadPluginCommands(): Promise<void>;
161
188
  runAsCLI(argv?: string[], options?: ParseOptions & {
162
189
  throwError?: boolean;
163
190
  reqId?: string;
@@ -36,12 +36,15 @@ var import_actions = require("@nocobase/actions");
36
36
  var import_auth = require("@nocobase/auth");
37
37
  var import_database = __toESM(require("@nocobase/database"));
38
38
  var import_logger = require("@nocobase/logger");
39
+ var import_telemetry = require("@nocobase/telemetry");
39
40
  var import_utils = require("@nocobase/utils");
40
- var import_chalk = __toESM(require("chalk"));
41
41
  var import_crypto = require("crypto");
42
+ var import_glob = __toESM(require("glob"));
42
43
  var import_koa = __toESM(require("koa"));
43
44
  var import_koa_compose = __toESM(require("koa-compose"));
44
45
  var import_lodash = __toESM(require("lodash"));
46
+ var import_path = require("path");
47
+ var import_semver = __toESM(require("semver"));
45
48
  var import_acl2 = require("./acl");
46
49
  var import_app_command = require("./app-command");
47
50
  var import_app_supervisor = require("./app-supervisor");
@@ -53,7 +56,6 @@ var import_helper = require("./helper");
53
56
  var import_application_version = require("./helpers/application-version");
54
57
  var import_locale = require("./locale");
55
58
  var import_plugin_manager = require("./plugin-manager");
56
- var import_telemetry = require("@nocobase/telemetry");
57
59
  var import_package = __toESM(require("../package.json"));
58
60
  const _Application = class _Application extends import_koa.default {
59
61
  constructor(options) {
@@ -180,6 +182,9 @@ const _Application = class _Application extends import_koa.default {
180
182
  getVersion() {
181
183
  return import_package.default.version;
182
184
  }
185
+ /**
186
+ * @deprecated
187
+ */
183
188
  plugin(pluginClass, options) {
184
189
  this.log.debug(`add plugin`, { method: "plugin", name: pluginClass.name });
185
190
  this.pm.addPreset(pluginClass, options);
@@ -213,6 +218,26 @@ const _Application = class _Application extends import_koa.default {
213
218
  findCommand(name) {
214
219
  return this.cli._findCommand(name);
215
220
  }
221
+ async preload() {
222
+ }
223
+ async reInit() {
224
+ if (!this._loaded) {
225
+ return;
226
+ }
227
+ this.log.info("app reinitializing");
228
+ if (this.cacheManager) {
229
+ await this.cacheManager.close();
230
+ }
231
+ if (this.telemetry.started) {
232
+ await this.telemetry.shutdown();
233
+ }
234
+ const oldDb = this._db;
235
+ this.init();
236
+ if (!oldDb.closed()) {
237
+ await oldDb.close();
238
+ }
239
+ this._loaded = false;
240
+ }
216
241
  async load(options) {
217
242
  var _a;
218
243
  if (this._loaded) {
@@ -238,14 +263,18 @@ const _Application = class _Application extends import_koa.default {
238
263
  await this.pm.initPlugins();
239
264
  this.setMaintainingMessage("start load");
240
265
  this.setMaintainingMessage("emit beforeLoad");
241
- await this.emitAsync("beforeLoad", this, options);
266
+ if ((options == null ? void 0 : options.hooks) !== false) {
267
+ await this.emitAsync("beforeLoad", this, options);
268
+ }
242
269
  this.telemetry.init();
243
270
  if ((_a = this.options.telemetry) == null ? void 0 : _a.enabled) {
244
271
  this.telemetry.start();
245
272
  }
246
273
  await this.pm.load(options);
247
274
  this.setMaintainingMessage("emit afterLoad");
248
- await this.emitAsync("afterLoad", this, options);
275
+ if ((options == null ? void 0 : options.hooks) !== false) {
276
+ await this.emitAsync("afterLoad", this, options);
277
+ }
249
278
  this._loaded = true;
250
279
  }
251
280
  async reload(options) {
@@ -261,6 +290,9 @@ const _Application = class _Application extends import_koa.default {
261
290
  await this.emitAsync("afterReload", this, options);
262
291
  this.log.debug(`finish reload`, { method: "reload" });
263
292
  }
293
+ /**
294
+ * @deprecated
295
+ */
264
296
  getPlugin(name) {
265
297
  return this.pm.get(name);
266
298
  }
@@ -293,8 +325,12 @@ const _Application = class _Application extends import_koa.default {
293
325
  status: "command_running",
294
326
  command: this.activatedCommand
295
327
  });
296
- await this.authenticate();
297
- await this.load();
328
+ if (actionCommand["_authenticate"]) {
329
+ await this.authenticate();
330
+ }
331
+ if (actionCommand["_preload"]) {
332
+ await this.load();
333
+ }
298
334
  }).hook("postAction", async (_, actionCommand) => {
299
335
  var _a;
300
336
  if (((_a = this._maintainingStatusBeforeCommand) == null ? void 0 : _a.error) && this._started) {
@@ -306,6 +342,64 @@ const _Application = class _Application extends import_koa.default {
306
342
  });
307
343
  return command;
308
344
  }
345
+ async loadMigrations(options) {
346
+ const { directory, context, namespace } = options;
347
+ const migrations = {
348
+ beforeLoad: [],
349
+ afterSync: [],
350
+ afterLoad: []
351
+ };
352
+ const extensions = ["js", "ts"];
353
+ const patten = `${directory}/*.{${extensions.join(",")}}`;
354
+ const files = import_glob.default.sync(patten, {
355
+ ignore: ["**/*.d.ts"]
356
+ });
357
+ const appVersion = await this.version.get();
358
+ for (const file of files) {
359
+ let filename = (0, import_path.basename)(file);
360
+ filename = filename.substring(0, filename.lastIndexOf(".")) || filename;
361
+ const Migration = await (0, import_utils.importModule)(file);
362
+ const m = new Migration({ app: this, db: this.db, ...context });
363
+ if (!m.appVersion || import_semver.default.satisfies(appVersion, m.appVersion, { includePrerelease: true })) {
364
+ m.name = `${filename}/${namespace}`;
365
+ migrations[m.on || "afterLoad"].push(m);
366
+ }
367
+ }
368
+ return migrations;
369
+ }
370
+ async loadCoreMigrations() {
371
+ const migrations = await this.loadMigrations({
372
+ directory: (0, import_path.resolve)(__dirname, "migrations"),
373
+ namespace: "@nocobase/server"
374
+ });
375
+ return {
376
+ beforeLoad: {
377
+ up: async () => {
378
+ this.log.debug("run core migrations(beforeLoad)");
379
+ const migrator = this.db.createMigrator({ migrations: migrations.beforeLoad });
380
+ await migrator.up();
381
+ }
382
+ },
383
+ afterSync: {
384
+ up: async () => {
385
+ this.log.debug("run core migrations(afterSync)");
386
+ const migrator = this.db.createMigrator({ migrations: migrations.afterSync });
387
+ await migrator.up();
388
+ }
389
+ },
390
+ afterLoad: {
391
+ up: async () => {
392
+ this.log.debug("run core migrations(afterLoad)");
393
+ const migrator = this.db.createMigrator({ migrations: migrations.afterLoad });
394
+ await migrator.up();
395
+ }
396
+ }
397
+ };
398
+ }
399
+ async loadPluginCommands() {
400
+ this.log.debug("load plugin commands");
401
+ await this.pm.loadCommands();
402
+ }
309
403
  async runAsCLI(argv = process.argv, options) {
310
404
  if (this.activatedCommand) {
311
405
  return;
@@ -316,6 +410,10 @@ const _Application = class _Application extends import_koa.default {
316
410
  }
317
411
  this._maintainingStatusBeforeCommand = this._maintainingCommandStatus;
318
412
  try {
413
+ const commandName = (options == null ? void 0 : options.from) === "user" ? argv[0] : argv[2];
414
+ if (!this.cli.hasCommand(commandName)) {
415
+ await this.pm.loadCommands();
416
+ }
319
417
  const command = await this.cli.parseAsync(argv, options);
320
418
  this.setMaintaining({
321
419
  status: "command_end",
@@ -323,7 +421,6 @@ const _Application = class _Application extends import_koa.default {
323
421
  });
324
422
  return command;
325
423
  } catch (error) {
326
- console.log({ error });
327
424
  if (!this.activatedCommand) {
328
425
  this.activatedCommand = {
329
426
  name: "unknown"
@@ -393,6 +490,7 @@ const _Application = class _Application extends import_koa.default {
393
490
  if (!this._started) {
394
491
  return;
395
492
  }
493
+ this.log.info("restarting...");
396
494
  this._started = false;
397
495
  await this.emitAsync("beforeStop");
398
496
  await this.reload(options);
@@ -403,7 +501,7 @@ const _Application = class _Application extends import_koa.default {
403
501
  this.log.debug("stop app...", { method: "stop" });
404
502
  this.setMaintainingMessage("stopping app...");
405
503
  if (this.stopped) {
406
- this.log.warn(`Application ${this.name} already stopped`, { method: "stop" });
504
+ this.log.warn(`app is stopped`, { method: "stop" });
407
505
  return;
408
506
  }
409
507
  await this.emitAsync("beforeStop", this, options);
@@ -423,7 +521,7 @@ const _Application = class _Application extends import_koa.default {
423
521
  }
424
522
  await this.emitAsync("afterStop", this, options);
425
523
  this.stopped = true;
426
- this.log.info(`${this.name} is stopped`, { method: "stop" });
524
+ this.log.info(`app has stopped`, { method: "stop" });
427
525
  this._started = false;
428
526
  }
429
527
  async destroy(options = {}) {
@@ -439,29 +537,27 @@ const _Application = class _Application extends import_koa.default {
439
537
  return await this.db.collectionExistsInDb("applicationVersion") || await this.db.collectionExistsInDb("collections");
440
538
  }
441
539
  async install(options = {}) {
442
- var _a, _b;
443
- this.setMaintainingMessage("installing app...");
444
- this.log.debug("Database dialect: " + this.db.sequelize.getDialect(), { method: "install" });
445
- if ((options == null ? void 0 : options.clean) || ((_a = options == null ? void 0 : options.sync) == null ? void 0 : _a.force)) {
446
- this.log.debug("truncate database", { method: "install" });
540
+ var _a;
541
+ const reinstall = options.clean || options.force;
542
+ if (reinstall) {
447
543
  await this.db.clean({ drop: true });
448
- this.log.debug("app reloading", { method: "install" });
449
- await this.reload();
450
- } else if (await this.isInstalled()) {
451
- this.log.warn("app is installed", { method: "install" });
544
+ }
545
+ if (await this.isInstalled()) {
546
+ this.log.warn("app is installed");
452
547
  return;
453
548
  }
549
+ await this.reInit();
550
+ await this.db.sync();
551
+ await this.load({ hooks: false });
454
552
  this.log.debug("emit beforeInstall", { method: "install" });
455
553
  this.setMaintainingMessage("call beforeInstall hook...");
456
554
  await this.emitAsync("beforeInstall", this, options);
457
- this.log.debug("start install plugins", { method: "install" });
458
- await this.pm.install(options);
459
- this.log.debug("update version", { method: "install" });
555
+ await this.pm.install();
460
556
  await this.version.update();
461
557
  this.log.debug("emit afterInstall", { method: "install" });
462
558
  this.setMaintainingMessage("call afterInstall hook...");
463
559
  await this.emitAsync("afterInstall", this, options);
464
- if ((_b = this._maintainingStatusBeforeCommand) == null ? void 0 : _b.error) {
560
+ if ((_a = this._maintainingStatusBeforeCommand) == null ? void 0 : _a.error) {
465
561
  return;
466
562
  }
467
563
  if (this._started) {
@@ -469,27 +565,33 @@ const _Application = class _Application extends import_koa.default {
469
565
  }
470
566
  }
471
567
  async upgrade(options = {}) {
472
- await this.emitAsync("beforeUpgrade", this, options);
473
- const force = false;
474
- await (0, import_utils.measureExecutionTime)(async () => {
475
- await this.db.migrator.up();
476
- }, "Migrator");
477
- await (0, import_utils.measureExecutionTime)(async () => {
478
- await this.db.sync({
479
- force,
480
- alter: {
481
- drop: force
482
- }
483
- });
484
- }, "Sync");
568
+ this.log.info("upgrading...");
569
+ await this.reInit();
570
+ const migrator1 = await this.loadCoreMigrations();
571
+ await migrator1.beforeLoad.up();
572
+ await this.db.sync();
573
+ await migrator1.afterSync.up();
574
+ await this.pm.initPresetPlugins();
575
+ const migrator2 = await this.pm.loadPresetMigrations();
576
+ await migrator2.beforeLoad.up();
577
+ await this.pm.load();
578
+ await this.db.sync();
579
+ await migrator2.afterSync.up();
580
+ await this.pm.upgrade();
581
+ await this.pm.initOtherPlugins();
582
+ const migrator3 = await this.pm.loadOtherMigrations();
583
+ await migrator3.beforeLoad.up();
584
+ await this.load();
585
+ await this.db.sync();
586
+ await migrator3.afterSync.up();
587
+ await this.pm.upgrade();
588
+ await migrator1.afterLoad.up();
589
+ await migrator2.afterLoad.up();
590
+ await migrator3.afterLoad.up();
591
+ await this.pm.repository.updateVersions();
485
592
  await this.version.update();
486
593
  await this.emitAsync("afterUpgrade", this, options);
487
- this.log.debug(import_chalk.default.green(`\u2728 NocoBase has been upgraded to v${this.getVersion()}`));
488
- if (this._started) {
489
- await (0, import_utils.measureExecutionTime)(async () => {
490
- await this.restart();
491
- }, "Restart");
492
- }
594
+ await this.restart();
493
595
  }
494
596
  toJSON() {
495
597
  return {
@@ -0,0 +1,3 @@
1
+ import Application from '../application';
2
+ declare const _default: (app: Application) => void;
3
+ export default _default;
@@ -0,0 +1,67 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var create_migration_exports = {};
30
+ __export(create_migration_exports, {
31
+ default: () => create_migration_default
32
+ });
33
+ module.exports = __toCommonJS(create_migration_exports);
34
+ var import_dayjs = __toESM(require("dayjs"));
35
+ var import_fs = __toESM(require("fs"));
36
+ var import_path = require("path");
37
+ var create_migration_default = /* @__PURE__ */ __name((app) => {
38
+ app.command("create-migration").argument("<name>").option("--pkg <pkg>").option("--on [on]").action(async (name, options) => {
39
+ const pkg = options.pkg;
40
+ const dir = await import_fs.default.promises.realpath((0, import_path.resolve)(process.env.NODE_MODULES_PATH, pkg));
41
+ const filename = (0, import_path.resolve)(
42
+ dir,
43
+ pkg === "@nocobase/server" ? "src" : "src/server",
44
+ "migrations",
45
+ `${(0, import_dayjs.default)().format("YYYYMMDDHHmmss")}-${name}.ts`
46
+ );
47
+ const version = app.getVersion();
48
+ const keys = version.split(".");
49
+ keys.push(1 * keys.pop() + 1);
50
+ const nextVersion = keys.join(".");
51
+ const from = pkg === "@nocobase/server" ? `../migration` : "@nocobase/server";
52
+ const data = `import { Migration } from '${from}';
53
+
54
+ export default class extends Migration {
55
+ on = '${options.on || "afterLoad"}'; // 'beforeLoad' or 'afterLoad'
56
+ appVersion = '<${nextVersion}';
57
+
58
+ async up() {
59
+ // coding
60
+ }
61
+ }
62
+ `;
63
+ await import_fs.default.promises.mkdir((0, import_path.dirname)(filename), { recursive: true });
64
+ await import_fs.default.promises.writeFile(filename, data, "utf8");
65
+ app.log.info(`migration file in ${filename}`);
66
+ });
67
+ }, "default");
@@ -22,7 +22,7 @@ __export(db_clean_exports, {
22
22
  });
23
23
  module.exports = __toCommonJS(db_clean_exports);
24
24
  var db_clean_default = /* @__PURE__ */ __name((app) => {
25
- app.command("db:clean").option("-y, --yes").action(async (opts) => {
25
+ app.command("db:clean").auth().option("-y, --yes").action(async (opts) => {
26
26
  console.log("Clearing database");
27
27
  await app.db.clean({
28
28
  drop: opts.yes
@@ -22,7 +22,7 @@ __export(db_sync_exports, {
22
22
  });
23
23
  module.exports = __toCommonJS(db_sync_exports);
24
24
  var db_sync_default = /* @__PURE__ */ __name((app) => {
25
- app.command("db:sync").action(async (...cliArgs) => {
25
+ app.command("db:sync").auth().action(async (...cliArgs) => {
26
26
  const [opts] = cliArgs;
27
27
  console.log("db sync...");
28
28
  const force = false;
@@ -22,7 +22,7 @@ __export(destroy_exports, {
22
22
  });
23
23
  module.exports = __toCommonJS(destroy_exports);
24
24
  var destroy_default = /* @__PURE__ */ __name((app) => {
25
- app.command("destroy").action(async (...cliArgs) => {
25
+ app.command("destroy").preload().action(async (...cliArgs) => {
26
26
  await app.destroy({
27
27
  cliArgs
28
28
  });
@@ -31,31 +31,29 @@ __export(commands_exports, {
31
31
  registerCli: () => registerCli
32
32
  });
33
33
  module.exports = __toCommonJS(commands_exports);
34
- var import_console = __toESM(require("./console"));
34
+ var import_create_migration = __toESM(require("./create-migration"));
35
35
  var import_db_auth = __toESM(require("./db-auth"));
36
36
  var import_db_clean = __toESM(require("./db-clean"));
37
37
  var import_db_sync = __toESM(require("./db-sync"));
38
38
  var import_destroy = __toESM(require("./destroy"));
39
39
  var import_install = __toESM(require("./install"));
40
- var import_migrator = __toESM(require("./migrator"));
41
40
  var import_pm = __toESM(require("./pm"));
42
41
  var import_restart = __toESM(require("./restart"));
43
42
  var import_start = __toESM(require("./start"));
44
43
  var import_stop = __toESM(require("./stop"));
45
44
  var import_upgrade = __toESM(require("./upgrade"));
46
45
  function registerCli(app) {
47
- (0, import_console.default)(app);
48
46
  (0, import_db_auth.default)(app);
47
+ (0, import_create_migration.default)(app);
49
48
  (0, import_db_clean.default)(app);
50
49
  (0, import_db_sync.default)(app);
51
50
  (0, import_install.default)(app);
52
- (0, import_migrator.default)(app);
53
- (0, import_start.default)(app);
54
51
  (0, import_upgrade.default)(app);
55
52
  (0, import_pm.default)(app);
56
53
  (0, import_restart.default)(app);
57
54
  (0, import_stop.default)(app);
58
55
  (0, import_destroy.default)(app);
56
+ (0, import_start.default)(app);
59
57
  app.command("build").argument("[packages...]");
60
58
  app.command("clean");
61
59
  app.command("dev").usage("[options]").option("-p, --port [port]").option("--client").option("--server");
@@ -22,14 +22,9 @@ __export(install_exports, {
22
22
  });
23
23
  module.exports = __toCommonJS(install_exports);
24
24
  var install_default = /* @__PURE__ */ __name((app) => {
25
- app.command("install").ipc().option("-f, --force").option("-c, --clean").action(async (...cliArgs) => {
26
- const [opts] = cliArgs;
27
- await app.install({
28
- cliArgs,
29
- clean: opts.clean,
30
- sync: {
31
- force: opts.force
32
- }
33
- });
25
+ app.command("install").ipc().auth().option("-f, --force").option("-c, --clean").action(async (options) => {
26
+ await app.install(options);
27
+ const reinstall = options.clean || options.force;
28
+ app.log.info(`app ${reinstall ? "reinstalled" : "installed"} successfully [v${app.getVersion()}]`);
34
29
  });
35
30
  }, "default");
@@ -22,7 +22,7 @@ __export(migrator_exports, {
22
22
  });
23
23
  module.exports = __toCommonJS(migrator_exports);
24
24
  var migrator_default = /* @__PURE__ */ __name((app) => {
25
- app.command("migrator").action(async (opts) => {
25
+ app.command("migrator").preload().action(async (opts) => {
26
26
  console.log("migrating...");
27
27
  await app.emitAsync("cli.beforeMigrator", opts);
28
28
  await app.db.migrator.runAsCLI(process.argv.slice(3));
@@ -38,7 +38,7 @@ var pm_default = /* @__PURE__ */ __name((app) => {
38
38
  pm.command("create").ipc().arguments("plugin").action(async (plugin) => {
39
39
  await app.pm.create(plugin);
40
40
  });
41
- pm.command("add").ipc().argument("<pkg>").option("--registry [registry]").option("--auth-token [authToken]").option("--version [version]").action(async (name, options, cli) => {
41
+ pm.command("add").ipc().preload().argument("<pkg>").option("--registry [registry]").option("--auth-token [authToken]").option("--version [version]").action(async (name, options, cli) => {
42
42
  try {
43
43
  await app.pm.addViaCLI(name, import_lodash.default.cloneDeep(options));
44
44
  } catch (error) {
@@ -55,21 +55,21 @@ var pm_default = /* @__PURE__ */ __name((app) => {
55
55
  throw new import_plugin_command_error.PluginCommandError(`Failed to update plugin: ${error.message}`);
56
56
  }
57
57
  });
58
- pm.command("enable").ipc().arguments("<plugins...>").action(async (plugins) => {
58
+ pm.command("enable").ipc().preload().arguments("<plugins...>").action(async (plugins) => {
59
59
  try {
60
60
  await app.pm.enable(plugins);
61
61
  } catch (error) {
62
62
  throw new import_plugin_command_error.PluginCommandError(`Failed to enable plugin: ${error.message}`);
63
63
  }
64
64
  });
65
- pm.command("disable").ipc().arguments("<plugins...>").action(async (plugins) => {
65
+ pm.command("disable").ipc().preload().arguments("<plugins...>").action(async (plugins) => {
66
66
  try {
67
67
  await app.pm.disable(plugins);
68
68
  } catch (error) {
69
69
  throw new import_plugin_command_error.PluginCommandError(`Failed to disable plugin: ${error.message}`);
70
70
  }
71
71
  });
72
- pm.command("remove").ipc().arguments("<plugins...>").action(async (plugins) => {
72
+ pm.command("remove").ipc().preload().arguments("<plugins...>").action(async (plugins) => {
73
73
  await app.pm.remove(plugins);
74
74
  });
75
75
  }, "default");
@@ -23,8 +23,13 @@ __export(restart_exports, {
23
23
  module.exports = __toCommonJS(restart_exports);
24
24
  var restart_default = /* @__PURE__ */ __name((app) => {
25
25
  app.command("restart").ipc().action(async (...cliArgs) => {
26
+ if (!await app.isStarted()) {
27
+ app.log.info("app has not started");
28
+ return;
29
+ }
26
30
  await app.restart({
27
31
  cliArgs
28
32
  });
33
+ app.log.info("app has been restarted");
29
34
  });
30
35
  }, "default");