@makano/rew 1.1.73 → 1.1.81

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. package/lib/rew/cli/cli.js +189 -144
  2. package/lib/rew/cli/log.js +18 -19
  3. package/lib/rew/cli/run.js +7 -9
  4. package/lib/rew/cli/utils.js +109 -95
  5. package/lib/rew/const/config_path.js +4 -0
  6. package/lib/rew/const/default.js +21 -3
  7. package/lib/rew/const/files.js +11 -8
  8. package/lib/rew/const/opt.js +6 -6
  9. package/lib/rew/css/theme.css +1 -1
  10. package/lib/rew/functions/core.js +7 -9
  11. package/lib/rew/functions/emitter.js +2 -2
  12. package/lib/rew/functions/exec.js +19 -15
  13. package/lib/rew/functions/export.js +4 -6
  14. package/lib/rew/functions/fs.js +21 -18
  15. package/lib/rew/functions/future.js +1 -1
  16. package/lib/rew/functions/import.js +55 -25
  17. package/lib/rew/functions/map.js +2 -5
  18. package/lib/rew/functions/match.js +21 -5
  19. package/lib/rew/functions/path.js +2 -2
  20. package/lib/rew/functions/require.js +16 -13
  21. package/lib/rew/functions/stdout.js +11 -11
  22. package/lib/rew/functions/types.js +67 -42
  23. package/lib/rew/html/ui.html +5 -6
  24. package/lib/rew/html/ui.js +100 -58
  25. package/lib/rew/main.js +3 -3
  26. package/lib/rew/misc/findAppInfo.js +16 -0
  27. package/lib/rew/misc/findAppPath.js +21 -0
  28. package/lib/rew/misc/seededid.js +13 -0
  29. package/lib/rew/models/struct.js +1 -1
  30. package/lib/rew/modules/compiler.js +90 -58
  31. package/lib/rew/modules/context.js +35 -22
  32. package/lib/rew/modules/runtime.js +1 -1
  33. package/lib/rew/pkgs/conf.js +50 -33
  34. package/lib/rew/pkgs/data.js +7 -2
  35. package/lib/rew/pkgs/date.js +8 -9
  36. package/lib/rew/pkgs/env.js +3 -5
  37. package/lib/rew/pkgs/modules/data/bintree.js +1 -1
  38. package/lib/rew/pkgs/modules/data/doublylinked.js +1 -1
  39. package/lib/rew/pkgs/modules/data/linkedList.js +1 -1
  40. package/lib/rew/pkgs/modules/data/queue.js +1 -2
  41. package/lib/rew/pkgs/modules/data/stack.js +1 -1
  42. package/lib/rew/pkgs/modules/threads/worker.js +31 -21
  43. package/lib/rew/pkgs/modules/ui/classes.js +68 -61
  44. package/lib/rew/pkgs/pkgs.js +5 -6
  45. package/lib/rew/pkgs/rune.js +437 -0
  46. package/lib/rew/pkgs/threads.js +30 -22
  47. package/lib/rew/pkgs/ui.js +68 -44
  48. package/package.json +8 -2
@@ -1,39 +1,40 @@
1
1
  const defaultContext = require("../const/default");
2
2
  const { execOptions } = require("../const/opt");
3
- const emitter = require("../functions/emitter");
4
3
  const { exportsFunction, pubFunction } = require("../functions/export");
5
4
  const { imp } = require("../functions/import");
6
5
  const { customRequire } = require("../functions/require");
7
- const fsLib = require('../functions/fs');
8
- const pathLib = require('../functions/path');
9
- const execLib = require('../functions/exec');
6
+ const fsLib = require("../functions/fs");
7
+ const pathLib = require("../functions/path");
8
+ const execLib = require("../functions/exec");
9
+ const { findAppInfo } = require("../misc/findAppInfo");
10
10
 
11
11
  let mainFile = "";
