@makano/rew 1.1.15 → 1.1.21
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/rew/const/default.js +11 -4
- package/lib/rew/const/opt.js +2 -1
- package/lib/rew/functions/core.js +1 -0
- package/lib/rew/functions/fs.js +54 -0
- package/lib/rew/functions/path.js +13 -0
- package/lib/rew/functions/require.js +2 -6
- package/lib/rew/functions/stdout.js +30 -0
- package/lib/rew/functions/types.js +29 -1
- package/lib/rew/modules/context.js +11 -8
- package/lib/rew/pkgs/modules/threads/worker.js +1 -1
- package/lib/rew/pkgs/threads.js +5 -1
- package/package.json +1 -1
package/lib/rew/const/default.js
CHANGED
@@ -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
|
33
|
-
|
34
|
-
},
|
40
|
+
print,
|
41
|
+
input
|
35
42
|
};
|
package/lib/rew/const/opt.js
CHANGED
@@ -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(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
|
+
}
|
@@ -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
|
+
}
|
@@ -3,12 +3,8 @@ const path = require('path');
|
|
3
3
|
|
4
4
|
|
5
5
|
module.exports.customRequire = function customRequire(modulePath, filePath) {
|
6
|
-
|
7
|
-
|
8
|
-
} catch(e){
|
9
|
-
const resolvedPath = resolveModulePath(modulePath, filePath);
|
10
|
-
return require(resolvedPath);
|
11
|
-
}
|
6
|
+
const resolvedPath = resolveModulePath(modulePath, filePath);
|
7
|
+
return require(resolvedPath);
|
12
8
|
}
|
13
9
|
|
14
10
|
function resolveModulePath(modulePath, filePath) {
|
@@ -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
|
|
@@ -4,7 +4,8 @@ const emitter = require("../functions/emitter");
|
|
4
4
|
const { exportsFunction } = require("../functions/export");
|
5
5
|
const { imp } = require("../functions/import");
|
6
6
|
const { customRequire } = require("../functions/require");
|
7
|
-
|
7
|
+
const fsLib = require('../functions/fs');
|
8
|
+
const pathLib = require('../functions/path');
|
8
9
|
|
9
10
|
module.exports.prepareContext = function (
|
10
11
|
custom_context,
|
@@ -19,12 +20,7 @@ module.exports.prepareContext = function (
|
|
19
20
|
filepath,
|
20
21
|
imports: []
|
21
22
|
},
|
22
|
-
|
23
|
-
set: (key, value) => execOptions[key] = value,
|
24
|
-
get: (key) => execOptions[key],
|
25
|
-
push: (key, value) => execOptions[key]?.push(value),
|
26
|
-
pop: (key) => execOptions[key]?.pop()
|
27
|
-
}
|
23
|
+
...fsLib(filepath)
|
28
24
|
};
|
29
25
|
if (options.useContext) {
|
30
26
|
context = {
|
@@ -35,13 +31,20 @@ module.exports.prepareContext = function (
|
|
35
31
|
context = {
|
36
32
|
...context,
|
37
33
|
...defaultContext,
|
34
|
+
...pathLib(filepath),
|
38
35
|
require: (package) => {
|
39
36
|
try {
|
40
|
-
return execOptions.nativeRequire ? require(package) : customRequire(package, filepath);
|
37
|
+
return execOptions.nativeRequire || package.startsWith('node:') ? require(package.startsWith('node:') ? package.split('node:')[1] : package) : customRequire(package, filepath);
|
41
38
|
} catch (e) {
|
42
39
|
throw new Error("Module "+package+" not found");
|
43
40
|
}
|
44
41
|
},
|
42
|
+
opt: {
|
43
|
+
set: (key, value) => execOptions[key] = value,
|
44
|
+
get: (key) => execOptions[key],
|
45
|
+
push: (key, value) => execOptions[key]?.push(value),
|
46
|
+
pop: (key) => execOptions[key]?.pop()
|
47
|
+
},
|
45
48
|
...custom_context,
|
46
49
|
};
|
47
50
|
context.imp = imp(runPath, context);
|
@@ -22,7 +22,7 @@ parentPort.on('message', (data) => {
|
|
22
22
|
exec(`(${workerData.cb}).call({ process }, context)`, { print: (...a) => console.log(...a), process: {
|
23
23
|
on: (e, cb) => { target.on(e, cb) },
|
24
24
|
off: (e) => { target.off(e) },
|
25
|
-
emit: (e) => { parentPort.postMessage({ type: 'event', event: e, data }) },
|
25
|
+
emit: (e, data) => { parentPort.postMessage({ type: 'event', event: e, data }) },
|
26
26
|
exit: (code) => process.exit(code),
|
27
27
|
finish: (data) => {
|
28
28
|
parentPort.postMessage({ type: 'result', result: data });
|
package/lib/rew/pkgs/threads.js
CHANGED
@@ -44,7 +44,10 @@ module.exports = (context) => ({
|
|
44
44
|
stop();
|
45
45
|
});
|
46
46
|
|
47
|
+
let exiting = false;
|
48
|
+
|
47
49
|
worker.on('exit', (code) => {
|
50
|
+
if(exiting) return;
|
48
51
|
stop();
|
49
52
|
if (code !== 0) {
|
50
53
|
throw new Error(`Worker stopped with exit code ${code}`);
|
@@ -57,7 +60,8 @@ module.exports = (context) => ({
|
|
57
60
|
on: (e, cb) => listener.on(e, cb),
|
58
61
|
off: (e, cb) => listener.off(e, cb),
|
59
62
|
emit: (e, data) => worker?.postMessage({ type: 'event', event: e, data }),
|
60
|
-
get: () => dataResult.wait()
|
63
|
+
get: () => dataResult.wait(),
|
64
|
+
stop: () => { exiting = true; stop(); }
|
61
65
|
};
|
62
66
|
}
|
63
67
|
};
|