@makano/rew 1.1.1 → 1.1.5

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/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  <h3 align="center">
2
- <img src="https://raw.githubusercontent.com/kevinJ045/rew/main/assets/logo.png" width="100" />
2
+ <img src="https://raw.githubusercontent.com/kevinJ045/rew-docs/main/assets/logo.png" width="100" />
3
3
  <br/>
4
4
  Rew
5
5
  <br/>
package/bin/rew CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  const path = require('path');
3
3
  const fs = require('fs');
4
- const rew_mod = path.resolve(process.cwd(), 'node_modules/@makano/rew');
4
+ const rew_mod = path.resolve(process.cwd(), 'snode_moduless/@makano/rew');
5
5
  if(fs.existsSync(rew_mod)){
6
6
  require(path.join(rew_mod, 'lib/rew/cli/cli.js'));
7
7
  } else {
@@ -6,8 +6,9 @@ const { hideBin } = require('yargs/helpers');
6
6
  const { fork, exec } = require('child_process');
7
7
  const { watch } = require('chokidar');
8
8
  const utils = require('./utils');
9
- const { existsSync } = require('fs');
9
+ const { existsSync, readFileSync, writeFileSync, mkdirSync } = require('fs');
10
10
  const { log } = require('./log');
11
+ const { compileFile, compile } = require('../modules/compiler');
11
12
 
12
13
  yargs(hideBin(process.argv))
13
14
  .command(
@@ -47,8 +48,6 @@ yargs(hideBin(process.argv))
47
48
  data.forEach(file => {
48
49
  watchIt(file);
49
50
  });
50
- } else {
51
- process.exit();
52
51
  }
53
52
  }).send({ filePath, watch: argv.watch });
54
53
  if(argv.watch) watchIt(filePath);
@@ -127,9 +126,69 @@ yargs(hideBin(process.argv))
127
126
  .positional('file', {
128
127
  describe: 'File to build',
129
128
  type: 'string',
129
+ })
130
+ .option('output', {
131
+ alias: 'o',
132
+ describe: 'Output directory',
133
+ type: 'string',
130
134
  });
131
135
  }, (argv) => {
132
- console.log(`Building file: ${argv.file}`);
136
+
137
+ function readFile(filePath) {
138
+ return readFileSync(filePath, { encoding: 'utf-8' });
139
+ }
140
+
141
+ function extractImports(content) {
142
+ const importRegex = /(\w+)\s*=\s*imp\s*['"](.+?)['"]/g;
143
+ const imports = [];
144
+ let match;
145
+ while ((match = importRegex.exec(content)) !== null) {
146
+ imports.push({ variable: match[1], url: match[2] });
147
+ }
148
+ return imports;
149
+ }
150
+
151
+ function writeCompiledFile(filePath, compiledCode) {
152
+ const dirName = outputDir ? outputDir : path.dirname(filePath);
153
+ if(!existsSync(dirName)) mkdirSync(dirName, { recursive: true });
154
+ const baseName = path.basename(filePath, path.extname(filePath));
155
+ const newFilePath = path.join(dirName, `${baseName}.js`);
156
+ writeFileSync(newFilePath, compiledCode, { encoding: 'utf-8' });
157
+ log(`Compiled: ${newFilePath}`);
158
+ }
159
+
160
+ function processFile(filePath, importsArray) {
161
+ const content = readFile(filePath);
162
+ const imports = extractImports(content);
163
+
164
+ imports.forEach(importStatement => {
165
+ const importedFilePath = path.resolve(path.dirname(filePath), importStatement.url);
166
+ if (!importsArray.some(importObj => importObj.url === importStatement.url)) {
167
+
168
+ if(existsSync(importedFilePath)){
169
+ importsArray.push(importStatement);
170
+ processFile(importedFilePath, importsArray);
171
+ } else if(existsSync(importedFilePath+'.coffee')){
172
+ importsArray.push(importStatement);
173
+ processFile(importedFilePath+'.coffee', importsArray);
174
+ } else if(existsSync(importedFilePath+'.js')){
175
+ importsArray.push(importStatement);
176
+ processFile(importedFilePath+'.js', importsArray);
177
+ }
178
+
179
+ }
180
+ });
181
+
182
+ const compiled = compile({ content }, {});
183
+ writeCompiledFile(filePath, compiled);
184
+ }
185
+
186
+ const filePath = path.resolve(process.cwd(), argv.file);
187
+ const importsArray = [];
188
+ const outputDir = argv.output ? path.resolve(process.cwd(), argv.output) : null;
189
+ log('Start compile at', outputDir || 'default path');
190
+ processFile(filePath, importsArray);
191
+ log('Compiled', importsArray.length + 1, 'files.', ':end');
133
192
  })
134
193
  .help()
