@tego/core 1.3.44
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/LICENSE +201 -0
- package/lib/acl/available-action.d.ts +5 -0
- package/lib/acl/available-action.js +62 -0
- package/lib/acl/index.d.ts +2 -0
- package/lib/acl/index.js +49 -0
- package/lib/aes-encryptor.d.ts +11 -0
- package/lib/aes-encryptor.js +116 -0
- package/lib/app-command.d.ts +12 -0
- package/lib/app-command.js +76 -0
- package/lib/app-supervisor.d.ts +61 -0
- package/lib/app-supervisor.js +299 -0
- package/lib/application.d.ts +354 -0
- package/lib/application.js +878 -0
- package/lib/cache/index.d.ts +3 -0
- package/lib/cache/index.js +35 -0
- package/lib/commands/console.d.ts +3 -0
- package/lib/commands/console.js +48 -0
- package/lib/commands/create-migration.d.ts +3 -0
- package/lib/commands/create-migration.js +80 -0
- package/lib/commands/db-auth.d.ts +3 -0
- package/lib/commands/db-auth.js +28 -0
- package/lib/commands/db-clean.d.ts +3 -0
- package/lib/commands/db-clean.js +31 -0
- package/lib/commands/db-sync.d.ts +3 -0
- package/lib/commands/db-sync.js +40 -0
- package/lib/commands/destroy.d.ts +3 -0
- package/lib/commands/destroy.js +30 -0
- package/lib/commands/index.d.ts +2 -0
- package/lib/commands/index.js +71 -0
- package/lib/commands/install.d.ts +3 -0
- package/lib/commands/install.js +33 -0
- package/lib/commands/migrator.d.ts +3 -0
- package/lib/commands/migrator.js +31 -0
- package/lib/commands/pm.d.ts +3 -0
- package/lib/commands/pm.js +75 -0
- package/lib/commands/refresh.d.ts +3 -0
- package/lib/commands/refresh.js +31 -0
- package/lib/commands/restart.d.ts +3 -0
- package/lib/commands/restart.js +35 -0
- package/lib/commands/start.d.ts +3 -0
- package/lib/commands/start.js +74 -0
- package/lib/commands/stop.d.ts +3 -0
- package/lib/commands/stop.js +34 -0
- package/lib/commands/upgrade.d.ts +3 -0
- package/lib/commands/upgrade.js +29 -0
- package/lib/cron/cron-job-manager.d.ts +25 -0
- package/lib/cron/cron-job-manager.js +89 -0
- package/lib/environment.d.ts +10 -0
- package/lib/environment.js +73 -0
- package/lib/errors/application-not-install.d.ts +4 -0
- package/lib/errors/application-not-install.js +35 -0
- package/lib/errors/handler.d.ts +3 -0
- package/lib/errors/handler.js +41 -0
- package/lib/errors/plugin-command-error.d.ts +2 -0
- package/lib/errors/plugin-command-error.js +31 -0
- package/lib/errors/plugin-not-exist.d.ts +2 -0
- package/lib/errors/plugin-not-exist.js +31 -0
- package/lib/gateway/errors.d.ts +22 -0
- package/lib/gateway/errors.js +138 -0
- package/lib/gateway/gateway.d.ts +56 -0
- package/lib/gateway/gateway.js +408 -0
- package/lib/gateway/index.d.ts +2 -0
- package/lib/gateway/index.js +30 -0
- package/lib/gateway/ipc-socket-client.d.ts +17 -0
- package/lib/gateway/ipc-socket-client.js +99 -0
- package/lib/gateway/ipc-socket-server.d.ts +12 -0
- package/lib/gateway/ipc-socket-server.js +140 -0
- package/lib/gateway/types.d.ts +25 -0
- package/lib/gateway/types.js +15 -0
- package/lib/gateway/ws-server.d.ts +63 -0
- package/lib/gateway/ws-server.js +335 -0
- package/lib/helper.d.ts +11 -0
- package/lib/helper.js +193 -0
- package/lib/helpers/application-version.d.ts +10 -0
- package/lib/helpers/application-version.js +77 -0
- package/lib/helpers/multiple-instance-manager.d.ts +6 -0
- package/lib/helpers/multiple-instance-manager.js +40 -0
- package/lib/index.d.ts +10 -0
- package/lib/index.js +57 -0
- package/lib/locale/index.d.ts +1 -0
- package/lib/locale/index.js +21 -0
- package/lib/locale/locale.d.ts +22 -0
- package/lib/locale/locale.js +166 -0
- package/lib/locale/resource.d.ts +1 -0
- package/lib/locale/resource.js +74 -0
- package/lib/main-data-source.d.ts +4 -0
- package/lib/main-data-source.js +45 -0
- package/lib/middlewares/data-template.d.ts +2 -0
- package/lib/middlewares/data-template.js +39 -0
- package/lib/middlewares/data-wrapping.d.ts +3 -0
- package/lib/middlewares/data-wrapping.js +90 -0
- package/lib/middlewares/db2resource.d.ts +6 -0
- package/lib/middlewares/db2resource.js +76 -0
- package/lib/middlewares/extract-client-ip.d.ts +2 -0
- package/lib/middlewares/extract-client-ip.js +37 -0
- package/lib/middlewares/i18n.d.ts +1 -0
- package/lib/middlewares/i18n.js +44 -0
- package/lib/middlewares/index.d.ts +3 -0
- package/lib/middlewares/index.js +32 -0
- package/lib/middlewares/parse-variables.d.ts +1 -0
- package/lib/middlewares/parse-variables.js +84 -0
- package/lib/migration.d.ts +12 -0
- package/lib/migration.js +47 -0
- package/lib/migrations/20230912193824-package-name-unique.d.ts +6 -0
- package/lib/migrations/20230912193824-package-name-unique.js +52 -0
- package/lib/migrations/20230912294620-update-pkg.d.ts +6 -0
- package/lib/migrations/20230912294620-update-pkg.js +58 -0
- package/lib/migrations/20240106082756-update-plugins.d.ts +6 -0
- package/lib/migrations/20240106082756-update-plugins.js +44 -0
- package/lib/migrations/20240705000001-remove-pkgs-approval.d.ts +6 -0
- package/lib/migrations/20240705000001-remove-pkgs-approval.js +47 -0
- package/lib/notice/index.d.ts +25 -0
- package/lib/notice/index.js +91 -0
- package/lib/plugin-manager/clientStaticUtils.d.ts +24 -0
- package/lib/plugin-manager/clientStaticUtils.js +156 -0
- package/lib/plugin-manager/constants.d.ts +7 -0
- package/lib/plugin-manager/constants.js +120 -0
- package/lib/plugin-manager/deps.d.ts +2 -0
- package/lib/plugin-manager/deps.js +68 -0
- package/lib/plugin-manager/index.d.ts +2 -0
- package/lib/plugin-manager/index.js +23 -0
- package/lib/plugin-manager/middleware.d.ts +2 -0
- package/lib/plugin-manager/middleware.js +36 -0
- package/lib/plugin-manager/options/collection.d.ts +2 -0
- package/lib/plugin-manager/options/collection.js +39 -0
- package/lib/plugin-manager/options/resource.d.ts +15 -0
- package/lib/plugin-manager/options/resource.js +185 -0
- package/lib/plugin-manager/plugin-manager-repository.d.ts +27 -0
- package/lib/plugin-manager/plugin-manager-repository.js +136 -0
- package/lib/plugin-manager/plugin-manager.d.ts +205 -0
- package/lib/plugin-manager/plugin-manager.js +1032 -0
- package/lib/plugin-manager/types.d.ts +15 -0
- package/lib/plugin-manager/types.js +15 -0
- package/lib/plugin-manager/utils.d.ts +101 -0
- package/lib/plugin-manager/utils.js +513 -0
- package/lib/plugin-manager/web-service.d.ts +6 -0
- package/lib/plugin-manager/web-service.js +116 -0
- package/lib/plugin.d.ts +125 -0
- package/lib/plugin.js +359 -0
- package/lib/pub-sub-manager/handler-manager.d.ts +28 -0
- package/lib/pub-sub-manager/handler-manager.js +149 -0
- package/lib/pub-sub-manager/index.d.ts +3 -0
- package/lib/pub-sub-manager/index.js +25 -0
- package/lib/pub-sub-manager/memory-pub-sub-adapter.d.ts +21 -0
- package/lib/pub-sub-manager/memory-pub-sub-adapter.js +82 -0
- package/lib/pub-sub-manager/pub-sub-manager.d.ts +19 -0
- package/lib/pub-sub-manager/pub-sub-manager.js +108 -0
- package/lib/pub-sub-manager/types.d.ts +20 -0
- package/lib/pub-sub-manager/types.js +15 -0
- package/lib/sync-message-manager.d.ts +16 -0
- package/lib/sync-message-manager.js +112 -0
- package/package.json +58 -0
|
@@ -0,0 +1,1032 @@
|
|
|
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 plugin_manager_exports = {};
|
|
30
|
+
__export(plugin_manager_exports, {
|
|
31
|
+
AddPresetError: () => AddPresetError,
|
|
32
|
+
PluginManager: () => PluginManager,
|
|
33
|
+
default: () => plugin_manager_default,
|
|
34
|
+
sleep: () => sleep
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(plugin_manager_exports);
|
|
37
|
+
var import_promises = __toESM(require("node:fs/promises"));
|
|
38
|
+
var import_node_path = require("node:path");
|
|
39
|
+
var import_di = require("@tachybase/di");
|
|
40
|
+
var import_globals = __toESM(require("@tachybase/globals"));
|
|
41
|
+
var import_utils = require("@tachybase/utils");
|
|
42
|
+
var import_execa = __toESM(require("execa"));
|
|
43
|
+
var import_fast_glob = __toESM(require("fast-glob"));
|
|
44
|
+
var import_lodash = __toESM(require("lodash"));
|
|
45
|
+
var import_helper = require("../helper");
|
|
46
|
+
var import_constants = require("./constants");
|
|
47
|
+
var import_middleware = require("./middleware");
|
|
48
|
+
var import_collection = __toESM(require("./options/collection"));
|
|
49
|
+
var import_resource = __toESM(require("./options/resource"));
|
|
50
|
+
var import_plugin_manager_repository = require("./plugin-manager-repository");
|
|
51
|
+
var import_utils2 = require("./utils");
|
|
52
|
+
var import_web_service = require("./web-service");
|
|
53
|
+
const sleep = /* @__PURE__ */ __name(async (timeout = 0) => {
|
|
54
|
+
return new Promise((resolve2) => {
|
|
55
|
+
setTimeout(resolve2, timeout);
|
|
56
|
+
});
|
|
57
|
+
}, "sleep");
|
|
58
|
+
const _AddPresetError = class _AddPresetError extends Error {
|
|
59
|
+
};
|
|
60
|
+
__name(_AddPresetError, "AddPresetError");
|
|
61
|
+
let AddPresetError = _AddPresetError;
|
|
62
|
+
const _PluginManager = class _PluginManager {
|
|
63
|
+
/**
|
|
64
|
+
* @internal
|
|
65
|
+
*/
|
|
66
|
+
constructor(options) {
|
|
67
|
+
this.options = options;
|
|
68
|
+
/**
|
|
69
|
+
* @internal
|
|
70
|
+
*/
|
|
71
|
+
this.pluginInstances = /* @__PURE__ */ new Map();
|
|
72
|
+
/**
|
|
73
|
+
* @internal
|
|
74
|
+
*/
|
|
75
|
+
this.pluginAliases = /* @__PURE__ */ new Map();
|
|
76
|
+
this.app = options.app;
|
|
77
|
+
this.app.db.registerRepositories({
|
|
78
|
+
PluginManagerRepository: import_plugin_manager_repository.PluginManagerRepository
|
|
79
|
+
});
|
|
80
|
+
this.collection = this.app.db.collection(import_collection.default);
|
|
81
|
+
this._repository = this.collection.repository;
|
|
82
|
+
this._repository.setPluginManager(this);
|
|
83
|
+
this.app.resourcer.define(import_resource.default);
|
|
84
|
+
this.app.acl.allow("pm", "listEnabled", "public");
|
|
85
|
+
this.app.acl.registerSnippet({
|
|
86
|
+
name: "pm",
|
|
87
|
+
actions: ["pm:*"]
|
|
88
|
+
});
|
|
89
|
+
this.app.db.addMigrations({
|
|
90
|
+
namespace: "core/pm",
|
|
91
|
+
directory: (0, import_node_path.resolve)(__dirname, "../migrations")
|
|
92
|
+
});
|
|
93
|
+
this.app.resourcer.use(import_middleware.uploadMiddleware, { tag: "pmFileUpload" });
|
|
94
|
+
}
|
|
95
|
+
get repository() {
|
|
96
|
+
return this.app.db.getRepository("applicationPlugins");
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* @internal
|
|
100
|
+
*/
|
|
101
|
+
static async getPackageJson(packageName) {
|
|
102
|
+
const pluginPaths = import_globals.default.getInstance().get("PLUGIN_PATHS");
|
|
103
|
+
for (const basePath of pluginPaths) {
|
|
104
|
+
const pkgPath = (0, import_node_path.resolve)(basePath, packageName, "package.json");
|
|
105
|
+
try {
|
|
106
|
+
const realPath = await import_promises.default.realpath(pkgPath);
|
|
107
|
+
const data = await import_promises.default.readFile(realPath, { encoding: "utf-8" });
|
|
108
|
+
return JSON.parse(data);
|
|
109
|
+
} catch (err) {
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
throw new Error(`package.json for ${packageName} not found in any PLUGIN_PATHS`);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* @internal
|
|
116
|
+
*/
|
|
117
|
+
static async getPackageName(name) {
|
|
118
|
+
const pluginPaths = import_globals.default.getInstance().get("PLUGIN_PATHS");
|
|
119
|
+
const prefixes = this.getPluginPkgPrefix();
|
|
120
|
+
for (const prefix of prefixes) {
|
|
121
|
+
for (const basePath of pluginPaths) {
|
|
122
|
+
const pkg = (0, import_node_path.resolve)(basePath, `${prefix}${name}`, "package.json");
|
|
123
|
+
const exists = await (0, import_utils.fsExists)(pkg);
|
|
124
|
+
if (exists) {
|
|
125
|
+
return `${prefix}${name}`;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
throw new Error(`${name} plugin does not exist`);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* @internal
|
|
133
|
+
*/
|
|
134
|
+
static getPluginPkgPrefix() {
|
|
135
|
+
return import_constants.pluginPrefix;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* @internal
|
|
139
|
+
*/
|
|
140
|
+
static async findPackage(name) {
|
|
141
|
+
try {
|
|
142
|
+
const packageName = this.getPackageName(name);
|
|
143
|
+
return packageName;
|
|
144
|
+
} catch (error) {
|
|
145
|
+
console.log(`\`${name}\` plugin not found locally`);
|
|
146
|
+
const prefixes = this.getPluginPkgPrefix();
|
|
147
|
+
for (const prefix of prefixes) {
|
|
148
|
+
try {
|
|
149
|
+
const packageName = `${prefix}${name}`;
|
|
150
|
+
console.log(`Try to find ${packageName}`);
|
|
151
|
+
await (0, import_execa.default)("npm", ["v", packageName, "versions"]);
|
|
152
|
+
console.log(`${packageName} downloading`);
|
|
153
|
+
await (0, import_execa.default)("pnpm", ["add", packageName, "-W"]);
|
|
154
|
+
console.log(`${packageName} downloaded`);
|
|
155
|
+
return packageName;
|
|
156
|
+
} catch (error2) {
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
throw new Error(`No available packages found, ${name} plugin does not exist`);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* @internal
|
|
165
|
+
*/
|
|
166
|
+
static clearCache(packageName) {
|
|
167
|
+
return;
|
|
168
|
+
const packageNamePath = packageName.replace("/", import_node_path.sep);
|
|
169
|
+
Object.keys(require.cache).forEach((key) => {
|
|
170
|
+
if (key.includes(packageNamePath)) {
|
|
171
|
+
delete require.cache[key];
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* @internal
|
|
177
|
+
*/
|
|
178
|
+
static async resolvePlugin(pluginName, isUpgrade = false, isPkg = false) {
|
|
179
|
+
if (typeof pluginName === "string") {
|
|
180
|
+
const packageName = isPkg ? pluginName : await this.getPackageName(pluginName);
|
|
181
|
+
this.clearCache(packageName);
|
|
182
|
+
return await (0, import_utils.importModule)(packageName);
|
|
183
|
+
} else {
|
|
184
|
+
return pluginName;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
addPreset(plugin, options = {}) {
|
|
188
|
+
if (this.app.loaded) {
|
|
189
|
+
throw new AddPresetError("must be added before executing app.load()");
|
|
190
|
+
}
|
|
191
|
+
if (!this.options.plugins) {
|
|
192
|
+
this.options.plugins = [];
|
|
193
|
+
}
|
|
194
|
+
this.options.plugins.push([plugin, options]);
|
|
195
|
+
}
|
|
196
|
+
getPlugins() {
|
|
197
|
+
return this.app.pm.pluginInstances;
|
|
198
|
+
}
|
|
199
|
+
getAliases() {
|
|
200
|
+
return this.app.pm.pluginAliases.keys();
|
|
201
|
+
}
|
|
202
|
+
get(name) {
|
|
203
|
+
if (typeof name === "string") {
|
|
204
|
+
return this.app.pm.pluginAliases.get(name);
|
|
205
|
+
}
|
|
206
|
+
return this.app.pm.pluginInstances.get(name);
|
|
207
|
+
}
|
|
208
|
+
has(name) {
|
|
209
|
+
if (typeof name === "string") {
|
|
210
|
+
return this.app.pm.pluginAliases.has(name);
|
|
211
|
+
}
|
|
212
|
+
return this.app.pm.pluginInstances.has(name);
|
|
213
|
+
}
|
|
214
|
+
del(name) {
|
|
215
|
+
const instance = this.get(name);
|
|
216
|
+
if (instance) {
|
|
217
|
+
this.app.pm.pluginAliases.delete(instance.name);
|
|
218
|
+
this.app.pm.pluginInstances.delete(instance.constructor);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
async create(pluginName, options) {
|
|
222
|
+
const createPlugin = /* @__PURE__ */ __name(async (name) => {
|
|
223
|
+
const pluginDir = (0, import_node_path.resolve)(process.cwd(), "packages", name);
|
|
224
|
+
if (options == null ? void 0 : options.forceRecreate) {
|
|
225
|
+
await import_promises.default.rm(pluginDir, { recursive: true, force: true });
|
|
226
|
+
}
|
|
227
|
+
await (0, import_execa.default)("pnpm", ["run", "tb", "create-plugin", name]);
|
|
228
|
+
}, "createPlugin");
|
|
229
|
+
await createPlugin(pluginName);
|
|
230
|
+
try {
|
|
231
|
+
await this.app.db.auth({ retry: 1 });
|
|
232
|
+
const installed = await this.app.isInstalled();
|
|
233
|
+
if (!installed) {
|
|
234
|
+
console.log(`pnpm pm add ${pluginName}`);
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
} catch (error) {
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
this.app.logger.info("attempt to add the plugin to the app");
|
|
241
|
+
await (0, import_utils.createDevPluginsSymlink)();
|
|
242
|
+
let packageName;
|
|
243
|
+
try {
|
|
244
|
+
packageName = await _PluginManager.getPackageName(pluginName);
|
|
245
|
+
} catch (error) {
|
|
246
|
+
packageName = pluginName;
|
|
247
|
+
}
|
|
248
|
+
const json = await _PluginManager.getPackageJson(packageName);
|
|
249
|
+
this.app.logger.info(`add plugin [${packageName}]`, {
|
|
250
|
+
name: pluginName,
|
|
251
|
+
packageName,
|
|
252
|
+
version: json.version
|
|
253
|
+
});
|
|
254
|
+
await this.repository.updateOrCreate({
|
|
255
|
+
values: {
|
|
256
|
+
name: pluginName,
|
|
257
|
+
packageName,
|
|
258
|
+
version: json.version
|
|
259
|
+
},
|
|
260
|
+
filterKeys: ["name"]
|
|
261
|
+
});
|
|
262
|
+
await sleep(1e3);
|
|
263
|
+
await (0, import_helper.tsxRerunning)();
|
|
264
|
+
}
|
|
265
|
+
async add(plugin, options = {}, insert = false, isUpgrade = false) {
|
|
266
|
+
if (!isUpgrade && this.has(plugin)) {
|
|
267
|
+
const name = typeof plugin === "string" ? plugin : plugin.name;
|
|
268
|
+
this.app.logger.warn(`plugin [${name}] added`);
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
if (!options.name && typeof plugin === "string") {
|
|
272
|
+
options.name = plugin;
|
|
273
|
+
}
|
|
274
|
+
try {
|
|
275
|
+
if (typeof plugin === "string" && options.name && !options.packageName && !options.isPreset) {
|
|
276
|
+
const packageName = await _PluginManager.getPackageName(options.name);
|
|
277
|
+
options["packageName"] = packageName;
|
|
278
|
+
}
|
|
279
|
+
if (options.packageName) {
|
|
280
|
+
const packageJson = await _PluginManager.getPackageJson(options.packageName);
|
|
281
|
+
options["packageJson"] = packageJson;
|
|
282
|
+
options["version"] = packageJson.version;
|
|
283
|
+
}
|
|
284
|
+
} catch (error) {
|
|
285
|
+
this.app.logger.error(error);
|
|
286
|
+
}
|
|
287
|
+
this.app.logger.debug(`add plugin [${options.name}]`, {
|
|
288
|
+
method: "add",
|
|
289
|
+
submodule: "plugin-manager",
|
|
290
|
+
name: options.name
|
|
291
|
+
});
|
|
292
|
+
let P;
|
|
293
|
+
try {
|
|
294
|
+
if (options.isPreset) {
|
|
295
|
+
P = import_globals.default.getInstance().get("PRESETS")[options.name];
|
|
296
|
+
} else {
|
|
297
|
+
P = await _PluginManager.resolvePlugin(options.packageName || plugin, isUpgrade, !!options.packageName);
|
|
298
|
+
}
|
|
299
|
+
} catch (error) {
|
|
300
|
+
if (process.env.APP_ENV === "development") {
|
|
301
|
+
throw error;
|
|
302
|
+
}
|
|
303
|
+
this.app.logger.warn("plugin not found", error);
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
const instance = new P((0, import_helper.createAppProxy)(this.app), options);
|
|
307
|
+
this.pluginInstances.set(P, instance);
|
|
308
|
+
if (options.name) {
|
|
309
|
+
if (this.app.name === "main" || options.subView) {
|
|
310
|
+
this.pluginAliases.set(options.name, instance);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
if (insert && options.name) {
|
|
314
|
+
await this.repository.updateOrCreate({
|
|
315
|
+
values: {
|
|
316
|
+
...options
|
|
317
|
+
},
|
|
318
|
+
filterKeys: ["name"]
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
await instance.afterAdd();
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* @internal
|
|
325
|
+
*/
|
|
326
|
+
async initPlugins() {
|
|
327
|
+
await this.initRuntimePlugins();
|
|
328
|
+
await this.initPresetPlugins();
|
|
329
|
+
await this.initOtherPlugins();
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* @internal
|
|
333
|
+
*/
|
|
334
|
+
async loadCommands() {
|
|
335
|
+
this.app.logger.debug("load commands");
|
|
336
|
+
const items = await this.repository.find({
|
|
337
|
+
filter: {
|
|
338
|
+
enabled: true
|
|
339
|
+
}
|
|
340
|
+
});
|
|
341
|
+
const packageNames = items.map((item) => item.packageName);
|
|
342
|
+
const source = [];
|
|
343
|
+
const pluginPaths = import_globals.default.getInstance().get("PLUGIN_PATHS");
|
|
344
|
+
for (const packageName of packageNames) {
|
|
345
|
+
const file = require.resolve(packageName, { paths: [process.cwd(), ...pluginPaths] });
|
|
346
|
+
const sourceDir = (0, import_node_path.basename)((0, import_node_path.dirname)(file)) === "src" ? "src" : "dist";
|
|
347
|
+
const directory = (0, import_node_path.join)(
|
|
348
|
+
packageName,
|
|
349
|
+
sourceDir,
|
|
350
|
+
"server/commands/*." + ((0, import_node_path.basename)((0, import_node_path.dirname)(file)) === "src" ? "ts" : "js")
|
|
351
|
+
);
|
|
352
|
+
source.push(directory.replaceAll(import_node_path.sep, "/"));
|
|
353
|
+
}
|
|
354
|
+
for (const plugin of this.options.plugins || []) {
|
|
355
|
+
if (typeof plugin === "string") {
|
|
356
|
+
if (plugin in import_globals.default.getInstance().get("PRESETS")) {
|
|
357
|
+
continue;
|
|
358
|
+
}
|
|
359
|
+
const packageName = await _PluginManager.getPackageName(plugin);
|
|
360
|
+
const file = require.resolve(packageName, { paths: [process.cwd(), ...pluginPaths] });
|
|
361
|
+
const sourceDir = (0, import_node_path.basename)((0, import_node_path.dirname)(file)) === "src" ? "src" : "lib";
|
|
362
|
+
const directory = (0, import_node_path.join)(packageName, sourceDir, "server/commands/*." + (sourceDir === "src" ? "ts" : "js"));
|
|
363
|
+
source.push(directory.replaceAll(import_node_path.sep, "/"));
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
for (const basePath of pluginPaths) {
|
|
367
|
+
const files = await (0, import_fast_glob.default)(source, {
|
|
368
|
+
ignore: ["**/*.d.ts"],
|
|
369
|
+
cwd: basePath
|
|
370
|
+
});
|
|
371
|
+
for (const file of files) {
|
|
372
|
+
const callback = await (0, import_utils.importModule)(file);
|
|
373
|
+
callback(this.app);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
async load(options = {}) {
|
|
378
|
+
import_di.Container.reset();
|
|
379
|
+
import_di.Container.set({ id: "db", value: this.app.db });
|
|
380
|
+
import_di.Container.set({ id: "app", value: (0, import_helper.createAppProxy)(this.app) });
|
|
381
|
+
import_di.Container.set({ id: "logger", value: this.app.logger });
|
|
382
|
+
await import_di.Container.get(import_web_service.WebControllerService).load();
|
|
383
|
+
this.app.setMaintainingMessage("loading plugins...");
|
|
384
|
+
const total = this.pluginInstances.size;
|
|
385
|
+
let current = 0;
|
|
386
|
+
for (const [P, plugin] of this.getPlugins()) {
|
|
387
|
+
if (plugin.state.loaded) {
|
|
388
|
+
continue;
|
|
389
|
+
}
|
|
390
|
+
const name = plugin.name || P.name;
|
|
391
|
+
current += 1;
|
|
392
|
+
this.app.setMaintainingMessage(`before load plugin [${name}], ${current}/${total}`);
|
|
393
|
+
if (!plugin.enabled) {
|
|
394
|
+
continue;
|
|
395
|
+
}
|
|
396
|
+
this.app.logger.debug(`before load plugin [${name}]`, { submodule: "plugin-manager", method: "load", name });
|
|
397
|
+
await plugin.beforeLoad();
|
|
398
|
+
for (const feature of plugin.featureInstances) {
|
|
399
|
+
await feature.beforeLoad();
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
current = 0;
|
|
403
|
+
for (const [P, plugin] of this.getPlugins()) {
|
|
404
|
+
if (plugin.state.loaded) {
|
|
405
|
+
continue;
|
|
406
|
+
}
|
|
407
|
+
const name = plugin.name || P.name;
|
|
408
|
+
current += 1;
|
|
409
|
+
this.app.setMaintainingMessage(`${this.app.i18n.t("load plugin")} [${name}], ${current}/${total}`);
|
|
410
|
+
if (!plugin.enabled) {
|
|
411
|
+
continue;
|
|
412
|
+
}
|
|
413
|
+
await this.app.emitAsync("beforeLoadPlugin", plugin, options);
|
|
414
|
+
this.app.logger.debug(`load plugin [${name}] `, { submodule: "plugin-manager", method: "load", name });
|
|
415
|
+
await plugin.loadCollections();
|
|
416
|
+
await plugin.load();
|
|
417
|
+
plugin.state.loaded = true;
|
|
418
|
+
await this.app.emitAsync("afterLoadPlugin", plugin, options);
|
|
419
|
+
for (const feature of plugin.featureInstances) {
|
|
420
|
+
await feature.load();
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
this.app.setMaintainingMessage("loaded plugins");
|
|
424
|
+
}
|
|
425
|
+
async install(options = {}) {
|
|
426
|
+
this.app.setMaintainingMessage("install plugins...");
|
|
427
|
+
const total = this.pluginInstances.size;
|
|
428
|
+
let current = 0;
|
|
429
|
+
this.app.logger.debug("call db.sync()");
|
|
430
|
+
await this.app.db.sync();
|
|
431
|
+
const toBeUpdated = [];
|
|
432
|
+
for (const [P, plugin] of this.getPlugins()) {
|
|
433
|
+
if (plugin.state.installing || plugin.state.installed) {
|
|
434
|
+
continue;
|
|
435
|
+
}
|
|
436
|
+
const name = plugin.name || P.name;
|
|
437
|
+
current += 1;
|
|
438
|
+
if (!plugin.enabled) {
|
|
439
|
+
continue;
|
|
440
|
+
}
|
|
441
|
+
plugin.state.installing = true;
|
|
442
|
+
this.app.setMaintainingMessage(`before install plugin [${name}], ${current}/${total}`);
|
|
443
|
+
await this.app.emitAsync("beforeInstallPlugin", plugin, options);
|
|
444
|
+
this.app.logger.debug(`install plugin [${name}]...`);
|
|
445
|
+
await plugin.install(options);
|
|
446
|
+
toBeUpdated.push(name);
|
|
447
|
+
plugin.state.installing = false;
|
|
448
|
+
plugin.state.installed = true;
|
|
449
|
+
plugin.installed = true;
|
|
450
|
+
this.app.setMaintainingMessage(`after install plugin [${name}], ${current}/${total}`);
|
|
451
|
+
await this.app.emitAsync("afterInstallPlugin", plugin, options);
|
|
452
|
+
for (const feature of plugin.featureInstances) {
|
|
453
|
+
await feature.install(options);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
await this.repository.update({
|
|
457
|
+
filter: {
|
|
458
|
+
name: toBeUpdated
|
|
459
|
+
},
|
|
460
|
+
values: {
|
|
461
|
+
installed: true
|
|
462
|
+
}
|
|
463
|
+
});
|
|
464
|
+
}
|
|
465
|
+
async enable(name) {
|
|
466
|
+
const pluginNames = import_lodash.default.castArray(name);
|
|
467
|
+
this.app.logger.debug(`enabling plugin ${pluginNames.join(",")}`);
|
|
468
|
+
this.app.setMaintainingMessage(`enabling plugin ${pluginNames.join(",")}`);
|
|
469
|
+
const toBeUpdated = [];
|
|
470
|
+
for (const pluginName of pluginNames) {
|
|
471
|
+
const plugin = this.get(pluginName);
|
|
472
|
+
if (!plugin) {
|
|
473
|
+
throw new Error(`${pluginName} plugin does not exist`);
|
|
474
|
+
}
|
|
475
|
+
if (plugin.enabled) {
|
|
476
|
+
continue;
|
|
477
|
+
}
|
|
478
|
+
await this.app.emitAsync("beforeEnablePlugin", pluginName);
|
|
479
|
+
await plugin.beforeEnable();
|
|
480
|
+
plugin.enabled = true;
|
|
481
|
+
toBeUpdated.push(pluginName);
|
|
482
|
+
}
|
|
483
|
+
if (toBeUpdated.length === 0) {
|
|
484
|
+
return;
|
|
485
|
+
}
|
|
486
|
+
await this.repository.update({
|
|
487
|
+
filter: {
|
|
488
|
+
name: toBeUpdated
|
|
489
|
+
},
|
|
490
|
+
values: {
|
|
491
|
+
enabled: true
|
|
492
|
+
}
|
|
493
|
+
});
|
|
494
|
+
try {
|
|
495
|
+
await this.app.reload();
|
|
496
|
+
this.app.logger.debug(`syncing database in enable plugin ${pluginNames.join(",")}...`);
|
|
497
|
+
this.app.setMaintainingMessage(`syncing database in enable plugin ${pluginNames.join(",")}...`);
|
|
498
|
+
await this.app.db.sync();
|
|
499
|
+
for (const pluginName of pluginNames) {
|
|
500
|
+
const plugin = this.get(pluginName);
|
|
501
|
+
if (!plugin.installed) {
|
|
502
|
+
this.app.logger.debug(`installing plugin ${pluginName}...`);
|
|
503
|
+
this.app.setMaintainingMessage(`installing plugin ${pluginName}...`);
|
|
504
|
+
await plugin.install();
|
|
505
|
+
plugin.installed = true;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
await this.repository.update({
|
|
509
|
+
filter: {
|
|
510
|
+
name: toBeUpdated
|
|
511
|
+
},
|
|
512
|
+
values: {
|
|
513
|
+
installed: true
|
|
514
|
+
}
|
|
515
|
+
});
|
|
516
|
+
for (const pluginName of pluginNames) {
|
|
517
|
+
const plugin = this.get(pluginName);
|
|
518
|
+
this.app.logger.debug(`emit afterEnablePlugin event...`);
|
|
519
|
+
await plugin.afterEnable();
|
|
520
|
+
await this.app.emitAsync("afterEnablePlugin", pluginName);
|
|
521
|
+
this.app.logger.debug(`afterEnablePlugin event emitted`);
|
|
522
|
+
}
|
|
523
|
+
await this.app.tryReloadOrRestart();
|
|
524
|
+
} catch (error) {
|
|
525
|
+
await this.repository.update({
|
|
526
|
+
filter: {
|
|
527
|
+
name: toBeUpdated
|
|
528
|
+
},
|
|
529
|
+
values: {
|
|
530
|
+
enabled: false,
|
|
531
|
+
installed: false
|
|
532
|
+
}
|
|
533
|
+
});
|
|
534
|
+
await this.app.tryReloadOrRestart({
|
|
535
|
+
recover: true
|
|
536
|
+
});
|
|
537
|
+
throw error;
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
async disable(name) {
|
|
541
|
+
const pluginNames = import_lodash.default.castArray(name);
|
|
542
|
+
this.app.logger.debug(`disabling plugin ${pluginNames.join(",")}`);
|
|
543
|
+
this.app.setMaintainingMessage(`disabling plugin ${pluginNames.join(",")}`);
|
|
544
|
+
const toBeUpdated = [];
|
|
545
|
+
for (const pluginName of pluginNames) {
|
|
546
|
+
const plugin = this.get(pluginName);
|
|
547
|
+
if (!plugin) {
|
|
548
|
+
throw new Error(`${pluginName} plugin does not exist`);
|
|
549
|
+
}
|
|
550
|
+
if (!plugin.enabled) {
|
|
551
|
+
continue;
|
|
552
|
+
}
|
|
553
|
+
await this.app.emitAsync("beforeDisablePlugin", pluginName);
|
|
554
|
+
await plugin.beforeDisable();
|
|
555
|
+
plugin.enabled = false;
|
|
556
|
+
toBeUpdated.push(pluginName);
|
|
557
|
+
}
|
|
558
|
+
if (toBeUpdated.length === 0) {
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
561
|
+
await this.repository.update({
|
|
562
|
+
filter: {
|
|
563
|
+
name: toBeUpdated
|
|
564
|
+
},
|
|
565
|
+
values: {
|
|
566
|
+
enabled: false
|
|
567
|
+
}
|
|
568
|
+
});
|
|
569
|
+
try {
|
|
570
|
+
await this.app.tryReloadOrRestart();
|
|
571
|
+
for (const pluginName of pluginNames) {
|
|
572
|
+
const plugin = this.get(pluginName);
|
|
573
|
+
this.app.logger.debug(`emit afterDisablePlugin event...`);
|
|
574
|
+
await plugin.afterDisable();
|
|
575
|
+
await this.app.emitAsync("afterDisablePlugin", pluginName);
|
|
576
|
+
this.app.logger.debug(`afterDisablePlugin event emitted`);
|
|
577
|
+
}
|
|
578
|
+
} catch (error) {
|
|
579
|
+
await this.repository.update({
|
|
580
|
+
filter: {
|
|
581
|
+
name: toBeUpdated
|
|
582
|
+
},
|
|
583
|
+
values: {
|
|
584
|
+
enabled: true
|
|
585
|
+
}
|
|
586
|
+
});
|
|
587
|
+
await this.app.tryReloadOrRestart({
|
|
588
|
+
recover: true
|
|
589
|
+
});
|
|
590
|
+
throw error;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
async remove(name, options) {
|
|
594
|
+
const pluginNames = import_lodash.default.castArray(name);
|
|
595
|
+
const records = pluginNames.map((name2) => {
|
|
596
|
+
return {
|
|
597
|
+
name: name2,
|
|
598
|
+
packageName: name2
|
|
599
|
+
};
|
|
600
|
+
});
|
|
601
|
+
const removeDir = /* @__PURE__ */ __name(async () => {
|
|
602
|
+
await Promise.all(
|
|
603
|
+
records.map(async (plugin) => {
|
|
604
|
+
const pluginPaths = import_globals.default.getInstance().get("PLUGIN_PATHS");
|
|
605
|
+
for (const basePath of pluginPaths) {
|
|
606
|
+
const dir = (0, import_node_path.resolve)(basePath, plugin.packageName);
|
|
607
|
+
try {
|
|
608
|
+
const realDir = await import_promises.default.realpath(dir);
|
|
609
|
+
this.app.logger.debug(`rm -rf ${realDir}`);
|
|
610
|
+
await import_promises.default.rm(realDir, { force: true, recursive: true });
|
|
611
|
+
return true;
|
|
612
|
+
} catch (error) {
|
|
613
|
+
}
|
|
614
|
+
return false;
|
|
615
|
+
}
|
|
616
|
+
})
|
|
617
|
+
);
|
|
618
|
+
await (0, import_execa.default)("pnpm", ["tachybase", "postinstall"]);
|
|
619
|
+
}, "removeDir");
|
|
620
|
+
if (options == null ? void 0 : options.force) {
|
|
621
|
+
await this.repository.destroy({
|
|
622
|
+
filter: {
|
|
623
|
+
name: pluginNames
|
|
624
|
+
}
|
|
625
|
+
});
|
|
626
|
+
} else {
|
|
627
|
+
await this.app.load();
|
|
628
|
+
for (const pluginName of pluginNames) {
|
|
629
|
+
const plugin = this.get(pluginName);
|
|
630
|
+
if (!plugin) {
|
|
631
|
+
continue;
|
|
632
|
+
}
|
|
633
|
+
if (plugin.enabled) {
|
|
634
|
+
throw new Error(`plugin is enabled [${pluginName}]`);
|
|
635
|
+
}
|
|
636
|
+
await plugin.beforeRemove();
|
|
637
|
+
}
|
|
638
|
+
await this.repository.destroy({
|
|
639
|
+
filter: {
|
|
640
|
+
name: pluginNames
|
|
641
|
+
}
|
|
642
|
+
});
|
|
643
|
+
const plugins = [];
|
|
644
|
+
for (const pluginName of pluginNames) {
|
|
645
|
+
const plugin = this.get(pluginName);
|
|
646
|
+
if (!plugin) {
|
|
647
|
+
continue;
|
|
648
|
+
}
|
|
649
|
+
plugins.push(plugin);
|
|
650
|
+
this.del(pluginName);
|
|
651
|
+
await plugin.afterRemove();
|
|
652
|
+
}
|
|
653
|
+
if (await this.app.isStarted()) {
|
|
654
|
+
await this.app.tryReloadOrRestart();
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
if (options == null ? void 0 : options.removeDir) {
|
|
658
|
+
await removeDir();
|
|
659
|
+
}
|
|
660
|
+
await (0, import_execa.default)("pnpm", ["tachybase", "refresh"]);
|
|
661
|
+
}
|
|
662
|
+
/**
|
|
663
|
+
* @deprecated
|
|
664
|
+
*/
|
|
665
|
+
async loadOne(plugin) {
|
|
666
|
+
this.app.setMaintainingMessage(`loading plugin ${plugin.name}...`);
|
|
667
|
+
if (plugin.state.loaded || !plugin.enabled) {
|
|
668
|
+
return;
|
|
669
|
+
}
|
|
670
|
+
const name = plugin.getName();
|
|
671
|
+
await plugin.beforeLoad();
|
|
672
|
+
await this.app.emitAsync("beforeLoadPlugin", plugin, {});
|
|
673
|
+
this.app.logger.debug(`loading plugin...`, { submodule: "plugin-manager", method: "loadOne", name });
|
|
674
|
+
await plugin.load();
|
|
675
|
+
plugin.state.loaded = true;
|
|
676
|
+
await this.app.emitAsync("afterLoadPlugin", plugin, {});
|
|
677
|
+
this.app.logger.debug(`after load plugin...`, { submodule: "plugin-manager", method: "loadOne", name });
|
|
678
|
+
this.app.setMaintainingMessage(`loaded plugin ${plugin.name}`);
|
|
679
|
+
}
|
|
680
|
+
/**
|
|
681
|
+
* @internal
|
|
682
|
+
*/
|
|
683
|
+
async addViaCLI(urlOrName, options) {
|
|
684
|
+
if ((0, import_utils.isURL)(urlOrName)) {
|
|
685
|
+
await this.addByCompressedFileUrl({
|
|
686
|
+
...options,
|
|
687
|
+
compressedFileUrl: urlOrName
|
|
688
|
+
});
|
|
689
|
+
} else if (await (0, import_utils.fsExists)(urlOrName)) {
|
|
690
|
+
await this.addByCompressedFileUrl({
|
|
691
|
+
...options,
|
|
692
|
+
compressedFileUrl: urlOrName
|
|
693
|
+
});
|
|
694
|
+
} else if (options == null ? void 0 : options.registry) {
|
|
695
|
+
if (!options.name) {
|
|
696
|
+
const model = await this.repository.findOne({ filter: { packageName: urlOrName } });
|
|
697
|
+
if (model) {
|
|
698
|
+
options["name"] = model == null ? void 0 : model.name;
|
|
699
|
+
}
|
|
700
|
+
if (!options.name) {
|
|
701
|
+
options["name"] = urlOrName.replace("@tachybase/plugin-", "");
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
await this.addByNpm({
|
|
705
|
+
...options,
|
|
706
|
+
packageName: urlOrName
|
|
707
|
+
});
|
|
708
|
+
} else {
|
|
709
|
+
const opts = {
|
|
710
|
+
...options
|
|
711
|
+
};
|
|
712
|
+
const model = await this.repository.findOne({ filter: { packageName: urlOrName } });
|
|
713
|
+
if (model) {
|
|
714
|
+
opts["name"] = model.name;
|
|
715
|
+
}
|
|
716
|
+
if (!opts["packageName"]) {
|
|
717
|
+
opts["packageName"] = urlOrName;
|
|
718
|
+
}
|
|
719
|
+
await this.add(opts["name"] || urlOrName, opts, true);
|
|
720
|
+
}
|
|
721
|
+
await this.app.emitStartedEvent();
|
|
722
|
+
await (0, import_execa.default)("pnpm", ["tachybase", "postinstall"]);
|
|
723
|
+
}
|
|
724
|
+
/**
|
|
725
|
+
* @internal
|
|
726
|
+
*/
|
|
727
|
+
async addByNpm(options) {
|
|
728
|
+
let { name = "", registry, packageName, authToken } = options;
|
|
729
|
+
name = name.trim();
|
|
730
|
+
registry = registry.trim();
|
|
731
|
+
packageName = packageName.trim();
|
|
732
|
+
authToken = authToken == null ? void 0 : authToken.trim();
|
|
733
|
+
const { compressedFileUrl } = await (0, import_utils2.getPluginInfoByNpm)({
|
|
734
|
+
packageName,
|
|
735
|
+
registry,
|
|
736
|
+
authToken
|
|
737
|
+
});
|
|
738
|
+
return this.addByCompressedFileUrl({ name, compressedFileUrl, registry, authToken, type: "npm" });
|
|
739
|
+
}
|
|
740
|
+
/**
|
|
741
|
+
* @internal
|
|
742
|
+
*/
|
|
743
|
+
async addByFile(options) {
|
|
744
|
+
const { file, authToken } = options;
|
|
745
|
+
const { packageName, tempFile, tempPackageContentDir } = await (0, import_utils2.downloadAndUnzipToTempDir)(file, authToken);
|
|
746
|
+
const name = options.name || packageName;
|
|
747
|
+
if (this.has(name)) {
|
|
748
|
+
await (0, import_utils2.removeTmpDir)(tempFile, tempPackageContentDir);
|
|
749
|
+
throw new Error(`plugin name [${name}] already exists`);
|
|
750
|
+
}
|
|
751
|
+
await (0, import_utils2.copyTempPackageToStorageAndLinkToNodeModules)(tempFile, tempPackageContentDir, packageName);
|
|
752
|
+
return this.add(name, { packageName }, true);
|
|
753
|
+
}
|
|
754
|
+
/**
|
|
755
|
+
* @internal
|
|
756
|
+
*/
|
|
757
|
+
async addByCompressedFileUrl(options) {
|
|
758
|
+
const { compressedFileUrl, authToken } = options;
|
|
759
|
+
const { packageName, tempFile, tempPackageContentDir } = await (0, import_utils2.downloadAndUnzipToTempDir)(
|
|
760
|
+
compressedFileUrl,
|
|
761
|
+
authToken
|
|
762
|
+
);
|
|
763
|
+
const name = options.name || packageName;
|
|
764
|
+
if (this.has(name)) {
|
|
765
|
+
await (0, import_utils2.removeTmpDir)(tempFile, tempPackageContentDir);
|
|
766
|
+
throw new Error(`plugin name [${name}] already exists`);
|
|
767
|
+
}
|
|
768
|
+
await (0, import_utils2.copyTempPackageToStorageAndLinkToNodeModules)(tempFile, tempPackageContentDir, packageName);
|
|
769
|
+
return this.add(name, { packageName }, true);
|
|
770
|
+
}
|
|
771
|
+
async update(options) {
|
|
772
|
+
if (options["url"]) {
|
|
773
|
+
options.compressedFileUrl = options["url"];
|
|
774
|
+
}
|
|
775
|
+
if (!options.name) {
|
|
776
|
+
const model = await this.repository.findOne({ filter: { packageName: options.packageName } });
|
|
777
|
+
options["name"] = model.name;
|
|
778
|
+
}
|
|
779
|
+
if (options.compressedFileUrl) {
|
|
780
|
+
await this.upgradeByCompressedFileUrl(options);
|
|
781
|
+
} else {
|
|
782
|
+
await this.upgradeByNpm(options);
|
|
783
|
+
}
|
|
784
|
+
const file = (0, import_node_path.resolve)(process.cwd(), "storage/app-upgrading");
|
|
785
|
+
await import_promises.default.writeFile(file, "", "utf-8");
|
|
786
|
+
await this.app.upgrade();
|
|
787
|
+
}
|
|
788
|
+
/**
|
|
789
|
+
* @internal
|
|
790
|
+
*/
|
|
791
|
+
async upgradeByNpm(values) {
|
|
792
|
+
var _a, _b, _c;
|
|
793
|
+
const name = values.name;
|
|
794
|
+
const plugin = this.get(name);
|
|
795
|
+
if (!this.has(name)) {
|
|
796
|
+
throw new Error(`plugin name [${name}] not exists`);
|
|
797
|
+
}
|
|
798
|
+
if (!plugin.options.packageName || !values.registry) {
|
|
799
|
+
throw new Error(`plugin name [${name}] not installed by npm`);
|
|
800
|
+
}
|
|
801
|
+
const version = (_a = values.version) == null ? void 0 : _a.trim();
|
|
802
|
+
const registry = ((_b = values.registry) == null ? void 0 : _b.trim()) || plugin.options.registry;
|
|
803
|
+
const authToken = ((_c = values.authToken) == null ? void 0 : _c.trim()) || plugin.options.authToken;
|
|
804
|
+
const { compressedFileUrl } = await (0, import_utils2.getPluginInfoByNpm)({
|
|
805
|
+
packageName: plugin.options.packageName,
|
|
806
|
+
registry,
|
|
807
|
+
authToken,
|
|
808
|
+
version
|
|
809
|
+
});
|
|
810
|
+
return this.upgradeByCompressedFileUrl({ compressedFileUrl, name, version, registry, authToken });
|
|
811
|
+
}
|
|
812
|
+
/**
|
|
813
|
+
* @internal
|
|
814
|
+
*/
|
|
815
|
+
async upgradeByCompressedFileUrl(options) {
|
|
816
|
+
const { name, compressedFileUrl, authToken } = options;
|
|
817
|
+
const data = await this.repository.findOne({ filter: { name } });
|
|
818
|
+
const { version } = await (0, import_utils2.updatePluginByCompressedFileUrl)({
|
|
819
|
+
compressedFileUrl,
|
|
820
|
+
packageName: data.packageName,
|
|
821
|
+
authToken
|
|
822
|
+
});
|
|
823
|
+
await this.add(name, { version, packageName: data.packageName }, true, true);
|
|
824
|
+
}
|
|
825
|
+
/**
|
|
826
|
+
* @internal
|
|
827
|
+
*/
|
|
828
|
+
getNameByPackageName(packageName) {
|
|
829
|
+
const prefixes = _PluginManager.getPluginPkgPrefix();
|
|
830
|
+
const prefix = prefixes.find((prefix2) => packageName.startsWith(prefix2));
|
|
831
|
+
if (!prefix) {
|
|
832
|
+
throw new Error(
|
|
833
|
+
`package name [${packageName}] invalid, just support ${prefixes.join(
|
|
834
|
+
", "
|
|
835
|
+
)}. You can modify process.env.PLUGIN_PACKAGE_PREFIX add more prefix.`
|
|
836
|
+
);
|
|
837
|
+
}
|
|
838
|
+
return packageName.replace(prefix, "");
|
|
839
|
+
}
|
|
840
|
+
async list(options = {}) {
|
|
841
|
+
const { locale = "en-US", isPreset = false } = options;
|
|
842
|
+
return Promise.all(
|
|
843
|
+
[...this.getAliases()].map((name) => {
|
|
844
|
+
const plugin = this.get(name);
|
|
845
|
+
if (!isPreset && plugin.options.isPreset) {
|
|
846
|
+
return;
|
|
847
|
+
}
|
|
848
|
+
return plugin.toJSON({ locale });
|
|
849
|
+
}).filter(Boolean)
|
|
850
|
+
);
|
|
851
|
+
}
|
|
852
|
+
/**
|
|
853
|
+
* @internal
|
|
854
|
+
*/
|
|
855
|
+
async getNpmVersionList(name) {
|
|
856
|
+
const plugin = this.get(name);
|
|
857
|
+
const npmInfo = await (0, import_utils2.getNpmInfo)(plugin.options.packageName, plugin.options.registry, plugin.options.authToken);
|
|
858
|
+
return Object.keys(npmInfo.versions);
|
|
859
|
+
}
|
|
860
|
+
/**
|
|
861
|
+
* @internal
|
|
862
|
+
*/
|
|
863
|
+
async loadPresetMigrations() {
|
|
864
|
+
const migrations = {
|
|
865
|
+
beforeLoad: [],
|
|
866
|
+
afterSync: [],
|
|
867
|
+
afterLoad: []
|
|
868
|
+
};
|
|
869
|
+
for (const [P, plugin] of this.getPlugins()) {
|
|
870
|
+
if (!plugin.isPreset) {
|
|
871
|
+
continue;
|
|
872
|
+
}
|
|
873
|
+
const { beforeLoad, afterSync, afterLoad } = await plugin.loadMigrations();
|
|
874
|
+
migrations.beforeLoad.push(...beforeLoad);
|
|
875
|
+
migrations.afterSync.push(...afterSync);
|
|
876
|
+
migrations.afterLoad.push(...afterLoad);
|
|
877
|
+
}
|
|
878
|
+
return {
|
|
879
|
+
beforeLoad: {
|
|
880
|
+
up: /* @__PURE__ */ __name(async () => {
|
|
881
|
+
this.app.logger.debug("run preset migrations(beforeLoad)");
|
|
882
|
+
const migrator = this.app.db.createMigrator({ migrations: migrations.beforeLoad });
|
|
883
|
+
await migrator.up();
|
|
884
|
+
}, "up")
|
|
885
|
+
},
|
|
886
|
+
afterSync: {
|
|
887
|
+
up: /* @__PURE__ */ __name(async () => {
|
|
888
|
+
this.app.logger.debug("run preset migrations(afterSync)");
|
|
889
|
+
const migrator = this.app.db.createMigrator({ migrations: migrations.afterSync });
|
|
890
|
+
await migrator.up();
|
|
891
|
+
}, "up")
|
|
892
|
+
},
|
|
893
|
+
afterLoad: {
|
|
894
|
+
up: /* @__PURE__ */ __name(async () => {
|
|
895
|
+
this.app.logger.debug("run preset migrations(afterLoad)");
|
|
896
|
+
const migrator = this.app.db.createMigrator({ migrations: migrations.afterLoad });
|
|
897
|
+
await migrator.up();
|
|
898
|
+
}, "up")
|
|
899
|
+
}
|
|
900
|
+
};
|
|
901
|
+
}
|
|
902
|
+
/**
|
|
903
|
+
* @internal
|
|
904
|
+
*/
|
|
905
|
+
async loadOtherMigrations() {
|
|
906
|
+
const migrations = {
|
|
907
|
+
beforeLoad: [],
|
|
908
|
+
afterSync: [],
|
|
909
|
+
afterLoad: []
|
|
910
|
+
};
|
|
911
|
+
for (const [P, plugin] of this.getPlugins()) {
|
|
912
|
+
if (plugin.isPreset) {
|
|
913
|
+
continue;
|
|
914
|
+
}
|
|
915
|
+
if (!plugin.enabled) {
|
|
916
|
+
continue;
|
|
917
|
+
}
|
|
918
|
+
const { beforeLoad, afterSync, afterLoad } = await plugin.loadMigrations();
|
|
919
|
+
migrations.beforeLoad.push(...beforeLoad);
|
|
920
|
+
migrations.afterSync.push(...afterSync);
|
|
921
|
+
migrations.afterLoad.push(...afterLoad);
|
|
922
|
+
}
|
|
923
|
+
return {
|
|
924
|
+
beforeLoad: {
|
|
925
|
+
up: /* @__PURE__ */ __name(async () => {
|
|
926
|
+
this.app.logger.debug("run others migrations(beforeLoad)");
|
|
927
|
+
const migrator = this.app.db.createMigrator({ migrations: migrations.beforeLoad });
|
|
928
|
+
await migrator.up();
|
|
929
|
+
}, "up")
|
|
930
|
+
},
|
|
931
|
+
afterSync: {
|
|
932
|
+
up: /* @__PURE__ */ __name(async () => {
|
|
933
|
+
this.app.logger.debug("run others migrations(afterSync)");
|
|
934
|
+
const migrator = this.app.db.createMigrator({ migrations: migrations.afterSync });
|
|
935
|
+
await migrator.up();
|
|
936
|
+
}, "up")
|
|
937
|
+
},
|
|
938
|
+
afterLoad: {
|
|
939
|
+
up: /* @__PURE__ */ __name(async () => {
|
|
940
|
+
this.app.logger.debug("run others migrations(afterLoad)");
|
|
941
|
+
const migrator = this.app.db.createMigrator({ migrations: migrations.afterLoad });
|
|
942
|
+
await migrator.up();
|
|
943
|
+
}, "up")
|
|
944
|
+
}
|
|
945
|
+
};
|
|
946
|
+
}
|
|
947
|
+
/**
|
|
948
|
+
* @internal
|
|
949
|
+
*/
|
|
950
|
+
async loadPresetPlugins() {
|
|
951
|
+
await this.initPresetPlugins();
|
|
952
|
+
await this.load();
|
|
953
|
+
}
|
|
954
|
+
async upgrade() {
|
|
955
|
+
this.app.logger.info("run upgrade");
|
|
956
|
+
const toBeUpdated = [];
|
|
957
|
+
for (const [P, plugin] of this.getPlugins()) {
|
|
958
|
+
if (plugin.state.upgraded) {
|
|
959
|
+
continue;
|
|
960
|
+
}
|
|
961
|
+
if (!plugin.enabled) {
|
|
962
|
+
continue;
|
|
963
|
+
}
|
|
964
|
+
if (!plugin.isPreset && !plugin.installed) {
|
|
965
|
+
this.app.logger.info(`install built-in plugin [${plugin.name}]`);
|
|
966
|
+
await plugin.install();
|
|
967
|
+
toBeUpdated.push(plugin.name);
|
|
968
|
+
}
|
|
969
|
+
this.app.logger.debug(`upgrade plugin [${plugin.name}]`);
|
|
970
|
+
await plugin.upgrade();
|
|
971
|
+
for (const feature of plugin.featureInstances) {
|
|
972
|
+
await feature.upgrade();
|
|
973
|
+
}
|
|
974
|
+
plugin.state.upgraded = true;
|
|
975
|
+
}
|
|
976
|
+
await this.repository.update({
|
|
977
|
+
filter: {
|
|
978
|
+
name: toBeUpdated
|
|
979
|
+
},
|
|
980
|
+
values: {
|
|
981
|
+
installed: true
|
|
982
|
+
}
|
|
983
|
+
});
|
|
984
|
+
}
|
|
985
|
+
/**
|
|
986
|
+
* @internal
|
|
987
|
+
*/
|
|
988
|
+
async initOtherPlugins() {
|
|
989
|
+
if (this["_initOtherPlugins"]) {
|
|
990
|
+
return;
|
|
991
|
+
}
|
|
992
|
+
await this.repository.init();
|
|
993
|
+
this["_initOtherPlugins"] = true;
|
|
994
|
+
}
|
|
995
|
+
/**
|
|
996
|
+
* @internal
|
|
997
|
+
*/
|
|
998
|
+
async initPresetPlugins() {
|
|
999
|
+
if (this["_initPresetPlugins"]) {
|
|
1000
|
+
return;
|
|
1001
|
+
}
|
|
1002
|
+
for (const plugin of this.options.plugins) {
|
|
1003
|
+
const [p, opts = {}] = Array.isArray(plugin) ? plugin : [plugin];
|
|
1004
|
+
await this.add(p, { enabled: true, isPreset: true, ...opts });
|
|
1005
|
+
}
|
|
1006
|
+
this["_initPresetPlugins"] = true;
|
|
1007
|
+
}
|
|
1008
|
+
/**
|
|
1009
|
+
* @internal
|
|
1010
|
+
*/
|
|
1011
|
+
async initRuntimePlugins() {
|
|
1012
|
+
if (this["_initRuntimePlugins"]) {
|
|
1013
|
+
return;
|
|
1014
|
+
}
|
|
1015
|
+
if (process.env.RUNTIME_PLUGINS) {
|
|
1016
|
+
const runtimePlugins = process.env.RUNTIME_PLUGINS.split(",");
|
|
1017
|
+
for (const plugin of runtimePlugins) {
|
|
1018
|
+
await this.add(plugin, { enabled: true });
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
this["_initRuntimePlugins"] = true;
|
|
1022
|
+
}
|
|
1023
|
+
};
|
|
1024
|
+
__name(_PluginManager, "PluginManager");
|
|
1025
|
+
let PluginManager = _PluginManager;
|
|
1026
|
+
var plugin_manager_default = PluginManager;
|
|
1027
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1028
|
+
0 && (module.exports = {
|
|
1029
|
+
AddPresetError,
|
|
1030
|
+
PluginManager,
|
|
1031
|
+
sleep
|
|
1032
|
+
});
|