@leonid-shutov/uncommonjs 1.0.0-alpha.0 → 1.0.0-alpha.2
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 +8 -20
- package/package.json +1 -1
- package/test/application/chestnut/chestnut.js +6 -0
- package/test/application/chestnut/methods/grow.js +3 -0
- package/test/test.js +18 -0
- 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
|
@@ -5,36 +5,24 @@ const fsp = require('node:fs/promises');
|
|
|
5
5
|
const vm = require('node:vm');
|
|
6
6
|
const { isService, loadService } = require('./service.js');
|
|
7
7
|
|
|
8
|
-
const chronology = ['before.js', 'after.js'];
|
|
9
|
-
const isChronological = (filePath) =>
|
|
10
|
-
chronology.some((file) => filePath.endsWith(file));
|
|
11
|
-
const orderFiles = (files) => {
|
|
12
|
-
const before = files.find(({ name }) => name === 'before.js');
|
|
13
|
-
const after = files.find(({ name }) => name === 'after.js');
|
|
14
|
-
const ordered = files.filter(({ name }) => !chronology.includes(name));
|
|
15
|
-
ordered.sort();
|
|
16
|
-
if (before !== undefined) ordered.unshift(before);
|
|
17
|
-
if (after !== undefined) ordered.push(after);
|
|
18
|
-
return ordered;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
8
|
const removeIndex = (key) => key.replace(/^\d+-/, '');
|
|
22
9
|
|
|
23
10
|
const loadFile = async (context, container, filePath, key) => {
|
|
11
|
+
key ??= path.basename(filePath, '.js');
|
|
24
12
|
const src = await fsp.readFile(filePath, 'utf8');
|
|
25
13
|
const code = `'use strict';\n${src}`;
|
|
26
14
|
const script = new vm.Script(code);
|
|
27
|
-
const sandbox = { ...context };
|
|
15
|
+
const sandbox = { ...context, module: container };
|
|
28
16
|
const result = await script.runInContext(vm.createContext(sandbox));
|
|
29
|
-
if (
|
|
17
|
+
if (result === undefined) return;
|
|
30
18
|
for (const [method, definition] of Object.entries(result)) {
|
|
31
19
|
if (isService(definition, context)) {
|
|
32
20
|
result[method] = loadService(definition, context);
|
|
33
21
|
}
|
|
34
22
|
}
|
|
35
|
-
sandbox.self = result;
|
|
36
23
|
if (container[key] === undefined) container[key] = result;
|
|
37
24
|
else Object.assign(container[key], result);
|
|
25
|
+
sandbox.self = container[key];
|
|
38
26
|
};
|
|
39
27
|
|
|
40
28
|
const loadDir = async (context, container, dirPath, key) => {
|
|
@@ -49,8 +37,7 @@ const loadDir = async (context, container, dirPath, key) => {
|
|
|
49
37
|
else directories.push(entry);
|
|
50
38
|
}
|
|
51
39
|
|
|
52
|
-
const
|
|
53
|
-
for (const { name } of orderedFiles) {
|
|
40
|
+
for (const { name } of files) {
|
|
54
41
|
if (!name.endsWith('.js')) continue;
|
|
55
42
|
const location = path.join(dirPath, name);
|
|
56
43
|
let basename = path.basename(name, '.js');
|
|
@@ -59,9 +46,10 @@ const loadDir = async (context, container, dirPath, key) => {
|
|
|
59
46
|
else await loadFile(context, nextContainer, location, basename);
|
|
60
47
|
}
|
|
61
48
|
|
|
62
|
-
for (
|
|
49
|
+
for (let { name } of directories) {
|
|
63
50
|
const location = path.join(dirPath, name);
|
|
64
|
-
|
|
51
|
+
name = removeIndex(name);
|
|
52
|
+
if (name === 'methods') await loadDir(context, container, location, key);
|
|
65
53
|
else await loadDir(context, nextContainer, location, name);
|
|
66
54
|
}
|
|
67
55
|
};
|
package/package.json
CHANGED
package/test/test.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const test = require('node:test');
|
|
4
|
+
const assert = require('node:assert');
|
|
5
|
+
const vm = require('node:vm');
|
|
6
|
+
const path = require('node:path');
|
|
7
|
+
const { loadDir } = require('../lib/loader');
|
|
8
|
+
|
|
9
|
+
const PATH_TO_APPLICATION = path.join(__dirname, 'application');
|
|
10
|
+
|
|
11
|
+
test('Methods', async () => {
|
|
12
|
+
const context = vm.createContext({});
|
|
13
|
+
await loadDir(context, context, PATH_TO_APPLICATION);
|
|
14
|
+
const chestnut = context.application.chestnut;
|
|
15
|
+
assert.strictEqual(chestnut.isRipe(), false);
|
|
16
|
+
chestnut.grow();
|
|
17
|
+
assert.strictEqual(chestnut.isRipe(), true);
|
|
18
|
+
});
|
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 };
|