@serenityjs/plugins 0.3.1-beta-20240604185231 → 0.3.1-beta-20240606042832
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/dist/index.d.ts +22 -6
- package/dist/index.js +127 -96
- package/package.json +6 -6
package/dist/index.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ interface PluginEvents {
|
|
|
11
11
|
ready: [];
|
|
12
12
|
}
|
|
13
13
|
interface PluginModule {
|
|
14
|
-
|
|
14
|
+
onInitialize?(...arguments_: Array<unknown>): void;
|
|
15
15
|
onStartup?(...arguments_: Array<unknown>): void;
|
|
16
16
|
onShutdown?(...arguments_: Array<unknown>): void;
|
|
17
17
|
}
|
|
@@ -34,20 +34,36 @@ declare class Plugins extends Emitter<PluginEvents> {
|
|
|
34
34
|
*/
|
|
35
35
|
readonly path: string;
|
|
36
36
|
/**
|
|
37
|
-
*
|
|
37
|
+
* Whether the plugins are enabled.
|
|
38
38
|
*/
|
|
39
|
-
readonly
|
|
39
|
+
readonly enabled: boolean;
|
|
40
40
|
/**
|
|
41
|
-
*
|
|
41
|
+
* A collection registry of all plugins.
|
|
42
42
|
*/
|
|
43
|
-
|
|
43
|
+
readonly entries: Map<string, Plugin>;
|
|
44
44
|
/**
|
|
45
45
|
* Constructs a new plugins instance.
|
|
46
46
|
*
|
|
47
47
|
* @param serenity - The serenity instance.
|
|
48
48
|
*/
|
|
49
49
|
constructor(path: string, enabled?: boolean);
|
|
50
|
-
|
|
50
|
+
/**
|
|
51
|
+
* Initializes all plugins.
|
|
52
|
+
*/
|
|
53
|
+
initialize(...arguments_: Array<unknown>): Promise<void>;
|
|
54
|
+
/**
|
|
55
|
+
* Starts all plugins.
|
|
56
|
+
*/
|
|
57
|
+
start(...arguments_: Array<unknown>): Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
* Stops all plugins.
|
|
60
|
+
*/
|
|
61
|
+
stop(...arguments_: Array<unknown>): Promise<void>;
|
|
62
|
+
/**
|
|
63
|
+
* Imports a plugin module.
|
|
64
|
+
* @param name - The name of the plugin.
|
|
65
|
+
* @returns The plugin module.
|
|
66
|
+
*/
|
|
51
67
|
import<T = PluginModule>(name: string): T;
|
|
52
68
|
}
|
|
53
69
|
|
package/dist/index.js
CHANGED
|
@@ -78,19 +78,19 @@ var Plugins = class extends import_emitter.default {
|
|
|
78
78
|
/**
|
|
79
79
|
* The logger instance.
|
|
80
80
|
*/
|
|
81
|
-
logger;
|
|
81
|
+
logger = new import_logger.Logger("Plugins", import_logger.LoggerColors.CyanBright);
|
|
82
82
|
/**
|
|
83
83
|
* The path to the plugins folder.
|
|
84
84
|
*/
|
|
85
85
|
path;
|
|
86
86
|
/**
|
|
87
|
-
*
|
|
87
|
+
* Whether the plugins are enabled.
|
|
88
88
|
*/
|
|
89
|
-
|
|
89
|
+
enabled;
|
|
90
90
|
/**
|
|
91
|
-
*
|
|
91
|
+
* A collection registry of all plugins.
|
|
92
92
|
*/
|
|
93
|
-
|
|
93
|
+
entries = /* @__PURE__ */ new Map();
|
|
94
94
|
/**
|
|
95
95
|
* Constructs a new plugins instance.
|
|
96
96
|
*
|
|
@@ -98,125 +98,156 @@ var Plugins = class extends import_emitter.default {
|
|
|
98
98
|
*/
|
|
99
99
|
constructor(path, enabled = true) {
|
|
100
100
|
super();
|
|
101
|
-
this.logger = new import_logger.Logger("Plugins", import_logger.LoggerColors.CyanBright);
|
|
102
101
|
this.path = (0, import_node_path.resolve)(import_node_process.default.cwd(), path);
|
|
103
|
-
this.
|
|
104
|
-
|
|
105
|
-
|
|
102
|
+
this.enabled = enabled;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Initializes all plugins.
|
|
106
|
+
*/
|
|
107
|
+
async initialize(...arguments_) {
|
|
108
|
+
if (!this.enabled) {
|
|
109
|
+
this.logger.warn("Plugins are set to be disabled.");
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
if (!(0, import_node_fs.existsSync)(this.path)) {
|
|
113
|
+
try {
|
|
114
|
+
await (0, import_node_fs.mkdirSync)(this.path);
|
|
115
|
+
} catch (reason) {
|
|
116
|
+
this.logger.error(`Failed to create plugins directory!`, reason);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
const directories = (0, import_node_fs.readdirSync)(this.path, {
|
|
120
|
+
withFileTypes: true
|
|
121
|
+
}).filter((dirent) => dirent.isDirectory());
|
|
122
|
+
for (const directory of directories) {
|
|
123
|
+
const path = (0, import_node_path.resolve)(this.path, directory.name);
|
|
124
|
+
if (!(0, import_node_fs.existsSync)((0, import_node_path.resolve)(path, "plugin.json"))) {
|
|
106
125
|
try {
|
|
107
|
-
(
|
|
108
|
-
|
|
109
|
-
|
|
126
|
+
const config2 = PLUGIN_TEMPLATE.replace("plugin-name", directory.name);
|
|
127
|
+
(0, import_node_fs.writeFileSync)((0, import_node_path.resolve)(path, "plugin.json"), config2);
|
|
128
|
+
} catch {
|
|
129
|
+
this.logger.error(
|
|
130
|
+
`Failed to create plugin.json for ${directory.name}`
|
|
131
|
+
);
|
|
110
132
|
}
|
|
111
133
|
}
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
if (!(0, import_node_fs.existsSync)((0, import_node_path.resolve)(path2, "plugin.json"))) {
|
|
134
|
+
const config = JSON.parse(
|
|
135
|
+
(0, import_node_fs.readFileSync)((0, import_node_path.resolve)(path, "plugin.json"), "utf8")
|
|
136
|
+
);
|
|
137
|
+
if (config.node === true) {
|
|
138
|
+
if (!(0, import_node_fs.existsSync)((0, import_node_path.resolve)(path, "package.json"))) {
|
|
118
139
|
try {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
directory.name
|
|
122
|
-
);
|
|
123
|
-
(0, import_node_fs.writeFileSync)((0, import_node_path.resolve)(path2, "plugin.json"), config2);
|
|
124
|
-
} catch {
|
|
140
|
+
(0, import_node_child_process.execSync)("npm init -y", { stdio: "ignore", cwd: path });
|
|
141
|
+
} catch (reason) {
|
|
125
142
|
this.logger.error(
|
|
126
|
-
`Failed to create
|
|
143
|
+
`Failed to create package.json for ${config.name}@${config.version}!`,
|
|
144
|
+
reason
|
|
127
145
|
);
|
|
128
146
|
}
|
|
129
147
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
if (!(0, import_node_fs.existsSync)((0, import_node_path.resolve)(path2, "package-lock.json"))) {
|
|
145
|
-
try {
|
|
146
|
-
this.logger.info(
|
|
147
|
-
`Installing dependencies for ${config.name}@${config.version}...`
|
|
148
|
-
);
|
|
149
|
-
(0, import_node_child_process.execSync)("npm install", { stdio: "ignore", cwd: path2 });
|
|
150
|
-
} catch (reason) {
|
|
151
|
-
this.logger.error(
|
|
152
|
-
`Failed to install dependencies for ${config.name}@${config.version}!`,
|
|
153
|
-
reason
|
|
154
|
-
);
|
|
155
|
-
}
|
|
148
|
+
if (!(0, import_node_fs.existsSync)((0, import_node_path.resolve)(path, "package-lock.json"))) {
|
|
149
|
+
try {
|
|
150
|
+
this.logger.info(
|
|
151
|
+
`Installing dependencies for ${config.name}@${config.version}...`
|
|
152
|
+
);
|
|
153
|
+
await (0, import_node_child_process.execSync)("npm install", { stdio: "ignore", cwd: path });
|
|
154
|
+
} catch (reason) {
|
|
155
|
+
this.logger.error(
|
|
156
|
+
`Failed to install dependencies for ${config.name}@${config.version}!`,
|
|
157
|
+
reason
|
|
158
|
+
);
|
|
156
159
|
}
|
|
157
160
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
161
|
+
}
|
|
162
|
+
if (config.typescript === true) {
|
|
163
|
+
if (!(0, import_node_fs.existsSync)((0, import_node_path.resolve)(path, "tsconfig.json"))) {
|
|
164
|
+
try {
|
|
165
|
+
await (0, import_node_fs.writeFileSync)(
|
|
166
|
+
(0, import_node_path.resolve)(path, "tsconfig.json"),
|
|
167
|
+
TSCONFIG_TEMPLATE
|
|
168
|
+
);
|
|
169
|
+
await (0, import_node_child_process.execSync)("npm pkg set scripts.build=tsc", {
|
|
170
|
+
stdio: "ignore",
|
|
171
|
+
cwd: path
|
|
172
|
+
});
|
|
173
|
+
} catch (reason) {
|
|
174
|
+
this.logger.error(
|
|
175
|
+
`Failed to create tsconfig.json for ${config.name}@${config.version}`,
|
|
176
|
+
reason
|
|
177
|
+
);
|
|
172
178
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
179
|
+
}
|
|
180
|
+
if (!(0, import_node_fs.existsSync)((0, import_node_path.resolve)(path, "dist")) || config.mode === "development") {
|
|
181
|
+
try {
|
|
182
|
+
this.logger.info(
|
|
183
|
+
`Building typescript files for ${config.name}@${config.version}...`
|
|
184
|
+
);
|
|
185
|
+
await (0, import_node_child_process.execSync)("npm run build", { stdio: "ignore", cwd: path });
|
|
186
|
+
} catch (reason) {
|
|
187
|
+
this.logger.error(
|
|
188
|
+
`Failed to build typescript files for ${config.name}@${config.version}!`,
|
|
189
|
+
reason
|
|
190
|
+
);
|
|
185
191
|
}
|
|
186
192
|
}
|
|
187
193
|
}
|
|
188
|
-
void this.load(directories);
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
async load(directories) {
|
|
192
|
-
for await (const directory of directories) {
|
|
193
194
|
try {
|
|
194
|
-
const
|
|
195
|
-
const
|
|
196
|
-
(0, import_node_fs.readFileSync)((0, import_node_path.resolve)(
|
|
195
|
+
const path2 = (0, import_node_path.resolve)(this.path, directory.name);
|
|
196
|
+
const config2 = JSON.parse(
|
|
197
|
+
(0, import_node_fs.readFileSync)((0, import_node_path.resolve)(path2, "plugin.json"), "utf8")
|
|
197
198
|
);
|
|
198
|
-
const instance = await import(`file://${(0, import_node_path.resolve)(
|
|
199
|
+
const instance = await import(`file://${(0, import_node_path.resolve)(path2, config2.entry)}`);
|
|
199
200
|
const module2 = instance["default"];
|
|
200
|
-
const adjustedColor =
|
|
201
|
+
const adjustedColor = config2.color.split("_").map((part) => {
|
|
201
202
|
return part.charAt(0).toUpperCase() + part.slice(1);
|
|
202
203
|
}).join("");
|
|
203
204
|
const color = import_logger.LoggerColors[adjustedColor] ?? import_logger.LoggerColors.White;
|
|
204
|
-
const logger = new import_logger.Logger(`${
|
|
205
|
-
const plugin = { config, module: module2, logger };
|
|
206
|
-
this.entries.set(
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
module2.onRegister?.(plugin);
|
|
210
|
-
} catch {
|
|
205
|
+
const logger = new import_logger.Logger(`${config2.name}@${config2.version}`, color);
|
|
206
|
+
const plugin = { config: config2, module: module2, logger };
|
|
207
|
+
this.entries.set(config2.name, plugin);
|
|
208
|
+
if (module2.onInitialize) {
|
|
209
|
+
await module2.onInitialize(...arguments_, plugin);
|
|
211
210
|
}
|
|
212
211
|
this.emit("register", plugin);
|
|
213
212
|
} catch (reason) {
|
|
214
213
|
this.logger.error(`Failed to load plugin! ${directory.name}`, reason);
|
|
215
214
|
}
|
|
216
215
|
}
|
|
217
|
-
|
|
218
|
-
|
|
216
|
+
const plugins = [...this.entries.values()].map(
|
|
217
|
+
(plugin) => `${plugin.config.name}@${plugin.config.version}`
|
|
218
|
+
);
|
|
219
|
+
const s = plugins.length > 1 ? "s" : "";
|
|
220
|
+
this.logger.success(
|
|
221
|
+
`Successfully initialized a total of ${plugins.length} plugin${s}! ${plugins.join(
|
|
222
|
+
", "
|
|
223
|
+
)}`
|
|
224
|
+
);
|
|
219
225
|
}
|
|
226
|
+
/**
|
|
227
|
+
* Starts all plugins.
|
|
228
|
+
*/
|
|
229
|
+
async start(...arguments_) {
|
|
230
|
+
for await (const plugin of this.entries.values()) {
|
|
231
|
+
if (plugin.module.onStartup) {
|
|
232
|
+
await plugin.module.onStartup(...arguments_, plugin);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Stops all plugins.
|
|
238
|
+
*/
|
|
239
|
+
async stop(...arguments_) {
|
|
240
|
+
for await (const plugin of this.entries.values()) {
|
|
241
|
+
if (plugin.module.onShutdown) {
|
|
242
|
+
await plugin.module.onShutdown(...arguments_, plugin);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Imports a plugin module.
|
|
248
|
+
* @param name - The name of the plugin.
|
|
249
|
+
* @returns The plugin module.
|
|
250
|
+
*/
|
|
220
251
|
import(name) {
|
|
221
252
|
const plugin = this.entries.get(name);
|
|
222
253
|
if (!plugin) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@serenityjs/plugins",
|
|
3
|
-
"version": "0.3.1-beta-
|
|
3
|
+
"version": "0.3.1-beta-20240606042832",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"repository": "https://github.com/SerenityJS/serenity",
|
|
@@ -18,9 +18,9 @@
|
|
|
18
18
|
"preset": "@serenityjs/jest-presets/jest/node"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
|
-
"@serenityjs/eslint-config": "0.3.1-beta-
|
|
22
|
-
"@serenityjs/jest-presets": "0.3.1-beta-
|
|
23
|
-
"@serenityjs/typescript-config": "0.3.1-beta-
|
|
21
|
+
"@serenityjs/eslint-config": "0.3.1-beta-20240606042832",
|
|
22
|
+
"@serenityjs/jest-presets": "0.3.1-beta-20240606042832",
|
|
23
|
+
"@serenityjs/typescript-config": "0.3.1-beta-20240606042832",
|
|
24
24
|
"@types/jest": "^29.5.12",
|
|
25
25
|
"@types/node": "^20.11.24",
|
|
26
26
|
"jest": "^29.7.0",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"typescript": "^5.4.2"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@serenityjs/emitter": "0.3.1-beta-
|
|
32
|
-
"@serenityjs/logger": "0.3.1-beta-
|
|
31
|
+
"@serenityjs/emitter": "0.3.1-beta-20240606042832",
|
|
32
|
+
"@serenityjs/logger": "0.3.1-beta-20240606042832"
|
|
33
33
|
}
|
|
34
34
|
}
|