@leonid-shutov/uncommonjs 1.0.0-alpha.1 → 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/loader.js CHANGED
@@ -5,19 +5,6 @@ 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) => {
@@ -25,9 +12,9 @@ const loadFile = async (context, container, filePath, key) => {
25
12
  const src = await fsp.readFile(filePath, 'utf8');
26
13
  const code = `'use strict';\n${src}`;
27
14
  const script = new vm.Script(code);
28
- const sandbox = { ...context };
15
+ const sandbox = { ...context, module: container };
29
16
  const result = await script.runInContext(vm.createContext(sandbox));
30
- if (isChronological(filePath)) return void (await result());
17
+ if (result === undefined) return;
31
18
  for (const [method, definition] of Object.entries(result)) {
32
19
  if (isService(definition, context)) {
33
20
  result[method] = loadService(definition, context);
@@ -50,8 +37,7 @@ const loadDir = async (context, container, dirPath, key) => {
50
37
  else directories.push(entry);
51
38
  }
52
39
 
53
- const orderedFiles = orderFiles(files);
54
- for (const { name } of orderedFiles) {
40
+ for (const { name } of files) {
55
41
  if (!name.endsWith('.js')) continue;
56
42
  const location = path.join(dirPath, name);
57
43
  let basename = path.basename(name, '.js');
@@ -60,9 +46,10 @@ const loadDir = async (context, container, dirPath, key) => {
60
46
  else await loadFile(context, nextContainer, location, basename);
61
47
  }
62
48
 
63
- for (const { name } of directories) {
49
+ for (let { name } of directories) {
64
50
  const location = path.join(dirPath, name);
65
- if (name === '>') await loadDir(context, container, location, key);
51
+ name = removeIndex(name);
52
+ if (name === 'methods') await loadDir(context, container, location, key);
66
53
  else await loadDir(context, nextContainer, location, name);
67
54
  }
68
55
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leonid-shutov/uncommonjs",
3
- "version": "1.0.0-alpha.1",
3
+ "version": "1.0.0-alpha.2",
4
4
  "author": "Leonid Shutov <leonid.shutov.main@gmail.com>",
5
5
  "license": "MIT",
6
6
  "homepage": "https://github.com/leonid-shutov/uncommonjs#readme",
@@ -0,0 +1,6 @@
1
+ 'use strict';
2
+
3
+ ({
4
+ age: 0,
5
+ isRipe: () => self.age === 1,
6
+ });
@@ -0,0 +1,3 @@
1
+ 'use strict';
2
+
3
+ () => module.age++;
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
+ });