12
- const isMainFile = filepath => filepath == mainFile;
12
+ const isMainFile = (filepath) => filepath == mainFile;
13
13
  module.exports.prepareContext = function (
14
14
  custom_context,
15
15
  options,
16
16
  filepath = "",
17
17
  runPath = () => {},
18
18
  ) {
19
- if(mainFile == "") mainFile = filepath;
19
+ if (mainFile == "") mainFile = filepath;
20
20
  let context = {
21
21
  module: {
22
22
  exports: null,
23
23
  filepath,
24
24
  main: isMainFile(filepath),
25
- imports: []
25
+ imports: [],
26
26
  },
27
27
  imports: {
28
28
  meta: {},
29
- assert: options.import ?? {}
29
+ assert: options.import ?? {},
30
30
  },
31
+ app: findAppInfo(filepath),
31
32
  ...fsLib(filepath),
32
33
  };
33
34
  if (options.useContext) {
34
35
  context = {
35
36
  ...custom_context,
36
- ...context
37
+ ...context,
37
38
  };
38
39
  } else {
39
40
  context = {
@@ -43,9 +44,15 @@ module.exports.prepareContext = function (
43
44
  ...execLib(filepath),
44
45
  require: (package) => {
45
46
  try {
46
- return execOptions.nativeRequire || package.startsWith('node:') ? require(package.startsWith('node:') ? package.split('node:')[1] : package) : customRequire(package, filepath);
47
+ return execOptions.nativeRequire || package.startsWith("node:")
48
+ ? require(
49
+ package.startsWith("node:")
50
+ ? package.split("node:")[1]
51
+ : package,
52
+ )
53
+ : customRequire(package, filepath);
47
54
  } catch (e) {
48
- throw new Error("Module "+package+" not found");
55
+ throw new Error("Module " + package + " not found");
49
56
  }
50
57
  },
51
58
  ...custom_context,
@@ -57,34 +64,40 @@ module.exports.prepareContext = function (
57
64
  target: {
58
65
  on: (event, listener) => process.on(event, listener),
59
66
  off: (event, listener) => process.off(event, listener),
60
- emit: (event, code) => process.emit(event, code)
67
+ emit: (event, code) => process.emit(event, code),
61
68
  },
62
69
  env: process.env,
63
70
  cwd: () => process.cwd(),
64
- arch: process.arch
71
+ arch: process.arch,
65
72
  };
66
-
73
+
67
74
  context.global = context;
68
75
  context.imports.assert = options.import ?? {};
69
76
  context.imp = imp(runPath, context);
70
77
  context.inc = (package, asserts) => {
71
- try{
72
- if(package.startsWith('node:') || package.startsWith('pkg:')) throw new Error('');
78
+ try {
79
+ if (package.startsWith("node:") || package.startsWith("pkg:"))
80
+ throw new Error("");
73
81
  return context.imp(package, asserts);
74
- } catch(e) {
75
- return context.require(package.startsWith('pkg:') ? package.split('pkg:')[1] : package);
82
+ } catch (e) {
83
+ return context.require(
84
+ package.startsWith("pkg:") ? package.split("pkg:")[1] : package,
85
+ );
76
86
  }
77
87
  };
78
88
  context.pub = pubFunction(context);
79
89
  context.exports = exportsFunction(context);
80
90
 
81
- if(context.module.main || (options.fromMain == true && options.as == 'main')){
91
+ if (
92
+ context.module.main ||
93
+ (options.fromMain == true && options.as == "main")
94
+ ) {
82
95
  context.opt = {
83
- set: (key, value) => execOptions[key] = value,
96
+ set: (key, value) => (execOptions[key] = value),
84
97
  get: (key) => execOptions[key],
85
98
  push: (key, value) => execOptions[key]?.push(value),
86
- pop: (key) => execOptions[key]?.pop()
99
+ pop: (key) => execOptions[key]?.pop(),
87
100
  };
88
- } else delete context.opt
101
+ } else delete context.opt;
89
102
  return context;
90
103
  };
@@ -6,7 +6,7 @@ const exec = (module.exports.exec = function (code, context) {
6
6
  return vm.runInNewContext(code, vm.createContext(context), {
7
7
  filename: context.module.filepath,
8
8
  lineOffset: 0,
9
- displayErrors: true
9
+ displayErrors: true,
10
10
  });
11
11
  });
12
12
 
@@ -1,83 +1,100 @@
1
- const fs = require('fs');
2
- const jsYaml = require('js-yaml');
3
- const path = require('path');
4
-
5
- const CONFIG_PATH = path.resolve(process.env.HOME, '.config/rew');
1
+ const fs = require("fs");
2
+ const jsYaml = require("js-yaml");
3
+ const path = require("path");
4
+ const { CONFIG_PATH } = require("../const/config_path");
5
+ const { seededID } = require("../misc/seededid");
6
6
 
7
7
  const createPackageRoot = (packageName) => {
8
8
  const rootPath = path.join(CONFIG_PATH, packageName);
9
9
  fs.mkdirSync(rootPath, { recursive: true });
10
10
  return rootPath;
11
- }
11
+ };
12
12
 
13
13
  module.exports = (context) => ({
14
14
  CONFIG_PATH,
15
+ _onImport() {
16
+ if (context.app) {
17
+ return this.create(context.app.config.package);
18
+ } else {
19
+ return this.create(
20
+ seededID(
21
+ path.basename(context.module.filepath).replace(/[-_/\.]/g, ""),
22
+ ),
23
+ );
24
+ }
25
+ },
15
26
  create: (packageName) => {
16
27
  const rootPath = createPackageRoot(packageName);
17
28
 
18
29
  const conf = {};
19
30
 
20
31
  const dumpYaml = (val) => {
21
- if(JSON.stringify(val) == '{}') return '';
32
+ if (JSON.stringify(val) == "{}") return "";
22
33
  else return jsYaml.dump(val);
23
- }
34
+ };
24
35
 
25
36
  const setData = (optionCenter, key, value) => {
26
37
  conf[optionCenter.name][key] = value;
27
38
  fs.writeFileSync(optionCenter.root, dumpYaml(conf[optionCenter.name]));
28
39
  return true;
29
- }
40
+ };
30
41
 
31
42
  const removeData = (optionCenter, key) => {
32
43
  delete conf[optionCenter.name][key];
33
44
  fs.writeFileSync(optionCenter.root, dumpYaml(conf[optionCenter.name]));
34
45
  return true;
35
- }
46
+ };
36
47
 
37
48
  const getData = (optionCenter, key) => {
38
49
  return conf[optionCenter.name][key];
39
- }
50
+ };
40
51
 
41
52
  const staticFile = (name, defaultValue = "") => {
42
53
  const fileRoot = path.join(rootPath, name);
43
54
  const exists = fs.existsSync(fileRoot);
44
55
  return {
45
- create(value){
46
- if(!fs.existsSync(path.dirname(fileRoot))) fs.mkdirSync(path.dirname(fileRoot), { recursive: true });
56
+ create(value) {
57
+ if (!fs.existsSync(path.dirname(fileRoot)))
58
+ fs.mkdirSync(path.dirname(fileRoot), { recursive: true });
47
59
  fs.writeFileSync(fileRoot, value || defaultValue);
48
60
  },
49
61
  fileRoot,
50
- exists
51
- }
52
- }
53
-
62
+ exists,
63
+ };
64
+ };
65
+
54
66
  const createOptionCenter = (name, defaults = {}) => {
55
- const optionRoot = path.join(rootPath, name+'.yaml');
56
- if(!fs.existsSync(path.dirname(optionRoot))) fs.mkdirSync(path.dirname(optionRoot), { recursive: true });
57
- if(!fs.existsSync(optionRoot)) {
67
+ const optionRoot = path.join(rootPath, name + ".yaml");
68
+ if (!fs.existsSync(path.dirname(optionRoot)))
69
+ fs.mkdirSync(path.dirname(optionRoot), { recursive: true });
70
+ if (!fs.existsSync(optionRoot)) {
58
71
  conf[name] = defaults;
59
72
  fs.writeFileSync(optionRoot, dumpYaml(defaults));
60
73
  } else {
61
- conf[name] = jsYaml.load(fs.readFileSync(optionRoot, { encoding: 'utf-8' }));
74
+ conf[name] = jsYaml.load(
75
+ fs.readFileSync(optionRoot, { encoding: "utf-8" }),
76
+ );
62
77
  }
63
78
 
64
79
  const optionCenter = {
65
80
  root: optionRoot,
66
81
  name,
67
- package: packageName
68
- }
82
+ package: packageName,
83
+ };
69
84
 
70
85
  return {
71
86
  get: (key) => getData(optionCenter, key),
72
87
  set: (key, value) => setData(optionCenter, key, value),
73
88
  remove: (key) => removeData(optionCenter, key),
74
- reset: () => fs.writeFileSync(optionCenter.root, dumpYaml(defaults)) && (conf[name] = defaults),
75
- getAll: (str = false) => str ? dumpYaml(conf[name]) : conf[name],
76
- ...optionCenter
77
- }
78
- }
89
+ reset: () =>
90
+ fs.writeFileSync(optionCenter.root, dumpYaml(defaults)) &&
91
+ (conf[name] = defaults),
92
+ getAll: (str = false) => (str ? dumpYaml(conf[name]) : conf[name]),
93
+ ...optionCenter,
94
+ };
95
+ };
79
96
 