135
194
  .argv;
@@ -1,6 +1,4 @@
1
- const path = require('path');
2
1
  const { run } = require('../main');
3
- const { watch } = require('fs');
4
2
 
5
3
 
6
4
  function exec(filePath){
@@ -8,13 +6,12 @@ function exec(filePath){
8
6
  .context.module.imports;
9
7
  }
10
8
 
11
-
12
- process.on('message', ({ filePath, watch }) => {
9
+ const onmsg = ({ filePath, watch }) => {
13
10
  const imports = exec(filePath);
14
11
  if(watch){
15
12
  process.send(imports);
16
- process.exit();
17
- } else {
18
- process.exit();
19
13
  }
20
- });
14
+ process.off('message', onmsg);
15
+ }
16
+
17
+ process.on('message', onmsg);
@@ -107,7 +107,7 @@ module.exports = {
107
107
  const c = jsYaml.load(fs.readFileSync(confPath, { encoding: 'utf-8' }));
108
108
  if(c.entry){
109
109
  const r = path.resolve(root, c.entry);
110
- const mod_path = path.resolve(root, 'node_modules/@makano/rew');
110
+ const mod_path = path.resolve(root, 'snode_moduless/@makano/rew');
111
111
  const mod_path_lib = path.join(mod_path, 'lib/rew/cli');
112
112
  if(fs.existsSync(mod_path) && __dirname !== mod_path_lib){
113
113
  const mod_path_utilsjs = path.join(mod_path_lib, '../main.js');
@@ -5,8 +5,9 @@ const future = require("../functions/future");
5
5
  const sleep = require("../functions/sleep");
6
6
  const { match } = require("../functions/match");
7
7
  const { map } = require("../functions/map");
8
- const { typex, typeis, typedef, typei } = require("../functions/types");
8
+ const { typex, typeis, typedef, typei, int, float, num, str, bool } = require("../functions/types");
9
9
  const { isEmpty, clone, deepClone, merge, uniqueId, compose, curry } = require("../functions/core");
10
+ const { print, input } = require("../functions/stdout");
10
11
 
11
12
  module.exports = {
12
13
  cenum,
@@ -16,11 +17,18 @@ module.exports = {
16
17
  sleep,
17
18
  match,
18
19
  map,
20
+
19
21
  typex,
20
22
  typei,
21
23
  typeis,
22
24
  typedef,
23
25
 
26
+ int,
27
+ float,
28
+ num,
29
+ str,
30
+ bool,
31
+
24
32
  isEmpty,
25
33
  clone,
26
34
  deepClone,
@@ -29,7 +37,6 @@ module.exports = {
29
37
  compose,
30
38
  curry,
31
39
 
32
- print: function (...arguments) {
33
- return console.log(...arguments);
34
- },
40
+ print,
41
+ input
35
42
  };
@@ -0,0 +1,8 @@
1
+ const execOptions = {
2
+ sharedContext: true,
3
+ resolveExtensions: [{ext: '.js', options: { type: 'js' }}, '.coffee'],
4
+ nativeRequire: false,
5
+ cwdAlias: '$'
6
+ }
7
+
8
+ module.exports.execOptions = execOptions;
@@ -1,4 +1,5 @@
1
1
 
2
+
2
3
  function isEmpty(value) {
3
4
  if (Array.isArray(value) || typeof value === 'string') {
4
5
  return value.length === 0;
@@ -0,0 +1,27 @@
1
+ const shell = require('child_process');
2
+
3
+
4
+ module.exports = (currentFile) => {
5
+
6
+ function exec(command, options){
7
+ return shell.execSync(command, { stdio: options?.output == false ? null : 'inherit' });
8
+ }
9
+
10
+ exec.background = function execAsync(command, options, callback){
11
+ if(typeof options == "function" && !callback){
12
+ callback = options;
13
+ options = {};
14
+ }
15
+ if(!options) options = {};
16
+ if(!callback) callback = () => {};
17
+ return shell.exec(command, {
18
+ ...options,
19
+ }, callback);
20
+ }
21
+
22
+ function spawn(command, options){
23
+ return shell.spawn(command, options);
24
+ }
25
+
26
+ return { exec, spawn };
27
+ }
@@ -1,9 +1,27 @@
1
+
2
+ function exportsThe(item, name, context){
3
+ if (name) {
4
+ if(!context.module.exports) context.module.exports = {};
5
+ context.module.exports[name] = item;
6
+ } else {
7
+ if(context.module.exports) context.module.exports.default = item;
8
+ else context.module.exports = item;
9
+ }
10
+ }
11
+
12
+ module.exports.pubFunction = function (context) {
13
+ return function (name, item) {
14
+ if(name && !item){
15
+ item = name;
16
+ name = null;
17
+ }
18
+ exportsThe(item, name, context);
19
+ };
20
+ };
21
+
1
22
  module.exports.exportsFunction = function (context) {
2
23
  return function (item, name) {
3
- if (name) {
4
- context.module.exports[name] = item;
5
- } else {
6
- context.module.exports = item;
7
- }
24
+ exportsThe(item, name, context);
8
25
  };
9
26
  };
27
+
@@ -0,0 +1,54 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const { execOptions } = require('../const/opt');
4
+
5
+
6
+ module.exports = (currentFile) => {
7
+
8
+ function gp(filepath){
9
+ return path.resolve(filepath.startsWith(execOptions.cwdAlias) ? process.cwd() : path.dirname(currentFile), filepath.replaceAll(execOptions.cwdAlias+'/', ''));
10
+ }
11
+
12
+ function read(filepath, options = { encoding: 'utf-8' }){
13
+ return fs.readFileSync(gp(filepath), options);
14
+ }
15
+
16
+ function write(filepath, content, options){
17
+ return fs.writeFileSync(gp(filepath), content, options);
18
+ }
19
+
20
+ function exists(filepath, options){
21
+ return fs.existsSync(gp(filepath));
22
+ }
23
+
24
+ function fstat(filepath, options){
25
+ return fs.statSync(gp(filepath), options);
26
+ }
27
+
28
+ function rm(filepath, options){
29
+ return fs.unlinkSync(filepath);
30
+ }
31
+
32
+ function chmod(filepath, mode, options){
33
+ return fs.chmodSync(gp(filepath), mode);
34
+ }
35
+
36
+ function mkdir(filepath, options){
37
+ return fs.mkdirSync(gp(filepath), options);
38
+ }
39
+
40
+ function ls(filepath, options){
41
+ return fs.readdirSync(gp(filepath), options);
42
+ }
43
+
44
+ return {
45
+ ls,
46
+ mkdir,
47
+ chmod,
48
+ rm,
49
+ fstat,
50
+ exists,
51
+ write,
52
+ read
53
+ };
54
+ }
@@ -5,6 +5,9 @@ const { findPackage, getPackage } = require("../pkgs/pkgs");
5
5
  const { existsSync, readFileSync } = require("fs");
6
6
  const conf = require("../pkgs/conf");
7
7
  const jsYaml = require("js-yaml");
8
+ const { execOptions } = require("../const/opt");
9
+
10
+ const cachedFiles = [];
8
11
 
9
12
  const lookUpInOtherApps = (fullPath) => {
10
13
  const con = conf({});
@@ -30,26 +33,45 @@ module.exports.imp = function (runPath, context) {
30
33
 
31
34
  // console.log(typeof runPath);
32
35
 
33
- if(!ispkg && !existsSync(filepath)){
36
+ const lookUp = () => {
34
37
  const otherPath = lookUpInOtherApps(filename);
35
38
  if(!otherPath) throw new Error('Module "'+filename+'" not found');
36
39
  else filepath = otherPath;
37
40
  }
38
41
 
42
+ const foundCache = cachedFiles.find(f => f.filepath == filepath);
43
+
44
+ if(!ispkg && foundCache){
45
+ exports = foundCache.exports;
46
+ }
47
+
48
+ if(!ispkg && !existsSync(filepath)){
49
+ if(Array.isArray(execOptions.resolveExtensions) && execOptions.resolveExtensions.length){
50
+ const resolve = execOptions.resolveExtensions.find(ext => typeof ext == "string" ? existsSync(filepath+ext) : existsSync(filepath+(ext.ext || '')));
51
+ if(resolve) {
52
+ filepath += typeof resolve == "string" ? resolve : resolve.ext;
53
+ if(typeof resolve == "object" && resolve.options){
54
+ if(resolve.options.type) type = resolve.options.type;
55
+ for(let i in resolve.options) options[i] = resolve.options[i];
56
+ }
57
+ } else lookUp();
58
+ } else lookUp();
59
+ }
60
+
61
+ const exec = (coptions = {}) => runPath(
62
+ filepath,
63
+ { import: options, main: false, useContext: execOptions.sharedContext == false ? false : true, ...coptions },
64
+ execOptions.sharedContext == false ? {} : context,
65
+ ).context.module.exports;
66
+
39
67
  if (ispkg) {
40
68
  exports = getPackage(filename)(context);
69
+ } else if(foundCache) {
70
+
41
71
  } else if (type == "coffee") {
42
- exports = runPath(
43
- filepath,
44
- { ...options, useContext: true },
45
- context,
46
- ).context.module.exports;
72
+ exports = exec({ });
47
73
  } else if (type == "js") {
48
- exports = runPath(
49
- filepath,
50
- { ...options, useContext: true, compile: false },
51
- context,
52
- ).context.module.exports;
74
+ exports = exec({ compile: false });
53
75
  } else if (type == "yaml" || type == "json" || type == "text") {
54
76
  const f = getFile(filepath);
55
77
  if (type == "yaml") {
@@ -74,6 +96,7 @@ module.exports.imp = function (runPath, context) {
74
96
  }
75
97
 
76
98
  if(!ispkg) context.module.imports.push(filepath);
99
+ if(!ispkg) cachedFiles.push({ filepath, exports });
77
100
 
78
101
  return exports;
79
102
  };
@@ -0,0 +1,13 @@
1
+ const path = require('path');
2
+
3
+ module.exports = (currentFile) => {
4
+ const e = {};
5
+ e.basename = (pathname, suffix) => path.basename(pathname, suffix);
6
+ e.dirname = (pathname) => path.dirname(pathname);
7
+ e.extname = (pathname) => path.extname(pathname);
8
+
9
+ e.pjoin = (...paths) => path.join(...paths);
10
+ e.presolve = (...paths) => path.resolve(...paths);
11
+
12
+ return e;
13
+ }
@@ -0,0 +1,41 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+
4
+
5
+ module.exports.customRequire = function customRequire(modulePath, filePath) {
6
+ const resolvedPath = resolveModulePath(modulePath, filePath);
7
+ return require(resolvedPath);
8
+ }
9
+
10
+ function resolveModulePath(modulePath, filePath) {
11
+ if (modulePath.startsWith('./') || modulePath.startsWith('../') || path.isAbsolute(modulePath)) {
12
+ return path.resolve(modulePath);
13
+ }
14
+
15
+ const paths = module.constructor._nodeModulePaths(path.dirname(filePath));
16
+ for (const basePath of paths) {
17
+ const fullPath = path.join(basePath, modulePath);
18
+ if (fs.existsSync(fullPath + '.js')) {
19
+ return fullPath + '.js';
20
+ }
21
+ if (fs.existsSync(fullPath + '.json')) {
22
+ return fullPath + '.json';
23
+ }
24
+ if (fs.existsSync(fullPath) && fs.statSync(fullPath).isDirectory()) {
25
+ const packageJsonPath = path.join(fullPath, 'package.json');
26
+ if (fs.existsSync(packageJsonPath)) {
27
+ const main = require(packageJsonPath).main || 'index.js';
28
+ const mainPath = path.join(fullPath, main);
29
+ if (fs.existsSync(mainPath)) {
30
+ return mainPath;
31
+ }
32
+ }
33
+ const indexPath = path.join(fullPath, 'index.js');
34
+ if (fs.existsSync(indexPath)) {
35
+ return indexPath;
36
+ }
37
+ }
38
+ }
39
+
40
+ throw new Error(`Cannot find module '${modulePath}'`);
41
+ }
@@ -0,0 +1,30 @@
1
+ const { execSync, spawnSync } = require('child_process');
2
+ const fs = require('fs');
3
+
4
+ const print = module.exports.print = function print(...arguments) {
5
+ return console.log(...arguments);
6
+ };
7
+
8
+ print.stdout = process.stdout;
9
+ print.stdin = process.stdin;
10
+
11
+ module.exports.input = function input(prompt) {
12
+ process.stdout.write(prompt);
13
+
14
+ let cmd;
15
+ let args;
16
+ if ("null" == "win32") {
17
+ cmd = 'cmd';
18
+ args = ['/V:ON', '/C', 'set /p response= && echo !response!'];
19
+ } else {
20
+ cmd = 'bash';
21
+ args = ['-c', 'read response; echo "$response"'];
22
+ }
23
+
24
+ let opts = {
25
+ stdio: ['inherit', 'pipe'],
26
+ shell: false,
27
+ };
28
+
29
+ return spawnSync(cmd, args, opts).stdout.toString().trim();
30
+ }
@@ -67,11 +67,39 @@ function typei(child, parent) {
67
67
  return child instanceof parent || child.constructor === parent;
68
68
  }
69
69
 
70
+ function int(str){
71
+ return parseInt(str);
72
+ }
73
+
74
+ function float(str){
75
+ return parseFloat(str);
76
+ }
77
+
78
+ function num(str){
79
+ return Number(str);
80
+ }
81
+
82
+ function str(str){
83
+ return str ? str.toString() : "";
84
+ }
85
+
86
+ function bool(value){
87
+ return typeof value == 'string' ? (
88
+ value == 'true' ? true : false
89
+ ) : value !== null && value !== undefined;
90
+ }
91
+
70
92
  module.exports = {
71
93
  typex,
72
94
  typei,
73
95
  typeis,
74
- typedef
96
+ typedef,
97
+
98
+ int,
99
+ float,
100
+ num,
101
+ str,
102
+ bool
75
103
  }
76
104
 
77
105
 
@@ -1,8 +1,160 @@
1
1
  const { compile } = require("../../coffeescript/coffeescript");
2
2
  const { getFile } = require("./fs");
3
3
 
4
+ function tokenizeCoffeeScript(code) {
5
+ const tokens = [];
6
+ let currentToken = '';
7
+
8
+ for (let i = 0; i < code.length; i++) {
9
+ const char = code[i];
10
+ const nextChar = code[i + 1];
11
+
12
+ if (char === '#') {
13
+ // Comment
14
+ tokens.push({ type: 'COMMENT', value: char + code.substring(i + 1).split('\n')[0] });
15
+ i = code.indexOf('\n', i);
16
+ } else if (char === '"' || char === "'") {
17
+ // String
18
+ let string = char;
19
+ let escaped = false;
20
+ i++;
21
+ while (i < code.length && (code[i] !== char || escaped)) {
22
+ string += code[i];
23
+ if (code[i] === '\\' && !escaped) {
24
+ escaped = true;
25
+ } else {
26
+ escaped = false;
27
+ }
28
+ i++;
29
+ }
30
+ string += char; // Include closing quote
31
+ tokens.push({ type: 'STRING', value: string });
32
+ } else if (char === '/' && (nextChar === '/' || nextChar === '*')) {
33
+ // Regular expression
34
+ let regex = char;
35
+ i++;
36
+ while (i < code.length && (code[i] !== '/' || regex.endsWith('\\'))) {
37
+ regex += code[i];
38
+ i++;
39
+ }
40
+ regex += '/';
41
+ tokens.push({ type: 'REGEX', value: regex });
42
+ } else if (/\s/.test(char)) {
43
+ // Whitespace
44
+ if(tokens[tokens.length-1]?.type == 'WHITESPACE'
45
+ && tokens[tokens.length-1].value[0] == char
46
+ ){
47
+ tokens[tokens.length-1].value += char;
48
+ } else {
49
+ tokens.push({ type: 'WHITESPACE', value: char });
50
+ }
51
+ } else if (/[a-zA-Z_$]/.test(char)) {
52
+ // Identifier
53
+ let identifier = char;
54
+ i++;
55
+ while (i < code.length && /[a-zA-Z0-9_$]/.test(code[i])) {
56
+ identifier += code[i];
57
+ i++;
58
+ }
59
+ tokens.push({ type: 'IDENTIFIER', value: identifier });
60
+ i--; // Move back one character to recheck
61
+ } else {
62
+ // Other characters
63
+ tokens.push({ type: 'OTHER', value: char });
64
+ }
65
+ }
66
+
67
+ return tokens;
68
+ }
69
+
70
+ const gnextToken = (i, n, tokens) => {
71
+ return tokens[i + n] ? tokens[i + n].type == 'WHITESPACE' ? gnextToken(i, n + 1, tokens) : { nextToken: tokens[i + n], n } : null;
72
+ }
73
+
74
+ const fnextToken = (i, tokens, type, value) => {
75
+ return tokens.map((t, ind) => { t.ti = ind; return t }).slice(i, tokens.length - 1).map((t, ind) => { t.ri = ind; t.index = ind - i; return t }).find(t => t.type == type && (value ? t.value == value : true));
76
+ }
77
+
78
+ function compileRewStuff(content) {
79
+ const tokens = tokenizeCoffeeScript(content);
80
+ let result = '';
81
+
82
+ let hooks = [];
83
+
84
+ for (let i = 0; i < tokens.length; i++) {
85
+ const token = tokens[i];
86
+ let { nextToken, n } = gnextToken(i, 1, tokens) || {};
87
+
88
+ if (token.type === 'IDENTIFIER' && token.value === 'import') {
89
+ // console.log(nextToken.type);
90
+ let ind = i + n + 2;
91
+
92
+ let defaultName;
93
+ if (nextToken.value === '{') {
94
+ const closingBraceToken = fnextToken(ind, tokens, 'OTHER', '}');
95
+ const nameToken = fnextToken(ind, tokens, 'STRING');
96
+ if (closingBraceToken) {
97
+ const exportsTokens = tokens.slice(ind, closingBraceToken.ti);
98
+ const exports = exportsTokens.filter(t => t.type === 'IDENTIFIER').map(t => t.value).join(', ');
99
+ result += `{ ${exports} } = inc ${nameToken.value}`;
100
+ i = nameToken.ti;
101
+ }
102
+ } else if (nextToken.value === '*') {
103
+ const asToken = fnextToken(ind, tokens, 'IDENTIFIER', 'as');
104
+ const nameToken = fnextToken(asToken.ri, tokens, 'STRING');
105
+ if (asToken) {
106
+ const nextToken = fnextToken(asToken.ti + 1, tokens, 'IDENTIFIER');
107
+ defaultName = nextToken.value;
108
+ result += `${defaultName} = inc ${nameToken.value}`;
109
+ i = ind + 6;
110
+ }
111
+ } else {
112
+ const nameToken = fnextToken(ind, tokens, 'STRING');
113
+ defaultName = nextToken.value;
114
+ result += `{ default: ${defaultName} } = inc ${nameToken.value}`;
115
+ i = ind + 2;
116
+ }
117
+
118
+ const nextLastToken = fnextToken(i, tokens, 'IDENTIFIER');
119
+
120
+ if(nextLastToken?.value == 'assert'){
121
+ result += ', ';
122
+ i += 3;
123
+ }
124
+
125
+ continue;
126
+ }
127
+
128
+
129
+ if (token.type === 'IDENTIFIER' && token.value === 'pub' &&
130
+ nextToken && nextToken.type === 'IDENTIFIER' &&
131
+ nextToken.value && nextToken.value !== 'undefined') {
132
+
133
+ hooks.push({
134
+ index: i + 1,
135
+ value: `"${nextToken.value}", `
136
+ });
137
+ }
138
+
139
+ result += token.value;
140
+ if (hooks.length) {
141
+ hooks.forEach((hook, ind) => {
142
+ if (i == hook.index) {
143
+ result += hook.value;
144
+ hooks.splice(ind, 1);
145
+ }
146
+ });
147
+ }
148
+ }
149
+
150
+ // console.log(result)
151
+
152
+ return result;
153
+ }
154
+
155
+
4
156
  const cpl = (module.exports.compile = function (file, options = {}) {
5
- return compile(file.content, options);
157
+ return compile(compileRewStuff(file.content), { ...options, filename: file.path, bare: true, inlineMap: true });
6
158
  });
7
159
 
8
160
  module.exports.compileFile = function (filepath, options = {}) {
@@ -1,7 +1,12 @@
1
1
  const defaultContext = require("../const/default");
2
+ const { execOptions } = require("../const/opt");
2
3
  const emitter = require("../functions/emitter");
3
- const { exportsFunction } = require("../functions/export");
4
+ const { exportsFunction, pubFunction } = require("../functions/export");
4
5
  const { imp } = require("../functions/import");
6
+ const { customRequire } = require("../functions/require");
7
+ const fsLib = require('../functions/fs');
8
+ const pathLib = require('../functions/path');
9
+ const execLib = require('../functions/exec');
5
10
 
6
11
  module.exports.prepareContext = function (
7
12
  custom_context,
@@ -12,10 +17,15 @@ module.exports.prepareContext = function (
12
17
  let context = {
13
18
  module: {
14
19
  exports: null,
15
- options,
16
20
  filepath,
21
+ main: options.main ?? true,
17
22
  imports: []
18
23
  },
24
+ imports: {
25
+ meta: {},
26
+ assert: options.import ?? {}
27
+ },
28
+ ...fsLib(filepath),
19
29
  };
20
30
  if (options.useContext) {
21
31
  context = {
@@ -26,16 +36,32 @@ module.exports.prepareContext = function (
26
36
  context = {
27
37
  ...context,
28
38
  ...defaultContext,
39
+ ...pathLib(filepath),
40
+ ...execLib(filepath),
29
41
  require: (package) => {
30
42
  try {
31
- return require(package);
43
+ return execOptions.nativeRequire || package.startsWith('node:') ? require(package.startsWith('node:') ? package.split('node:')[1] : package) : customRequire(package, filepath);
32
44
  } catch (e) {
33
- throw new Error("Module not found");
45
+ throw new Error("Module "+package+" not found");
34
46
  }
35
47
  },
48
+ opt: {
49
+ set: (key, value) => execOptions[key] = value,
50
+ get: (key) => execOptions[key],
51
+ push: (key, value) => execOptions[key]?.push(value),
52
+ pop: (key) => execOptions[key]?.pop()
53
+ },
36
54
  ...custom_context,
37
55
  };
38
56
  context.imp = imp(runPath, context);
57
+ context.inc = (package, asserts) => {
58
+ try{
59
+ return context.imp(package, asserts);
60
+ } catch(e) {
61
+ return context.require(package);
62
+ }
63
+ };
64
+ context.pub = pubFunction(context);
39
65
  context.exports = exportsFunction(context);
40
66
  }
41
67
  if (!context.global) context.global = context;
@@ -44,7 +70,9 @@ module.exports.prepareContext = function (
44
70
  argv: process.argv,
45
71
  target: emitter(),
46
72
  env: process.env,
73
+ cwd: () => process.cwd(),
74
+ arch: process.arch
47
75
  };
48
- // console.log(custom_context);
76
+ context.imports.assert = options.import ?? {};
49
77
  return context;
50
78
  };
@@ -0,0 +1,9 @@
1
+
2
+
3
+ module.exports = (context) => ({
4
+ has: (key) => key in process.env,
5
+ get: (key) => process.env[key],
6
+ set: (key, value) => process.env[key] = value,
7
+ rm: (key) => delete process.env[key],
8
+ is: (key, value) => process.env[key] == value
9
+ })
@@ -0,0 +1,37 @@
1
+ const { parentPort, workerData } = require('worker_threads');
2
+ const { exec } = require('../../../modules/runtime');
3
+
4
+ const target = {};
5
+ target.events = [];
6
+ target.on = (e, cb) => {
7
+ target.events.push({ e, cb });
8
+ }
9
+ target.off = (e) => {
10
+ target.events = target.events.filter(i => i.e !== e);
11
+ }
12
+ target.emit = (e, data) => {
13
+ target.events.filter(i => i.e == e).forEach(i => i.cb(data));
14
+ }
15
+
16
+ parentPort.on('message', (data) => {
17
+ if (data.type === 'event') {
18
+ target.emit(data.event, data.data);
19
+ } else if (data.type === 'start') {
20
+ (async () => {
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 })
32
+ } catch (e) {
33
+ parentPort.postMessage({ type: 'error', error: e.message });
34
+ }
35
+ })();
36
+ }
37
+ });
@@ -0,0 +1,71 @@
1
+ const { Worker } = require('worker_threads');
2
+ const emitter = require('../functions/emitter');
3
+ const future = require('../functions/future');
4
+ const { struct } = require('../models/struct');
5
+ const path = require('path');
6
+
7
+ module.exports = (context) => ({
8
+ thread: (cb) => {
9
+ const workers = [];
10
+
11
+ const stopWorker = (worker) => {
12
+ workers.splice(workers.indexOf(worker), 1);
13
+ worker.terminate()
14
+ };
15
+
16
+ const athread = {
17
+ stopAll: () => {
18
+ if (!run) return;
19
+ workers.forEach(stopWorker);
20
+ },
21
+ start: (context) => {
22
+ let dataResult = future(() => {}), listener = emitter();
23
+ const worker = new Worker(path.resolve(__dirname, './modules/threads/worker.js'), {
24
+ workerData: { context, cb: cb.toString() }
25
+ });
26
+ workers.push(worker);
27
+
28
+ const stop = () => stopWorker(worker);
29
+
30
+ worker.on('message', (data) => {
31
+ if (data.type === 'result') {
32
+ dataResult.resolve(data.result);
33
+ stop();
34
+ } else if (data.type === 'error') {
35
+ reject(new Error(data.error));
36
+ stop();
37
+ } else if (data.type === 'event') {
38
+ listener.emit(data.event, data.data);
39
+ }
40
+ });
41
+
42
+ worker.on('error', (error) => {
43
+ console.log(error);
44
+ stop();
45
+ });
46
+
47
+ let exiting = false;
48
+
49
+ worker.on('exit', (code) => {
50
+ if(exiting) return;
51
+ stop();
52
+ if (code !== 0) {
53
+ throw new Error(`Worker stopped with exit code ${code}`);
54
+ }
55
+ });
56
+
57
+ worker.postMessage({ type: 'start' });
58
+
59
+ return {
60
+ on: (e, cb) => listener.on(e, cb),
61
+ off: (e, cb) => listener.off(e, cb),
62
+ emit: (e, data) => worker?.postMessage({ type: 'event', event: e, data }),
63
+ get: () => dataResult.wait(),
64
+ stop: () => { exiting = true; stop(); }
65
+ };
66
+ }
67
+ };
68
+
69
+ return athread;
70
+ }
71
+ });
@@ -26,7 +26,7 @@ const defaultOptions = {
26
26
  onExit: () => process.exit(),
27
27
  style: '',
28
28
  stylePath: THEME_PATH,
29
- exec: () => {},
29
+ exec: () => { },
30
30
  execContext: {}
31
31
  };
32
32
 
@@ -44,18 +44,22 @@ module.exports = (context) => ({
44
44
 
45
45
  options.runId = runId;
46
46
 
47
- if(fs.existsSync(options.stylePath)) options.style = fs.readFileSync(options.stylePath, { encoding: 'utf-8' }) + '\n' + options.style;
47
+ if (fs.existsSync(options.stylePath)) options.style = fs.readFileSync(options.stylePath, { encoding: 'utf-8' }) + '\n' + options.style;
48
48
 
49
- options.style = ' */\n'+options.style+'\n/* ';
49
+ options.style = ' */\n' + options.style + '\n/* ';
50
50
 
51
51
  const HTML = replaceString(HTML_STRING, options);
52
52
  const JS = replaceString(JS_STRING, options);
53
53
 
54
+ /**
55
+ * Queue for future writes
56
+ * @type {string[]}
57
+ * */
54
58
  const queue = [];
55
59
 
56
60
  const send = (data) => {
57
61
  const content = fs.readFileSync(tmpFile, { encoding: 'utf-8' });
58
- if(content) {
62
+ if (content) {
59
63
  queue.push(data);
60
64
  } else {
61
65
  fs.writeFileSync(tmpFile, typeof data !== "string" ? JSON.stringify(data) : data);
@@ -77,20 +81,20 @@ module.exports = (context) => ({
77
81
  output: process.stdout
78
82
  });
79
83
 
80
- rl.question('', () => {});
84
+ rl.question('', () => { });
81
85
 
82
86
  fs.writeFileSync(tmpFile, '');
83
87
 
84
88
  fs.watch(tmpFile, { encoding: 'utf-8' })
85
- .on('change', () => {
86
- if(queue.length){
87
- send(queue.pop());
88
- }
89
- });
89
+ .on('change', () => {
90
+ if (queue.length) {
91
+ send(queue.pop());
92
+ }
93
+ });
90
94
 
91
95
  const p = spawn(BIN_PATH, [runId]);
92
96
 
93
-
97
+
94
98
 
95
99
  p.on("close", (code) => {
96
100
  rl.close();
@@ -103,12 +107,12 @@ module.exports = (context) => ({
103
107
  });
104
108
 
105
109
  g_emitter.on('recieve', (edata) => {
106
- if(edata.action.startsWith('hook:')){
110
+ if (edata.action.startsWith('hook:')) {
107
111
  const hook = hookedSocketListeners[edata.data.rid];
108
112
  const type = edata.action.split('hook:')[1];
109
- if(hook && hook.type == type) {
113
+ if (hook && hook.type == type) {
110
114
  hookedSocketListeners[edata.data.rid].cb(edata.data.object);
111
- if(hook.once) delete hookedSocketListeners[edata.data.rid];
115
+ if (hook.once) delete hookedSocketListeners[edata.data.rid];
112
116
  }
113
117
  }
114
118
  });
@@ -119,7 +123,7 @@ module.exports = (context) => ({
119
123
  const d = data.toString().split("RESPONSE::")[1];
120
124
  const jd = JSON.parse(d);
121
125
  recieve(jd);
122
- } else if(data.toString().trim().endsWith('SETUP::READY')) {
126
+ } else if (data.toString().trim().endsWith('SETUP::READY')) {
123
127
  console.log('READY');
124
128
  r(uiClasses(context, options, sendEvent, (cb) => {
125
129
  g_emitter.on('recieve', cb);
@@ -128,15 +132,15 @@ module.exports = (context) => ({
128
132
  }, (rid) => { // Remove hook
129
133
  delete hookedSocketListeners[rid];
130
134
  }));
131
- } else if(data.toString().endsWith('SETUP::HTML')) {
132
- send({action: 'JS2', data: JS, isSetup: true});
133
- } else if(data.toString() == 'INIT::READY') {
134
- send({action: 'HTML', data: HTML});
135
+ } else if (data.toString().endsWith('SETUP::HTML')) {
136
+ send({ action: 'JS2', data: JS, isSetup: true });
137
+ } else if (data.toString() == 'INIT::READY') {
138
+ send({ action: 'HTML', data: HTML });
135
139
  } else {
136
140
  console.log(data.toString());
137
141
  }
138
142
  });
139
-
143
+
140
144
  p.stderr.on("data", (data) => {
141
145
  console.error(data.toString());
142
146
  });
package/package.json CHANGED
@@ -1,16 +1,13 @@
1
1
  {
2
2
  "name": "@makano/rew",
3
- "version": "1.1.1",
3
+ "version": "1.1.5",
4
4
  "description": "A simple coffescript runtime",
5
5
  "main": "lib/rew/main.js",
6
6
  "directories": {
7
7
  "lib": "lib"
8
8
  },
9
9
  "scripts": {
10
- "test": "node test/test.js",
11
- "docs:dev": "vitepress dev docs",
12
- "docs:build": "vitepress build docs",
13
- "docs:preview": "vitepress preview docs"
10
+ "test": "node test/test.js"
14
11
  },
15
12
  "files": [
16
13
  "lib/",
@@ -32,7 +29,6 @@
32
29
  "chokidar": "^3.6.0",
33
30
  "js-yaml": "^4.1.0",
34
31
  "vm": "^0.1.0",
35
- "ws": "^8.17.0",
36
32
  "yargs": "^17.7.2"
37
33
  },
38
34
  "devDependencies": {