@nocobase/server 1.0.0-alpha.8 → 1.0.1-alpha.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.
@@ -25,6 +25,7 @@ export declare class AppSupervisor extends EventEmitter implements AsyncEmitter
25
25
  apps: {
26
26
  [appName: string]: Application;
27
27
  };
28
+ lastSeenAt: Map<string, number>;
28
29
  appErrors: {
29
30
  [appName: string]: Error;
30
31
  };
@@ -57,6 +58,7 @@ export declare class AppSupervisor extends EventEmitter implements AsyncEmitter
57
58
  getAppStatus(appName: string, defaultStatus?: AppStatus): AppStatus | null;
58
59
  bootMainApp(options: ApplicationOptions): Application<import("./application").DefaultState, import("./application").DefaultContext>;
59
60
  hasApp(appName: string): boolean;
61
+ touchApp(appName: string): void;
60
62
  addApp(app: Application): Application<import("./application").DefaultState, import("./application").DefaultContext>;
61
63
  getAppsNames(): Promise<string[]>;
62
64
  removeApp(appName: string): Promise<void>;
@@ -54,6 +54,7 @@ const _AppSupervisor = class _AppSupervisor extends import_events.EventEmitter {
54
54
  runningMode = "multiple";
55
55
  singleAppName = null;
56
56
  apps = {};
57
+ lastSeenAt = /* @__PURE__ */ new Map();
57
58
  appErrors = {};
58
59
  appStatus = {};
59
60
  lastMaintainingMessage = {};
@@ -156,6 +157,12 @@ const _AppSupervisor = class _AppSupervisor extends import_events.EventEmitter {
156
157
  hasApp(appName) {
157
158
  return !!this.apps[appName];
158
159
  }
160
+ touchApp(appName) {
161
+ if (!this.hasApp(appName)) {
162
+ return;
163
+ }
164
+ this.lastSeenAt.set(appName, Math.floor(Date.now() / 1e3));
165
+ }
159
166
  // add app into supervisor
160
167
  addApp(app) {
161
168
  if (this.apps[app.name]) {
@@ -203,6 +210,7 @@ const _AppSupervisor = class _AppSupervisor extends import_events.EventEmitter {
203
210
  delete this.appErrors[app.name];
204
211
  delete this.lastMaintainingMessage[app.name];
205
212
  delete this.statusBeforeCommanding[app.name];
213
+ this.lastSeenAt.delete(app.name);
206
214
  });
207
215
  app.on("maintainingMessageChanged", ({ message, maintainingStatus }) => {
208
216
  if (this.lastMaintainingMessage[app.name] === message) {
@@ -11,7 +11,7 @@
11
11
  import { AuthManager, AuthManagerOptions } from '@nocobase/auth';
12
12
  import { Cache, CacheManager, CacheManagerOptions } from '@nocobase/cache';
13
13
  import Database, { CollectionOptions, IDatabaseOptions } from '@nocobase/database';
14
- import { LoggerOptions, RequestLoggerOptions, SystemLogger, SystemLoggerOptions } from '@nocobase/logger';
14
+ import { Logger, LoggerOptions, RequestLoggerOptions, SystemLogger, SystemLoggerOptions } from '@nocobase/logger';
15
15
  import { ResourceOptions, Resourcer } from '@nocobase/resourcer';
16
16
  import { Telemetry, TelemetryOptions } from '@nocobase/telemetry';
17
17
  import { AsyncEmitter, ToposortOptions } from '@nocobase/utils';
@@ -155,9 +155,15 @@ export declare class Application<StateT = DefaultState, ContextT = DefaultContex
155
155
  private _maintainingCommandStatus;
156
156
  private _maintainingStatusBeforeCommand;
157
157
  private _actionCommand;
158
- constructor(options: ApplicationOptions);
158
+ /**
159
+ * @internal
160
+ */
161
+ requestLogger: Logger;
162
+ private sqlLogger;
159
163
  protected _logger: SystemLogger;
164
+ constructor(options: ApplicationOptions);
160
165
  get logger(): SystemLogger;
166
+ get log(): SystemLogger;
161
167
  protected _loaded: boolean;
162
168
  /**
163
169
  * @internal
@@ -208,7 +214,6 @@ export declare class Application<StateT = DefaultState, ContextT = DefaultContex
208
214
  get telemetry(): Telemetry;
209
215
  protected _version: ApplicationVersion;
210
216
  get version(): ApplicationVersion;
211
- get log(): SystemLogger;
212
217
  get name(): string;
213
218
  protected _dataSourceManager: DataSourceManager;
214
219
  get dataSourceManager(): DataSourceManager;
@@ -329,8 +334,10 @@ export declare class Application<StateT = DefaultState, ContextT = DefaultContex
329
334
  * @internal
330
335
  */
331
336
  reInitEvents(): void;
332
- createLogger(options: LoggerOptions): import("winston").Logger;
337
+ createLogger(options: LoggerOptions): Logger;
333
338
  protected createCLI(): AppCommand;
339
+ protected initLogger(options: AppLoggerOptions): void;
340
+ protected closeLogger(): void;
334
341
  protected init(): void;
335
342
  protected createMainDataSource(options: ApplicationOptions): void;
336
343
  protected createDatabase(options: ApplicationOptions): Database;
@@ -69,6 +69,9 @@ var import_data_source_manager = require("@nocobase/data-source-manager");
69
69
  var import_package = __toESM(require("../package.json"));
70
70
  var import_main_data_source = require("./main-data-source");
71
71
  var import_validate_filter_params = __toESM(require("./middlewares/validate-filter-params"));
72
+ var import_path2 = __toESM(require("path"));
73
+ var import_middlewares = require("./middlewares");
74
+ var import_data_template = require("./middlewares/data-template");
72
75
  const _Application = class _Application extends import_koa.default {
73
76
  constructor(options) {
74
77
  super();
@@ -110,10 +113,18 @@ const _Application = class _Application extends import_koa.default {
110
113
  _maintainingCommandStatus;
111
114
  _maintainingStatusBeforeCommand;
112
115
  _actionCommand;
116
+ /**
117
+ * @internal
118
+ */
119
+ requestLogger;
120
+ sqlLogger;
113
121
  _logger;
114
122
  get logger() {
115
123
  return this._logger;
116
124
  }
125
+ get log() {
126
+ return this._logger;
127
+ }
117
128
  _loaded;
118
129
  /**
119
130
  * @internal
@@ -206,9 +217,6 @@ const _Application = class _Application extends import_koa.default {
206
217
  get version() {
207
218
  return this._version;
208
219
  }
209
- get log() {
210
- return this._logger;
211
- }
212
220
  get name() {
213
221
  return this.options.name || "main";
214
222
  }
@@ -322,6 +330,7 @@ const _Application = class _Application extends import_koa.default {
322
330
  if (this.telemetry.started) {
323
331
  await this.telemetry.shutdown();
324
332
  }
333
+ this.closeLogger();
325
334
  const oldDb = this.db;
326
335
  this.init();
327
336
  if (!oldDb.closed()) {
@@ -350,8 +359,10 @@ const _Application = class _Application extends import_koa.default {
350
359
  }
351
360
  }
352
361
  this._cacheManager = await (0, import_cache2.createCacheManager)(this, this.options.cacheManager);
362
+ this.log.debug("init plugins");
353
363
  this.setMaintainingMessage("init plugins");
354
364
  await this.pm.initPlugins();
365
+ this.log.debug("loading app...");
355
366
  this.setMaintainingMessage("start load");
356
367
  this.setMaintainingMessage("emit beforeLoad");
357
368
  if ((options == null ? void 0 : options.hooks) !== false) {
@@ -531,6 +542,7 @@ const _Application = class _Application extends import_koa.default {
531
542
  `Application ${this.name} is not installed, Please run 'yarn nocobase install' command first`
532
543
  );
533
544
  }
545
+ this.log.debug(`starting app...`);
534
546
  this.setMaintainingMessage("starting app...");
535
547
  if (this.db.closed()) {
536
548
  await this.db.reconnect();
@@ -620,6 +632,7 @@ const _Application = class _Application extends import_koa.default {
620
632
  this.log.debug("emit afterDestroy", { method: "destory" });
621
633
  await this.emitAsync("afterDestroy", this, options);
622
634
  this.log.debug("finish destroy app", { method: "destory" });
635
+ this.closeLogger();
623
636
  }
624
637
  async isInstalled() {
625
638
  return await this.db.collectionExistsInDb("applicationVersion") || await this.db.collectionExistsInDb("collections");
@@ -702,7 +715,7 @@ const _Application = class _Application extends import_koa.default {
702
715
  const { dirname } = options;
703
716
  return (0, import_logger.createLogger)({
704
717
  ...options,
705
- dirname: (0, import_logger.getLoggerFilePath)(this.name || "main", dirname || "")
718
+ dirname: (0, import_logger.getLoggerFilePath)(import_path2.default.join(this.name || "main", dirname || ""))
706
719
  });
707
720
  }
708
721
  createCLI() {
@@ -736,19 +749,38 @@ const _Application = class _Application extends import_koa.default {
736
749
  });
737
750
  return command;
738
751
  }
739
- init() {
740
- var _a;
741
- const options = this.options;
752
+ initLogger(options) {
742
753
  this._logger = (0, import_logger.createSystemLogger)({
743
754
  dirname: (0, import_logger.getLoggerFilePath)(this.name),
744
755
  filename: "system",
745
756
  seperateError: true,
746
- ...((_a = options.logger) == null ? void 0 : _a.system) || {}
757
+ ...(options == null ? void 0 : options.system) || {}
747
758
  }).child({
748
759
  reqId: this.context.reqId,
749
760
  app: this.name,
750
761
  module: "application"
762
+ // Due to the use of custom log levels,
763
+ // we have to use any type here until Winston updates the type definitions.
751
764
  });
765
+ this.requestLogger = (0, import_logger.createLogger)({
766
+ dirname: (0, import_logger.getLoggerFilePath)(this.name),
767
+ filename: "request",
768
+ ...(options == null ? void 0 : options.request) || {}
769
+ });
770
+ this.sqlLogger = this.createLogger({
771
+ filename: "sql",
772
+ level: "debug"
773
+ });
774
+ }
775
+ closeLogger() {
776
+ var _a, _b, _c;
777
+ (_a = this.log) == null ? void 0 : _a.close();
778
+ (_b = this.requestLogger) == null ? void 0 : _b.close();
779
+ (_c = this.sqlLogger) == null ? void 0 : _c.close();
780
+ }
781
+ init() {
782
+ const options = this.options;
783
+ this.initLogger(options.logger);
752
784
  this.reInitEvents();
753
785
  this.middleware = new import_utils.Toposort();
754
786
  this.plugins = /* @__PURE__ */ new Map();
@@ -785,6 +817,11 @@ const _Application = class _Application extends import_koa.default {
785
817
  });
786
818
  this._dataSourceManager.use(this._authManager.middleware(), { tag: "auth" });
787
819
  this._dataSourceManager.use(import_validate_filter_params.default, { tag: "validate-filter-params", before: ["auth"] });
820
+ this._dataSourceManager.use(import_middlewares.parseVariables, {
821
+ group: "parseVariables",
822
+ after: "acl"
823
+ });
824
+ this._dataSourceManager.use(import_data_template.dataTemplate, { group: "dataTemplate", after: "acl" });
788
825
  this._locales = new import_locale.Locale((0, import_helper.createAppProxy)(this));
789
826
  if (options.perfHooks) {
790
827
  (0, import_helper.enablePerfHooks)(this);
@@ -808,10 +845,6 @@ const _Application = class _Application extends import_koa.default {
808
845
  this.dataSourceManager.dataSources.set("main", mainDataSourceInstance);
809
846
  }
810
847
  createDatabase(options) {
811
- const sqlLogger = this.createLogger({
812
- filename: "sql",
813
- level: "debug"
814
- });
815
848
  const logging = /* @__PURE__ */ __name((msg) => {
816
849
  if (typeof msg === "string") {
817
850
  msg = msg.replace(/[\r\n]/gm, "").replace(/\s+/g, " ");
@@ -819,7 +852,7 @@ const _Application = class _Application extends import_koa.default {
819
852
  if (msg.includes("INSERT INTO")) {
820
853
  msg = msg.substring(0, 2e3) + "...";
821
854
  }
822
- sqlLogger.debug({ message: msg, app: this.name, reqId: this.context.reqId });
855
+ this.sqlLogger.debug({ message: msg, app: this.name, reqId: this.context.reqId });
823
856
  }, "logging");
824
857
  const dbOptions = options.database instanceof import_database.default ? options.database.options : options.database;
825
858
  const db = new import_database.default({
@@ -52,7 +52,10 @@ var start_default = /* @__PURE__ */ __name((app) => {
52
52
  const upgrading = await (0, import_utils.fsExists)(file);
53
53
  if (upgrading) {
54
54
  await app.upgrade();
55
- await import_fs.default.promises.rm(file);
55
+ try {
56
+ await import_fs.default.promises.rm(file);
57
+ } catch (error) {
58
+ }
56
59
  } else if (options.quickstart) {
57
60
  if (await app.isInstalled()) {
58
61
  await app.upgrade();
@@ -53,8 +53,15 @@ const errors = {
53
53
  APP_ERROR: {
54
54
  status: 503,
55
55
  message: ({ app }) => {
56
- var _a;
57
- return (_a = import_app_supervisor.AppSupervisor.getInstance().appErrors[app.name]) == null ? void 0 : _a.message;
56
+ const error = import_app_supervisor.AppSupervisor.getInstance().appErrors[app.name];
57
+ if (!error) {
58
+ return "";
59
+ }
60
+ let message = error.message;
61
+ if (error.cause) {
62
+ message = `${message}: ${error.cause.message}`;
63
+ }
64
+ return message;
58
65
  },
59
66
  code: ({ app }) => {
60
67
  const error = import_app_supervisor.AppSupervisor.getInstance().appErrors[app.name];
@@ -52,7 +52,7 @@ export declare class Gateway extends EventEmitter {
52
52
  destroy(): void;
53
53
  reset(): void;
54
54
  addAppSelectorMiddleware(middleware: AppSelectorMiddleware, options?: ToposortOptions): void;
55
- getLogger(appName: string, res: ServerResponse): import("winston").Logger;
55
+ getLogger(appName: string, res: ServerResponse): import("@nocobase/logger/lib/logger").Logger;
56
56
  responseError(res: ServerResponse, error: {
57
57
  status: number;
58
58
  maintaining: boolean;
@@ -240,6 +240,9 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
240
240
  res.end("ok");
241
241
  return;
242
242
  }
243
+ if (handleApp !== "main") {
244
+ import_app_supervisor.AppSupervisor.getInstance().touchApp(handleApp);
245
+ }
243
246
  app.callback()(req, res);
244
247
  }
245
248
  getAppSelectorMiddlewares() {
package/lib/helper.js CHANGED
@@ -80,7 +80,7 @@ function registerMiddlewares(app, options) {
80
80
  app.context.reqId = (0, import_crypto.randomUUID)();
81
81
  await next();
82
82
  });
83
- app.use((0, import_logger.requestLogger)(app.name, (_a = options.logger) == null ? void 0 : _a.request), { tag: "logger" });
83
+ app.use((0, import_logger.requestLogger)(app.name, app.requestLogger, (_a = options.logger) == null ? void 0 : _a.request), { tag: "logger" });
84
84
  app.use(
85
85
  (0, import_cors.default)({
86
86
  exposeHeaders: ["content-disposition"],
@@ -43,7 +43,7 @@ const _Locale = class _Locale {
43
43
  constructor(app) {
44
44
  this.app = app;
45
45
  this.app.on("afterLoad", async () => {
46
- this.app.log.debug("load locale resource", { submodule: "locale", method: "onAfterLoad" });
46
+ this.app.log.debug("loading locale resource...", { submodule: "locale", method: "onAfterLoad" });
47
47
  this.app.setMaintainingMessage("load locale resource");
48
48
  await this.load();
49
49
  this.app.log.debug("locale resource loaded", { submodule: "locale", method: "onAfterLoad" });
@@ -31,8 +31,6 @@ __export(main_data_source_exports, {
31
31
  });
32
32
  module.exports = __toCommonJS(main_data_source_exports);
33
33
  var import_data_source_manager = require("@nocobase/data-source-manager");
34
- var import_middlewares = require("./middlewares");
35
- var import_data_template = require("./middlewares/data-template");
36
34
  const _MainDataSource = class _MainDataSource extends import_data_source_manager.SequelizeDataSource {
37
35
  init(options = {}) {
38
36
  const { acl, resourceManager, database } = options;
@@ -49,11 +47,6 @@ const _MainDataSource = class _MainDataSource extends import_data_source_manager
49
47
  if (options.useACL !== false) {
50
48
  this.resourceManager.use(this.acl.middleware(), { group: "acl", after: "auth" });
51
49
  }
52
- this.resourceManager.use(import_middlewares.parseVariables, {
53
- group: "parseVariables",
54
- after: "acl"
55
- });
56
- this.resourceManager.use(import_data_template.dateTemplate, { group: "dateTemplate", after: "acl" });
57
50
  }
58
51
  };
59
52
  __name(_MainDataSource, "MainDataSource");
@@ -7,4 +7,4 @@
7
7
  * For more information, please refer to: https://www.nocobase.com/agreement.
8
8
  */
9
9
  import { Context } from '@nocobase/actions';
10
- export declare const dateTemplate: (ctx: Context, next: any) => Promise<void>;
10
+ export declare const dataTemplate: (ctx: Context, next: any) => Promise<void>;
@@ -27,10 +27,10 @@ var __copyProps = (to, from, except, desc) => {
27
27
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
28
  var data_template_exports = {};
29
29
  __export(data_template_exports, {
30
- dateTemplate: () => dateTemplate
30
+ dataTemplate: () => dataTemplate
31
31
  });
32
32
  module.exports = __toCommonJS(data_template_exports);
33
- const dateTemplate = /* @__PURE__ */ __name(async (ctx, next) => {
33
+ const dataTemplate = /* @__PURE__ */ __name(async (ctx, next) => {
34
34
  const { resourceName, actionName } = ctx.action;
35
35
  const { isTemplate, fields } = ctx.action.params;
36
36
  await next();
@@ -40,7 +40,7 @@ const dateTemplate = /* @__PURE__ */ __name(async (ctx, next) => {
40
40
  include: fields
41
41
  });
42
42
  }
43
- }, "dateTemplate");
43
+ }, "dataTemplate");
44
44
  const traverseHasMany = /* @__PURE__ */ __name((arr, { collection, exclude = [], include = [] }) => {
45
45
  if (!arr) {
46
46
  return arr;
@@ -142,5 +142,5 @@ const traverseJSON = /* @__PURE__ */ __name((data, options) => {
142
142
  }, "traverseJSON");
143
143
  // Annotate the CommonJS export names for ESM import in node:
144
144
  0 && (module.exports = {
145
- dateTemplate
145
+ dataTemplate
146
146
  });
@@ -338,7 +338,7 @@ const _PluginManager = class _PluginManager {
338
338
  } catch (error) {
339
339
  console.error(error);
340
340
  }
341
- this.app.log.debug(`add plugin [${options.name}]`, {
341
+ this.app.log.trace(`add plugin [${options.name}]`, {
342
342
  method: "add",
343
343
  submodule: "plugin-manager",
344
344
  name: options.name
@@ -379,7 +379,7 @@ const _PluginManager = class _PluginManager {
379
379
  * @internal
380
380
  */
381
381
  async loadCommands() {
382
- this.app.log.debug("load commands");
382
+ this.app.log.info("load commands");
383
383
  const items = await this.repository.find({
384
384
  filter: {
385
385
  enabled: true
@@ -388,21 +388,15 @@ const _PluginManager = class _PluginManager {
388
388
  const packageNames = items.map((item) => item.packageName);
389
389
  const source = [];
390
390
  for (const packageName of packageNames) {
391
- const file = require.resolve(packageName);
392
- const sourceDir = (0, import_path.basename)((0, import_path.dirname)(file)) === "src" ? "src" : "dist";
393
- const directory = (0, import_path.join)(
394
- packageName,
395
- sourceDir,
396
- "server/commands/*." + ((0, import_path.basename)((0, import_path.dirname)(file)) === "src" ? "ts" : "js")
397
- );
391
+ const dirname = await (0, import_utils2.getPluginBasePath)(packageName);
392
+ const directory = (0, import_path.join)(dirname, "server/commands/*." + ((0, import_path.basename)(dirname) === "src" ? "ts" : "js"));
398
393
  source.push(directory.replaceAll(import_path.sep, "/"));
399
394
  }
400
395
  for (const plugin of this.options.plugins || []) {
401
396
  if (typeof plugin === "string") {
402
- const packageName = await _PluginManager.getPackageName(plugin);
403
- const file = require.resolve(packageName);
404
- const sourceDir = (0, import_path.basename)((0, import_path.dirname)(file)) === "src" ? "src" : "lib";
405
- const directory = (0, import_path.join)(packageName, sourceDir, "server/commands/*." + (sourceDir === "src" ? "ts" : "js"));
397
+ const { packageName } = await _PluginManager.parseName(plugin);
398
+ const dirname = await (0, import_utils2.getPluginBasePath)(packageName);
399
+ const directory = (0, import_path.join)(dirname, "server/commands/*." + ((0, import_path.basename)(dirname) === "src" ? "ts" : "js"));
406
400
  source.push(directory.replaceAll(import_path.sep, "/"));
407
401
  }
408
402
  }
@@ -416,6 +410,7 @@ const _PluginManager = class _PluginManager {
416
410
  }
417
411
  }
418
412
  async load(options = {}) {
413
+ this.app.log.debug("loading plugins...");
419
414
  this.app.setMaintainingMessage("loading plugins...");
420
415
  const total = this.pluginInstances.size;
421
416
  let current = 0;
@@ -429,7 +424,7 @@ const _PluginManager = class _PluginManager {
429
424
  if (!plugin.enabled) {
430
425
  continue;
431
426
  }
432
- this.app.logger.debug(`before load plugin [${name}]`, { submodule: "plugin-manager", method: "load", name });
427
+ this.app.logger.trace(`before load plugin [${name}]`, { submodule: "plugin-manager", method: "load", name });
433
428
  await plugin.beforeLoad();
434
429
  }
435
430
  current = 0;
@@ -444,13 +439,14 @@ const _PluginManager = class _PluginManager {
444
439
  continue;
445
440
  }
446
441
  await this.app.emitAsync("beforeLoadPlugin", plugin, options);
447
- this.app.logger.debug(`load plugin [${name}] `, { submodule: "plugin-manager", method: "load", name });
442
+ this.app.logger.trace(`load plugin [${name}] `, { submodule: "plugin-manager", method: "load", name });
448
443
  await plugin.loadCollections();
449
444
  await plugin.load();
450
445
  plugin.state.loaded = true;
451
446
  await this.app.emitAsync("afterLoadPlugin", plugin, options);
452
447
  }
453
- this.app.setMaintainingMessage("loaded plugins");
448
+ this.app.log.debug("plugins loaded");
449
+ this.app.setMaintainingMessage("plugins loaded");
454
450
  }
455
451
  async install(options = {}) {
456
452
  this.app.setMaintainingMessage("install plugins...");
@@ -820,20 +816,18 @@ const _PluginManager = class _PluginManager {
820
816
  if (!await this.app.isStarted()) {
821
817
  this.app.log.debug("app upgrading");
822
818
  await this.app.runCommand("upgrade");
823
- await (0, import_execa.default)("yarn", ["nocobase", "refresh"], {
819
+ await (0, import_helper.tsxRerunning)();
820
+ await (0, import_execa.default)("yarn", ["nocobase", "pm2-restart"], {
824
821
  env: process.env
825
822
  });
826
823
  return;
827
824
  }
828
825
  const file = (0, import_path.resolve)(process.cwd(), "storage/app-upgrading");
829
826
  await import_fs.default.promises.writeFile(file, "", "utf-8");
830
- if (process.env.IS_DEV_CMD) {
831
- await (0, import_helper.tsxRerunning)();
832
- } else {
833
- await (0, import_execa.default)("yarn", ["nocobase", "pm2-restart"], {
834
- env: process.env
835
- });
836
- }
827
+ await (0, import_helper.tsxRerunning)();
828
+ await (0, import_execa.default)("yarn", ["nocobase", "pm2-restart"], {
829
+ env: process.env
830
+ });
837
831
  }, "upgrade");
838
832
  if (Array.isArray(nameOrPkg)) {
839
833
  for (const name of nameOrPkg) {
@@ -111,4 +111,5 @@ export declare function checkAndGetCompatible(packageName: string): Promise<{
111
111
  isCompatible: boolean;
112
112
  depsCompatible: DepCompatible[];
113
113
  }>;
114
+ export declare function getPluginBasePath(packageName: string): Promise<string>;
114
115
  export {};
@@ -57,6 +57,7 @@ __export(utils_exports, {
57
57
  getPackageJsonByLocalPath: () => getPackageJsonByLocalPath,
58
58
  getPackageNameFromString: () => getPackageNameFromString,
59
59
  getPackagesFromFiles: () => getPackagesFromFiles,
60
+ getPluginBasePath: () => getPluginBasePath,
60
61
  getPluginInfoByNpm: () => getPluginInfoByNpm,
61
62
  getPluginStoragePath: () => getPluginStoragePath,
62
63
  getServerPackages: () => getServerPackages,
@@ -512,6 +513,21 @@ async function checkAndGetCompatible(packageName) {
512
513
  };
513
514
  }
514
515
  __name(checkAndGetCompatible, "checkAndGetCompatible");
516
+ async function getPluginBasePath(packageName) {
517
+ if (!packageName) {
518
+ return;
519
+ }
520
+ const file = await import_fs_extra.default.realpath(await (0, import_utils.requireResolve)(packageName));
521
+ try {
522
+ const basePath = await import_fs_extra.default.realpath(import_path.default.resolve(process.env.NODE_MODULES_PATH, packageName, "src"));
523
+ if (file.startsWith(basePath)) {
524
+ return basePath;
525
+ }
526
+ } catch (error) {
527
+ }
528
+ return import_path.default.dirname(import_path.default.dirname(file));
529
+ }
530
+ __name(getPluginBasePath, "getPluginBasePath");
515
531
  // Annotate the CommonJS export names for ESM import in node:
516
532
  0 && (module.exports = {
517
533
  checkAndGetCompatible,
@@ -534,6 +550,7 @@ __name(checkAndGetCompatible, "checkAndGetCompatible");
534
550
  getPackageJsonByLocalPath,
535
551
  getPackageNameFromString,
536
552
  getPackagesFromFiles,
553
+ getPluginBasePath,
537
554
  getPluginInfoByNpm,
538
555
  getPluginStoragePath,
539
556
  getServerPackages,
package/lib/plugin.d.ts CHANGED
@@ -43,7 +43,7 @@ export declare abstract class Plugin<O = any> implements PluginInterface {
43
43
  */
44
44
  private _sourceDir;
45
45
  constructor(app: Application, options?: any);
46
- get log(): import("winston").Logger;
46
+ get log(): import("@nocobase/logger/lib/logger").Logger;
47
47
  get name(): string;
48
48
  get pm(): import("./plugin-manager").PluginManager;
49
49
  get db(): import("@nocobase/database").default;
@@ -53,7 +53,7 @@ export declare abstract class Plugin<O = any> implements PluginInterface {
53
53
  set installed(value: any);
54
54
  get isPreset(): any;
55
55
  getName(): any;
56
- createLogger(options: LoggerOptions): import("winston").Logger;
56
+ createLogger(options: LoggerOptions): import("@nocobase/logger/lib/logger").Logger;
57
57
  afterAdd(): void;
58
58
  beforeLoad(): void;
59
59
  load(): Promise<void>;
@@ -73,10 +73,6 @@ export declare abstract class Plugin<O = any> implements PluginInterface {
73
73
  * @internal
74
74
  */
75
75
  setOptions(options: any): void;
76
- /**
77
- * @internal
78
- */
79
- loadCommands(): Promise<void>;
80
76
  /**
81
77
  * @internal
82
78
  */
@@ -85,6 +81,7 @@ export declare abstract class Plugin<O = any> implements PluginInterface {
85
81
  afterSync: any[];
86
82
  afterLoad: any[];
87
83
  }>;
84
+ private getPluginBasePath;
88
85
  /**
89
86
  * @internal
90
87
  */
@@ -98,13 +95,5 @@ export declare abstract class Plugin<O = any> implements PluginInterface {
98
95
  * @experimental
99
96
  */
100
97
  toJSON(options?: any): Promise<any>;
101
- /**
102
- * @internal
103
- */
104
- protected getSourceDir(): Promise<string>;
105
- /**
106
- * @internal
107
- */
108
- protected isDev(): Promise<boolean>;
109
98
  }
110
99
  export default Plugin;
package/lib/plugin.js CHANGED
@@ -43,7 +43,6 @@ __export(plugin_exports, {
43
43
  module.exports = __toCommonJS(plugin_exports);
44
44
  var import_utils = require("@nocobase/utils");
45
45
  var import_fs = __toESM(require("fs"));
46
- var import_glob = __toESM(require("glob"));
47
46
  var import_path = require("path");
48
47
  var import_plugin_manager = require("./plugin-manager");
49
48
  var import_utils2 = require("./plugin-manager/utils");
@@ -136,45 +135,16 @@ const _Plugin = class _Plugin {
136
135
  setOptions(options) {
137
136
  this.options = options || {};
138
137
  }
139
- /**
140
- * @internal
141
- */
142
- async loadCommands() {
143
- const extensions = ["js", "ts"];
144
- const directory = (0, import_path.resolve)(
145
- process.env.NODE_MODULES_PATH,
146
- this.options.packageName,
147
- await this.getSourceDir(),
148
- "server/commands"
149
- );
150
- const patten = `${directory}/*.{${extensions.join(",")}}`;
151
- const files = import_glob.default.sync(patten, {
152
- ignore: ["**/*.d.ts"]
153
- });
154
- for (const file of files) {
155
- let filename = (0, import_path.basename)(file);
156
- filename = filename.substring(0, filename.lastIndexOf(".")) || filename;
157
- const callback = await (0, import_utils.importModule)(file);
158
- callback(this.app);
159
- }
160
- if (files.length) {
161
- this.app.log.debug(`load commands [${this.name}]`);
162
- }
163
- }
164
138
  /**
165
139
  * @internal
166
140
  */
167
141
  async loadMigrations() {
168
142
  this.app.log.debug(`load plugin migrations [${this.name}]`);
169
- if (!this.options.packageName) {
143
+ const basePath = await this.getPluginBasePath();
144
+ if (!basePath) {
170
145
  return { beforeLoad: [], afterSync: [], afterLoad: [] };
171
146
  }
172
- const directory = (0, import_path.resolve)(
173
- process.env.NODE_MODULES_PATH,
174
- this.options.packageName,
175
- await this.getSourceDir(),
176
- "server/migrations"
177
- );
147
+ const directory = (0, import_path.resolve)(basePath, "server/migrations");
178
148
  return await this.app.loadMigrations({
179
149
  directory,
180
150
  namespace: this.options.packageName,
@@ -183,19 +153,21 @@ const _Plugin = class _Plugin {
183
153
  }
184
154
  });
185
155
  }
156
+ async getPluginBasePath() {
157
+ if (!this.options.packageName) {
158
+ return;
159
+ }
160
+ return (0, import_utils2.getPluginBasePath)(this.options.packageName);
161
+ }
186
162
  /**
187
163
  * @internal
188
164
  */
189
165
  async loadCollections() {
190
- if (!this.options.packageName) {
166
+ const basePath = await this.getPluginBasePath();
167
+ if (!basePath) {
191
168
  return;
192
169
  }
193
- const directory = (0, import_path.resolve)(
194
- process.env.NODE_MODULES_PATH,
195
- this.options.packageName,
196
- await this.getSourceDir(),
197
- "server/collections"
198
- );
170
+ const directory = (0, import_path.resolve)(basePath, "server/collections");
199
171
  if (await (0, import_utils.fsExists)(directory)) {
200
172
  await this.db.import({
201
173
  directory,
@@ -246,36 +218,6 @@ const _Plugin = class _Plugin {
246
218
  }
247
219
  return results;
248
220
  }
249
- /**
250
- * @internal
251
- */
252
- async getSourceDir() {
253
- if (this._sourceDir) {
254
- return this._sourceDir;
255
- }
256
- if (await this.isDev()) {
257
- return this._sourceDir = "src";
258
- }
259
- if ((0, import_path.basename)(__dirname) === "src") {
260
- return this._sourceDir = "src";
261
- }
262
- return this._sourceDir = this.isPreset ? "lib" : "dist";
263
- }
264
- /**
265
- * @internal
266
- */
267
- async isDev() {
268
- if (!this.options.packageName) {
269
- return false;
270
- }
271
- const file = await import_fs.default.promises.realpath(
272
- (0, import_path.resolve)(process.env.NODE_MODULES_PATH || (0, import_path.resolve)(process.cwd(), "node_modules"), this.options.packageName)
273
- );
274
- if (file.startsWith((0, import_path.resolve)(process.cwd(), "packages"))) {
275
- return !!process.env.IS_DEV_CMD;
276
- }
277
- return false;
278
- }
279
221
  };
280
222
  __name(_Plugin, "Plugin");
281
223
  let Plugin = _Plugin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocobase/server",
3
- "version": "1.0.0-alpha.8",
3
+ "version": "1.0.1-alpha.1",
4
4
  "main": "lib/index.js",
5
5
  "types": "./lib/index.d.ts",
6
6
  "license": "AGPL-3.0",
@@ -10,18 +10,18 @@
10
10
  "@koa/cors": "^3.1.0",
11
11
  "@koa/multer": "^3.0.2",
12
12
  "@koa/router": "^9.4.0",
13
- "@nocobase/acl": "1.0.0-alpha.8",
14
- "@nocobase/actions": "1.0.0-alpha.8",
15
- "@nocobase/auth": "1.0.0-alpha.8",
16
- "@nocobase/cache": "1.0.0-alpha.8",
17
- "@nocobase/data-source-manager": "1.0.0-alpha.8",
18
- "@nocobase/database": "1.0.0-alpha.8",
19
- "@nocobase/evaluators": "1.0.0-alpha.8",
20
- "@nocobase/logger": "1.0.0-alpha.8",
21
- "@nocobase/resourcer": "1.0.0-alpha.8",
22
- "@nocobase/sdk": "1.0.0-alpha.8",
23
- "@nocobase/telemetry": "1.0.0-alpha.8",
24
- "@nocobase/utils": "1.0.0-alpha.8",
13
+ "@nocobase/acl": "1.0.1-alpha.1",
14
+ "@nocobase/actions": "1.0.1-alpha.1",
15
+ "@nocobase/auth": "1.0.1-alpha.1",
16
+ "@nocobase/cache": "1.0.1-alpha.1",
17
+ "@nocobase/data-source-manager": "1.0.1-alpha.1",
18
+ "@nocobase/database": "1.0.1-alpha.1",
19
+ "@nocobase/evaluators": "1.0.1-alpha.1",
20
+ "@nocobase/logger": "1.0.1-alpha.1",
21
+ "@nocobase/resourcer": "1.0.1-alpha.1",
22
+ "@nocobase/sdk": "1.0.1-alpha.1",
23
+ "@nocobase/telemetry": "1.0.1-alpha.1",
24
+ "@nocobase/utils": "1.0.1-alpha.1",
25
25
  "@types/decompress": "4.2.4",
26
26
  "@types/ini": "^1.3.31",
27
27
  "@types/koa-send": "^4.1.3",
@@ -54,5 +54,5 @@
54
54
  "@types/serve-handler": "^6.1.1",
55
55
  "@types/ws": "^8.5.5"
56
56
  },
57
- "gitHead": "3c4e978f6fc366c01621c9d625678e91f53f8662"
57
+ "gitHead": "d24aa16987a4068f857ae073fcce18f3cb490660"
58
58
  }