80
- const defaultCenter = createOptionCenter('_default', { default: true });
97
+ const defaultCenter = createOptionCenter("_default", { default: true });
81
98
 
82
99
  return {
83
100
  optionCenter: createOptionCenter,
@@ -86,7 +103,7 @@ module.exports = (context) => ({
86
103
  get: (key) => defaultCenter.get(key),
87
104
  remove: (key) => defaultCenter.remove(key),
88
105
  root: rootPath,
89
- package: packageName
90
- }
91
- }
92
- });
106
+ package: packageName,
107
+ };
108
+ },
109
+ });
@@ -4,5 +4,10 @@ const { LinkedList } = require("./modules/data/linkedList");
4
4
  const { Queue } = require("./modules/data/queue");
5
5
  const { Stack } = require("./modules/data/stack");
6
6
 
7
-
8
- module.exports = (context) => ({ Stack, Queue, BinaryTree, DoublyLinkedList, LinkedList });
7
+ module.exports = (context) => ({
8
+ Stack,
9
+ Queue,
10
+ BinaryTree,
11
+ DoublyLinkedList,
12
+ LinkedList,
13
+ });
@@ -9,8 +9,8 @@
9
9
  */
10
10
  function formatDate(date) {
11
11
  const year = date.getFullYear();
12
- const month = String(date.getMonth() + 1).padStart(2, '0');
13
- const day = String(date.getDate()).padStart(2, '0');
12
+ const month = String(date.getMonth() + 1).padStart(2, "0");
13
+ const day = String(date.getDate()).padStart(2, "0");
14
14
  return `${year}-${month}-${day}`;
15
15
  }
