@makano/rew 1.2.68 → 1.2.69

Sign up to get free protection for your applications and to get access to all the features.
@@ -13,6 +13,7 @@ const { wait } = require('../functions/wait');
13
13
  const { scheduleFrame } = require('../functions/misc');
14
14
  const { jsons, yaml, json, yamls } = require('../functions/json');
15
15
  const { generateRandomID } = require('../functions/id');
16
+ const { namespace } = require('./usage');
16
17
 
17
18
  module.exports = {
18
19
  cenum,
@@ -50,6 +51,8 @@ module.exports = {
50
51
  yaml,
51
52
  yamls,
52
53
 
54
+ namespace,
55
+
53
56
  genID: generateRandomID,
54
57
 
55
58
  curl,
@@ -16,14 +16,31 @@ module.exports.USING_DEFAULT = {
16
16
 
17
17
  module.exports.Usage = class Usage {
18
18
  name = "null";
19
- trigger = () => {}
19
+ trigger = () => {};
20
+ save = true;
20
21
 
21
- constructor(name, trigger){
22
+ constructor(name, trigger, save){
22
23
  this.name = name;
23
24
  this.trigger = trigger;
25
+ this.save = save;
24
26
  }
25
27
 
26
- create(name, trigger){
27
- return new Usage(name, trigger);
28
+ create(name, trigger, save = true){
29
+ return new Usage(name, trigger, save);
28
30
  }
31
+ }
32
+
33
+ class Namespace extends module.exports.Usage {
34
+ namespace = {};
35
+ constructor(ns, cb){
36
+ super('namespace');
37
+ this.save = false;
38
+ this.trigger = cb;
39
+ this.namespace = ns;
40
+ }
41
+ }
42
+ module.exports.Namespace = Namespace;
43
+
44
+ module.exports.namespace = (namespace, cb) => {
45
+ return new Namespace(namespace, cb);
29
46
  }
@@ -5,9 +5,9 @@ const { wait } = require('./wait');
5
5
 
6
6
  const cahcedRequires = {};
7
7
 
8
- const doImp = (path) => wait(async () => await import(resolvedPath));
8
+ const doImp = (path) => wait(async () => await import(path));
9
9
 
10
- module.exports.customRequire = function customRequire(modulePath, filePath) {
10
+ module.exports.customRequire = function customRequire(modulePath, filePath, esm) {
11
11
  let pathname = modulePath;
12
12
  if (modulePath.startsWith('./') || modulePath.startsWith('../') || path.isAbsolute(modulePath)) {
13
13
  pathname = path.resolve(modulePath);
@@ -17,7 +17,24 @@ module.exports.customRequire = function customRequire(modulePath, filePath) {
17
17
  }
18
18
  const resolvedPath = resolveModulePath(modulePath, filePath);
19
19
  if(!resolvedPath) throw new Error('Module '+modulePath+' not found');
20
- const r = cahcedRequires[resolvedPath] ? cahcedRequires[resolvedPath] : execOptions.useImport ? doImp(resolvedPath) : require(resolvedPath);
20
+
21
+ let isEsm = esm || false;
22
+ const get_file = () => cahcedRequires[resolvedPath] ? cahcedRequires[resolvedPath] : execOptions.useImport || isEsm ? doImp(resolvedPath) : require(resolvedPath);
23
+
24
+ let r;
25
+
26
+ try{
27
+ r = get_file();
28
+ } catch(e){
29
+ if(e.code === 'ERR_REQUIRE_ESM') {
30
+ isEsm = true;
31
+ console.log('Trying with esm');
32
+ r = get_file();
33
+ } else {
34
+ throw e;
35
+ }
36
+ }
37
+
21
38
  if(!cahcedRequires[resolvedPath]) cahcedRequires[resolvedPath] = r;
22
39
  if(!cahcedRequires[pathname]) cahcedRequires[pathname] = r;
23
40
  return r;
@@ -40,10 +57,13 @@ function resolveModulePath(modulePath, filePath) {
40
57
 
41
58
  if (fs.existsSync(fullPath) && fs.statSync(fullPath).isDirectory()) {
42
59
  return searchInPath(fullPath);
60
+ } else if (fs.existsSync(fullPath) && fs.statSync(fullPath).isFile()) {
61
+ return fullPath;
43
62
  }
44
63
 
45
64
  const rootPath = modulePath.split('/').shift();
46
65
  const halfFullPath = path.join(basePath, rootPath);
66
+
47
67
  if (fs.existsSync(halfFullPath) && fs.statSync(halfFullPath).isDirectory()) {
48
68
  return searchInPath(halfFullPath, ['.'].concat(fullPath.split('/').slice(1)).join('/'));
49
69
  }
@@ -61,8 +81,8 @@ function searchInPath(fullPath, exportses){
61
81
  }
62
82
  }
63
83
  if(typeof main == "object"){
64
- if(Array.isArray(main)) main = main[0].require;
65
- else main = main.require;
84
+ if(Array.isArray(main)) main = execOptions.useImport ? main[0].import || main[0].require : main[0].require || main[0].import;
85
+ else main = execOptions.useImport ? main.import || main.require : main.require || main.import;
66
86
  }
67
87
  const mainPath = path.join(fullPath, main);
68
88
  if (fs.existsSync(mainPath)) {
@@ -8,7 +8,7 @@ const pathLib = require("../functions/path");
8
8
  const path = require("path");
9
9
  const execLib = require("../functions/exec");
10
10
  const { findAppInfo } = require("../misc/findAppInfo");
11
- const { USING_DEFAULT, Usage } = require("../const/usage");
11
+ const { USING_DEFAULT, Usage, Namespace } = require("../const/usage");
12
12
 
13
13
  let mainFile = "";
14
14
  const isMainFile = (filepath) => filepath == mainFile;
@@ -67,7 +67,8 @@ module.exports.prepareContext = function (
67
67
  context.global = context;
68
68
  context.imports.assert = options.import ?? {};
69
69
  context.imp = imp(runPath, context);
70
- context.require = (package) => {
70
+ context.import = context.imp;
71
+ context.require = (package, esm) => {
71
72
  try {
72
73
  const search = execOptions.nativeRequire || package.startsWith("node:")
73
74
  ? require(
@@ -75,7 +76,7 @@ module.exports.prepareContext = function (
75
76
  ? package.split("node:")[1]
76
77
  : package,
77
78
  )
78
- : customRequire(package, filepath);
79
+ : customRequire(package, filepath, esm);
79
80
  if(!search) throw new Error("Module " + package + " not found");
80
81
  return search;
81
82
  } catch (e) {
@@ -88,8 +89,15 @@ module.exports.prepareContext = function (
88
89
  throw new Error("");
89
90
  return context.imp(package, asserts);
90
91
  } catch (e) {
92
+ let pname = package.startsWith("pkg:") ? package.split("pkg:")[1] : package;
93
+ if(pname.endsWith('#esm')){
94
+ pname = pname.slice(0, -4);
95
+ if(!asserts) asserts = { esm: true };
96
+ else asserts.esm = true;
97
+ }
91
98
  return context.require(
92
- package.startsWith("pkg:") ? package.split("pkg:")[1] : package,
99
+ pname,
100
+ asserts?.esm
93
101
  );
94
102
  }
95
103
  };
@@ -101,8 +109,17 @@ module.exports.prepareContext = function (
101
109
  if(USING_DEFAULT[name].param) {
102
110
  context.__using__[name] = USING_DEFAULT[name].param(...params);
103
111
  }
104
- } else if(name instanceof Usage) {
105
- context.__using__[name.name] = name.trigger(...params) || true;
112
+ } else if(name instanceof Namespace) {
113
+ const trigger = name.trigger;
114
+ const childContext = {...context, ...name.namespace, trigger};
115
+ childContext.currentNamespace = name.namespace;
116
+ childContext.parentNamespace = context;
117
+ with(childContext){
118
+ eval(`(${trigger.toString()})()`);
119
+ }
120
+ } else if(name instanceof Usage) {
121
+ const v = name.trigger(...params) || true;
122
+ if(name.save !== false) context.__using__[name.name] = v;
106
123
  } else {
107
124
  context.__using__[name] = params.length ? params.length > 1 ? [...params] : params : true;
108
125
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@makano/rew",
3
- "version": "1.2.68",
3
+ "version": "1.2.69",
4
4
  "description": "A simple coffescript runtime and app manager",
5
5
  "main": "main.js",
6
6
  "directories": {
package/runtime.d.ts CHANGED
@@ -942,9 +942,12 @@ declare function using(fn: any, ...args: any[]): any;
942
942
  declare function wait(fn: CallableFunction, ...args: any[]): any;
943
943
  declare function clear(): void;
944
944
 
945
+ declare function namespace(object: any, callback: () => any): Usage;
946
+
945
947
  declare class Usage<T = () => void> {
946
948
  name: string;
947
949
  trigger: T;
948
- constructor(name: string, trigger: T);
949
- create(name: string, trigger: T): Usage;
950
+ save: boolean;
951
+ constructor(name: string, trigger: T, save: boolean);
952
+ create(name: string, trigger: T, save: boolean): Usage;
950
953
  }