@leonid-shutov/uncommonjs 1.0.0-alpha.0 → 1.0.0-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.
- package/lib/application.js +8 -2
- package/lib/loader.js +2 -1
- package/package.json +1 -1
- package/uncommon.js +2 -1
package/lib/application.js
CHANGED
|
@@ -5,7 +5,7 @@ const path = require('node:path');
|
|
|
5
5
|
const vm = require('node:vm');
|
|
6
6
|
const errors = require('./errors.js');
|
|
7
7
|
const { loadModules } = require('./deps.js');
|
|
8
|
-
const { loadDir } = require('./loader.js');
|
|
8
|
+
const { loadDir, loadFile } = require('./loader.js');
|
|
9
9
|
|
|
10
10
|
const getDefaultApplicationPath = () => path.join(process.cwd(), 'application');
|
|
11
11
|
|
|
@@ -14,6 +14,11 @@ const readLayers = (applicationPath) =>
|
|
|
14
14
|
.readFile(path.join(applicationPath, '.layers'), 'utf8')
|
|
15
15
|
.then((data) => data.split(/[\r\n\s]+/).filter((s) => s.length !== 0));
|
|
16
16
|
|
|
17
|
+
const loader = (context) => (layer) => {
|
|
18
|
+
if (layer.endsWith('.js')) return loadFile(context, context, layer);
|
|
19
|
+
else return loadDir(context, context, layer);
|
|
20
|
+
};
|
|
21
|
+
|
|
17
22
|
const loadApplication = async (sandbox = {}, options) => {
|
|
18
23
|
Object.assign(sandbox, errors);
|
|
19
24
|
const applicationPath = options?.path ?? getDefaultApplicationPath();
|
|
@@ -21,8 +26,9 @@ const loadApplication = async (sandbox = {}, options) => {
|
|
|
21
26
|
const [modules, layers] = await Promise.all(promises);
|
|
22
27
|
Object.assign(sandbox, modules);
|
|
23
28
|
const context = vm.createContext(sandbox);
|
|
24
|
-
const load =
|
|
29
|
+
const load = loader(context);
|
|
25
30
|
for (const layer of layers) await load(path.join(applicationPath, layer));
|
|
31
|
+
Object.assign(sandbox, { application: sandbox });
|
|
26
32
|
return sandbox;
|
|
27
33
|
};
|
|
28
34
|
|
package/lib/loader.js
CHANGED
|
@@ -21,6 +21,7 @@ const orderFiles = (files) => {
|
|
|
21
21
|
const removeIndex = (key) => key.replace(/^\d+-/, '');
|
|
22
22
|
|
|
23
23
|
const loadFile = async (context, container, filePath, key) => {
|
|
24
|
+
key ??= path.basename(filePath, '.js');
|
|
24
25
|
const src = await fsp.readFile(filePath, 'utf8');
|
|
25
26
|
const code = `'use strict';\n${src}`;
|
|
26
27
|
const script = new vm.Script(code);
|
|
@@ -32,9 +33,9 @@ const loadFile = async (context, container, filePath, key) => {
|
|
|
32
33
|
result[method] = loadService(definition, context);
|
|
33
34
|
}
|
|
34
35
|
}
|
|
35
|
-
sandbox.self = result;
|
|
36
36
|
if (container[key] === undefined) container[key] = result;
|
|
37
37
|
else Object.assign(container[key], result);
|
|
38
|
+
sandbox.self = container[key];
|
|
38
39
|
};
|
|
39
40
|
|
|
40
41
|
const loadDir = async (context, container, dirPath, key) => {
|
package/package.json
CHANGED
package/uncommon.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const loader = require('./lib/loader.js');
|
|
4
|
+
const application = require('./lib/application.js');
|
|
4
5
|
const rest = require('./lib/rest.js');
|
|
5
6
|
const errors = require('./lib/errors.js');
|
|
6
7
|
|
|
7
|
-
module.exports = { ...loader, ...rest, ...errors };
|
|
8
|
+
module.exports = { ...loader, ...application, ...rest, ...errors };
|