@twin.org/modules 0.0.2-next.5 → 0.0.2-next.7
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/cjs/index.cjs
CHANGED
|
@@ -5,7 +5,6 @@ var core = require('@twin.org/core');
|
|
|
5
5
|
|
|
6
6
|
// Copyright 2024 IOTA Stiftung.
|
|
7
7
|
// SPDX-License-Identifier: Apache-2.0.
|
|
8
|
-
/* eslint-disable promise/prefer-await-to-then */
|
|
9
8
|
/**
|
|
10
9
|
* Helper functions for modules.
|
|
11
10
|
*/
|
|
@@ -14,6 +13,13 @@ class ModuleHelper {
|
|
|
14
13
|
* Runtime name for the class.
|
|
15
14
|
*/
|
|
16
15
|
static CLASS_NAME = "ModuleHelper";
|
|
16
|
+
/**
|
|
17
|
+
* Override the import function for modules.
|
|
18
|
+
* @param overrideImport The override import function.
|
|
19
|
+
*/
|
|
20
|
+
static overrideImport(overrideImport) {
|
|
21
|
+
core.SharedStore.set("overrideImport", overrideImport);
|
|
22
|
+
}
|
|
17
23
|
/**
|
|
18
24
|
* Get the module entry.
|
|
19
25
|
* @param module The module.
|
|
@@ -24,7 +30,16 @@ class ModuleHelper {
|
|
|
24
30
|
static async getModuleEntry(module, entry) {
|
|
25
31
|
let moduleInstance;
|
|
26
32
|
try {
|
|
27
|
-
|
|
33
|
+
let useDefault = true;
|
|
34
|
+
const overrideImport = core.SharedStore.get("overrideImport");
|
|
35
|
+
if (!core.Is.empty(overrideImport)) {
|
|
36
|
+
const overrideResult = await overrideImport(module);
|
|
37
|
+
moduleInstance = overrideResult.module;
|
|
38
|
+
useDefault = overrideResult.useDefault;
|
|
39
|
+
}
|
|
40
|
+
if (useDefault) {
|
|
41
|
+
moduleInstance = await import(module);
|
|
42
|
+
}
|
|
28
43
|
}
|
|
29
44
|
catch (err) {
|
|
30
45
|
throw new core.GeneralError(ModuleHelper.CLASS_NAME, "moduleNotFound", {
|
|
@@ -32,7 +47,7 @@ class ModuleHelper {
|
|
|
32
47
|
entry
|
|
33
48
|
}, core.BaseError.fromError(err));
|
|
34
49
|
}
|
|
35
|
-
const moduleEntry = moduleInstance[entry];
|
|
50
|
+
const moduleEntry = moduleInstance?.[entry];
|
|
36
51
|
if (core.Is.empty(moduleEntry)) {
|
|
37
52
|
throw new core.GeneralError(ModuleHelper.CLASS_NAME, "entryNotFound", {
|
|
38
53
|
module,
|
|
@@ -159,6 +174,22 @@ class ModuleHelper {
|
|
|
159
174
|
});
|
|
160
175
|
});
|
|
161
176
|
}
|
|
177
|
+
/**
|
|
178
|
+
* Check if a module is a local module.
|
|
179
|
+
* @param name The name of the module.
|
|
180
|
+
* @returns True if the module is local, false otherwise.
|
|
181
|
+
*/
|
|
182
|
+
static isLocalModule(name) {
|
|
183
|
+
return name.startsWith(".") || name.startsWith("/");
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Check if a module is a relative module.
|
|
187
|
+
* @param name The name of the module.
|
|
188
|
+
* @returns True if the module is relative, false otherwise.
|
|
189
|
+
*/
|
|
190
|
+
static isRelativeModule(name) {
|
|
191
|
+
return name.startsWith(".");
|
|
192
|
+
}
|
|
162
193
|
}
|
|
163
194
|
|
|
164
195
|
exports.ModuleHelper = ModuleHelper;
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { Worker } from 'node:worker_threads';
|
|
2
|
-
import { GeneralError, BaseError
|
|
2
|
+
import { SharedStore, Is, GeneralError, BaseError } from '@twin.org/core';
|
|
3
3
|
|
|
4
4
|
// Copyright 2024 IOTA Stiftung.
|
|
5
5
|
// SPDX-License-Identifier: Apache-2.0.
|
|
6
|
-
/* eslint-disable promise/prefer-await-to-then */
|
|
7
6
|
/**
|
|
8
7
|
* Helper functions for modules.
|
|
9
8
|
*/
|
|
@@ -12,6 +11,13 @@ class ModuleHelper {
|
|
|
12
11
|
* Runtime name for the class.
|
|
13
12
|
*/
|
|
14
13
|
static CLASS_NAME = "ModuleHelper";
|
|
14
|
+
/**
|
|
15
|
+
* Override the import function for modules.
|
|
16
|
+
* @param overrideImport The override import function.
|
|
17
|
+
*/
|
|
18
|
+
static overrideImport(overrideImport) {
|
|
19
|
+
SharedStore.set("overrideImport", overrideImport);
|
|
20
|
+
}
|
|
15
21
|
/**
|
|
16
22
|
* Get the module entry.
|
|
17
23
|
* @param module The module.
|
|
@@ -22,7 +28,16 @@ class ModuleHelper {
|
|
|
22
28
|
static async getModuleEntry(module, entry) {
|
|
23
29
|
let moduleInstance;
|
|
24
30
|
try {
|
|
25
|
-
|
|
31
|
+
let useDefault = true;
|
|
32
|
+
const overrideImport = SharedStore.get("overrideImport");
|
|
33
|
+
if (!Is.empty(overrideImport)) {
|
|
34
|
+
const overrideResult = await overrideImport(module);
|
|
35
|
+
moduleInstance = overrideResult.module;
|
|
36
|
+
useDefault = overrideResult.useDefault;
|
|
37
|
+
}
|
|
38
|
+
if (useDefault) {
|
|
39
|
+
moduleInstance = await import(module);
|
|
40
|
+
}
|
|
26
41
|
}
|
|
27
42
|
catch (err) {
|
|
28
43
|
throw new GeneralError(ModuleHelper.CLASS_NAME, "moduleNotFound", {
|
|
@@ -30,7 +45,7 @@ class ModuleHelper {
|
|
|
30
45
|
entry
|
|
31
46
|
}, BaseError.fromError(err));
|
|
32
47
|
}
|
|
33
|
-
const moduleEntry = moduleInstance[entry];
|
|
48
|
+
const moduleEntry = moduleInstance?.[entry];
|
|
34
49
|
if (Is.empty(moduleEntry)) {
|
|
35
50
|
throw new GeneralError(ModuleHelper.CLASS_NAME, "entryNotFound", {
|
|
36
51
|
module,
|
|
@@ -157,6 +172,22 @@ class ModuleHelper {
|
|
|
157
172
|
});
|
|
158
173
|
});
|
|
159
174
|
}
|
|
175
|
+
/**
|
|
176
|
+
* Check if a module is a local module.
|
|
177
|
+
* @param name The name of the module.
|
|
178
|
+
* @returns True if the module is local, false otherwise.
|
|
179
|
+
*/
|
|
180
|
+
static isLocalModule(name) {
|
|
181
|
+
return name.startsWith(".") || name.startsWith("/");
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Check if a module is a relative module.
|
|
185
|
+
* @param name The name of the module.
|
|
186
|
+
* @returns True if the module is relative, false otherwise.
|
|
187
|
+
*/
|
|
188
|
+
static isRelativeModule(name) {
|
|
189
|
+
return name.startsWith(".");
|
|
190
|
+
}
|
|
160
191
|
}
|
|
161
192
|
|
|
162
193
|
export { ModuleHelper };
|
|
@@ -6,6 +6,14 @@ export declare class ModuleHelper {
|
|
|
6
6
|
* Runtime name for the class.
|
|
7
7
|
*/
|
|
8
8
|
static readonly CLASS_NAME: string;
|
|
9
|
+
/**
|
|
10
|
+
* Override the import function for modules.
|
|
11
|
+
* @param overrideImport The override import function.
|
|
12
|
+
*/
|
|
13
|
+
static overrideImport(overrideImport: (moduleName: string) => Promise<{
|
|
14
|
+
module?: unknown;
|
|
15
|
+
useDefault: boolean;
|
|
16
|
+
}>): void;
|
|
9
17
|
/**
|
|
10
18
|
* Get the module entry.
|
|
11
19
|
* @param module The module.
|
|
@@ -40,4 +48,16 @@ export declare class ModuleHelper {
|
|
|
40
48
|
* @throws GeneralError if executing the module entry failed.
|
|
41
49
|
*/
|
|
42
50
|
static execModuleMethodThread<T>(module: string, method: string, args?: unknown[]): Promise<T>;
|
|
51
|
+
/**
|
|
52
|
+
* Check if a module is a local module.
|
|
53
|
+
* @param name The name of the module.
|
|
54
|
+
* @returns True if the module is local, false otherwise.
|
|
55
|
+
*/
|
|
56
|
+
static isLocalModule(name: string): boolean;
|
|
57
|
+
/**
|
|
58
|
+
* Check if a module is a relative module.
|
|
59
|
+
* @param name The name of the module.
|
|
60
|
+
* @returns True if the module is relative, false otherwise.
|
|
61
|
+
*/
|
|
62
|
+
static isRelativeModule(name: string): boolean;
|
|
43
63
|
}
|
package/docs/changelog.md
CHANGED
|
@@ -1,5 +1,41 @@
|
|
|
1
1
|
# @twin.org/modules - Changelog
|
|
2
2
|
|
|
3
|
+
## [0.0.2-next.7](https://github.com/twinfoundation/framework/compare/modules-v0.0.2-next.6...modules-v0.0.2-next.7) (2025-08-29)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* eslint migration to flat config ([74427d7](https://github.com/twinfoundation/framework/commit/74427d78d342167f7850e49ab87269326355befe))
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Dependencies
|
|
12
|
+
|
|
13
|
+
* The following workspace dependencies were updated
|
|
14
|
+
* dependencies
|
|
15
|
+
* @twin.org/core bumped from 0.0.2-next.6 to 0.0.2-next.7
|
|
16
|
+
* @twin.org/nameof bumped from 0.0.2-next.6 to 0.0.2-next.7
|
|
17
|
+
* devDependencies
|
|
18
|
+
* @twin.org/nameof-transformer bumped from 0.0.2-next.6 to 0.0.2-next.7
|
|
19
|
+
* @twin.org/nameof-vitest-plugin bumped from 0.0.2-next.6 to 0.0.2-next.7
|
|
20
|
+
|
|
21
|
+
## [0.0.2-next.6](https://github.com/twinfoundation/framework/compare/modules-v0.0.2-next.5...modules-v0.0.2-next.6) (2025-08-27)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
### Features
|
|
25
|
+
|
|
26
|
+
* provide module helper override ([e998a64](https://github.com/twinfoundation/framework/commit/e998a64842cfd18693a14444be33b084fef2bb90))
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
### Dependencies
|
|
30
|
+
|
|
31
|
+
* The following workspace dependencies were updated
|
|
32
|
+
* dependencies
|
|
33
|
+
* @twin.org/core bumped from 0.0.2-next.5 to 0.0.2-next.6
|
|
34
|
+
* @twin.org/nameof bumped from 0.0.2-next.5 to 0.0.2-next.6
|
|
35
|
+
* devDependencies
|
|
36
|
+
* @twin.org/nameof-transformer bumped from 0.0.2-next.5 to 0.0.2-next.6
|
|
37
|
+
* @twin.org/nameof-vitest-plugin bumped from 0.0.2-next.5 to 0.0.2-next.6
|
|
38
|
+
|
|
3
39
|
## [0.0.2-next.5](https://github.com/twinfoundation/framework/compare/modules-v0.0.2-next.4...modules-v0.0.2-next.5) (2025-08-19)
|
|
4
40
|
|
|
5
41
|
|
|
@@ -22,6 +22,26 @@ Runtime name for the class.
|
|
|
22
22
|
|
|
23
23
|
## Methods
|
|
24
24
|
|
|
25
|
+
### overrideImport()
|
|
26
|
+
|
|
27
|
+
> `static` **overrideImport**(`overrideImport`): `void`
|
|
28
|
+
|
|
29
|
+
Override the import function for modules.
|
|
30
|
+
|
|
31
|
+
#### Parameters
|
|
32
|
+
|
|
33
|
+
##### overrideImport
|
|
34
|
+
|
|
35
|
+
(`moduleName`) => `Promise`\<\{ `module?`: `unknown`; `useDefault`: `boolean`; \}\>
|
|
36
|
+
|
|
37
|
+
The override import function.
|
|
38
|
+
|
|
39
|
+
#### Returns
|
|
40
|
+
|
|
41
|
+
`void`
|
|
42
|
+
|
|
43
|
+
***
|
|
44
|
+
|
|
25
45
|
### getModuleEntry()
|
|
26
46
|
|
|
27
47
|
> `static` **getModuleEntry**\<`T`\>(`module`, `entry`): `Promise`\<`T`\>
|
|
@@ -183,3 +203,47 @@ The result of the method execution.
|
|
|
183
203
|
#### Throws
|
|
184
204
|
|
|
185
205
|
GeneralError if executing the module entry failed.
|
|
206
|
+
|
|
207
|
+
***
|
|
208
|
+
|
|
209
|
+
### isLocalModule()
|
|
210
|
+
|
|
211
|
+
> `static` **isLocalModule**(`name`): `boolean`
|
|
212
|
+
|
|
213
|
+
Check if a module is a local module.
|
|
214
|
+
|
|
215
|
+
#### Parameters
|
|
216
|
+
|
|
217
|
+
##### name
|
|
218
|
+
|
|
219
|
+
`string`
|
|
220
|
+
|
|
221
|
+
The name of the module.
|
|
222
|
+
|
|
223
|
+
#### Returns
|
|
224
|
+
|
|
225
|
+
`boolean`
|
|
226
|
+
|
|
227
|
+
True if the module is local, false otherwise.
|
|
228
|
+
|
|
229
|
+
***
|
|
230
|
+
|
|
231
|
+
### isRelativeModule()
|
|
232
|
+
|
|
233
|
+
> `static` **isRelativeModule**(`name`): `boolean`
|
|
234
|
+
|
|
235
|
+
Check if a module is a relative module.
|
|
236
|
+
|
|
237
|
+
#### Parameters
|
|
238
|
+
|
|
239
|
+
##### name
|
|
240
|
+
|
|
241
|
+
`string`
|
|
242
|
+
|
|
243
|
+
The name of the module.
|
|
244
|
+
|
|
245
|
+
#### Returns
|
|
246
|
+
|
|
247
|
+
`boolean`
|
|
248
|
+
|
|
249
|
+
True if the module is relative, false otherwise.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@twin.org/modules",
|
|
3
|
-
"version": "0.0.2-next.
|
|
3
|
+
"version": "0.0.2-next.7",
|
|
4
4
|
"description": "Helper classes for loading and executing from modules",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -14,8 +14,8 @@
|
|
|
14
14
|
"node": ">=20.0.0"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@twin.org/core": "0.0.2-next.
|
|
18
|
-
"@twin.org/nameof": "0.0.2-next.
|
|
17
|
+
"@twin.org/core": "0.0.2-next.7",
|
|
18
|
+
"@twin.org/nameof": "0.0.2-next.7"
|
|
19
19
|
},
|
|
20
20
|
"main": "./dist/cjs/index.cjs",
|
|
21
21
|
"module": "./dist/esm/index.mjs",
|