16
16
 
@@ -20,9 +20,9 @@ function formatDate(date) {
20
20
  * @return {string} - The formatted time string.
21
21
  */
22
22
  function formatTime(date) {
23
- const hours = String(date.getHours()).padStart(2, '0');
24
- const minutes = String(date.getMinutes()).padStart(2, '0');
25
- const seconds = String(date.getSeconds()).padStart(2, '0');
23
+ const hours = String(date.getHours()).padStart(2, "0");
24
+ const minutes = String(date.getMinutes()).padStart(2, "0");
25
+ const seconds = String(date.getSeconds()).padStart(2, "0");
26
26
  return `${hours}:${minutes}:${seconds}`;
27
27
  }
28
28
 
@@ -34,7 +34,7 @@ function formatTime(date) {
34
34
  * @return {Date} - The parsed date object.
35
35
  */
36
36
  function parseDate(dateString) {
37
- const [year, month, day] = dateString.split('-').map(Number);
37
+ const [year, month, day] = dateString.split("-").map(Number);
38
38
  return new Date(year, month - 1, day);
39
39
  }
40
40
 
@@ -44,7 +44,7 @@ function parseDate(dateString) {
44
44
  * @return {Date} - The parsed date object.
45
45
  */
46
46
  function parseTime(timeString) {
47
- const [hours, minutes, seconds] = timeString.split(':').map(Number);
47
+ const [hours, minutes, seconds] = timeString.split(":").map(Number);
48
48
  const date = new Date();
49
49
  date.setHours(hours, minutes, seconds, 0);
50
50
  return date;
@@ -85,7 +85,6 @@ function differenceInDays(date1, date2) {
85
85
  return Math.ceil(diffTime / (1000 * 60 * 60 * 24));
86
86
  }
87
87
 
88
-
89
88
  // Exporting the functions as a CommonJS module
90
89
  module.exports = () => ({
91
90
  formatDate,
@@ -94,5 +93,5 @@ module.exports = () => ({
94
93
  parseTime,
95
94
  addDays,
96
95
  subtractDays,
97
- differenceInDays
96
+ differenceInDays,
98
97
  });
@@ -1,9 +1,7 @@
1
-
2
-
3
1
  module.exports = (context) => ({
4
2
  has: (key) => key in process.env,
5
3
  get: (key) => process.env[key],
6
- set: (key, value) => process.env[key] = value,
4
+ set: (key, value) => (process.env[key] = value),
7
5
  rm: (key) => delete process.env[key],
8
- is: (key, value) => process.env[key] == value
9
- })
6
+ is: (key, value) => process.env[key] == value,
7
+ });
@@ -63,4 +63,4 @@ class BinaryTree {
63
63
 
64
64
  BinaryTree.Node = TreeNode;
65
65
 
66
- module.exports = { BinaryTree };
66
+ module.exports = { BinaryTree };
@@ -97,4 +97,4 @@ class DoublyLinkedList {
97
97
 
98
98
  DoublyLinkedList.Node = DoublyNode;
99
99
 
100
- module.exports = { DoublyLinkedList };
100
+ module.exports = { DoublyLinkedList };
@@ -85,4 +85,4 @@ class LinkedList {
85
85
 
86
86
  LinkedList.Node = Node;
87
87
 
88
- module.exports = { LinkedList };
88
+ module.exports = { LinkedList };
@@ -24,5 +24,4 @@ class Queue {
24
24
  }
25
25
  }
26
26
 
27
-
28
- module.exports = { Queue };
27
+ module.exports = { Queue };
@@ -24,4 +24,4 @@ class Stack {
24
24
  }
25
25
  }
26
26
 
27
- module.exports = { Stack };
27
+ module.exports = { Stack };
@@ -1,36 +1,46 @@
1
- const { parentPort, workerData } = require('worker_threads');
2
- const { exec } = require('../../../modules/runtime');
1
+ const { parentPort, workerData } = require("worker_threads");
2
+ const { exec } = require("../../../modules/runtime");
3
3
 
4
4
  const target = {};
5
5
  target.events = [];
6
6
  target.on = (e, cb) => {
7
7
  target.events.push({ e, cb });
8
- }
8
+ };
9
9
  target.off = (e) => {
10
- target.events = target.events.filter(i => i.e !== e);
11
- }
10
+ target.events = target.events.filter((i) => i.e !== e);
11
+ };
12
12
  target.emit = (e, data) => {
13
- target.events.filter(i => i.e == e).forEach(i => i.cb(data));
14
- }
13
+ target.events.filter((i) => i.e == e).forEach((i) => i.cb(data));
14
+ };
15
15
 
16
- parentPort.on('message', (data) => {
17
- if (data.type === 'event') {
16
+ parentPort.on("message", (data) => {
17
+ if (data.type === "event") {
18
18
  target.emit(data.event, data.data);
19
- } else if (data.type === 'start') {
19
+ } else if (data.type === "start") {
20
20
  (async () => {
21
21
  try {
22
- exec(`(${workerData.cb}).call({ process }, context)`, { print: (...a) => console.log(...a), process: {
23
- on: (e, cb) => { target.on(e, cb) },
24
- off: (e) => { target.off(e) },
25
- emit: (e, data) => { parentPort.postMessage({ type: 'event', event: e, data }) },
26
- exit: (code) => process.exit(code),
27
- finish: (data) => {
28
- parentPort.postMessage({ type: 'result', result: data });
29
- process.exit(0)
30
- }
31
- }, context: workerData.context })
22
+ exec(`(${workerData.cb}).call({ process }, context)`, {
23
+ print: (...a) => console.log(...a),
24
+ process: {
25
+ on: (e, cb) => {
26
+ target.on(e, cb);
27
+ },
28
+ off: (e) => {
29
+ target.off(e);
30
+ },
31
+ emit: (e, data) => {
32
+ parentPort.postMessage({ type: "event", event: e, data });
33
+ },
34
+ exit: (code) => process.exit(code),
35
+ finish: (data) => {
36
+ parentPort.postMessage({ type: "result", result: data });
37
+ process.exit(0);
38
+ },
39
+ },
40
+ context: workerData.context,
41
+ });
32
42
  } catch (e) {
33
- parentPort.postMessage({ type: 'error', error: e.message });
43
+ parentPort.postMessage({ type: "error", error: e.message });
34
44
  }
35
45
  })();
36
46
  }