@makano/rew 1.2.4 → 1.2.6
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/civet/main.js +17239 -0
- package/lib/rew/cli/cli.js +57 -5
- package/lib/rew/cli/log.js +5 -1
- package/lib/rew/cli/run.js +2 -2
- package/lib/rew/cli/utils.js +147 -60
- package/lib/rew/const/default.js +5 -1
- package/lib/rew/const/ext.js +5 -0
- package/lib/rew/const/opt.js +7 -2
- package/lib/rew/const/usage.js +15 -0
- package/lib/rew/functions/exec.js +2 -2
- package/lib/rew/functions/export.js +1 -1
- package/lib/rew/functions/fs.js +6 -1
- package/lib/rew/functions/id.js +2 -2
- package/lib/rew/functions/import.js +34 -13
- package/lib/rew/functions/require.js +17 -1
- package/lib/rew/functions/stdout.js +4 -0
- package/lib/rew/main.js +1 -13
- package/lib/rew/modules/compiler.js +122 -26
- package/lib/rew/modules/context.js +13 -1
- package/lib/rew/modules/runtime.js +37 -20
- package/lib/rew/pkgs/conf.js +1 -1
- package/lib/rew/pkgs/rune.js +8 -1
- package/lib/rew/pkgs/serve.js +373 -0
- package/lib/rew/pkgs/stream.js +10 -0
- package/lib/rew/pkgs/web.js +504 -0
- package/package.json +10 -5
- package/runtime.d.ts +943 -0
- package/lib/coffeescript/browser.js +0 -156
- package/lib/coffeescript/cake.js +0 -134
- package/lib/coffeescript/coffeescript.js +0 -465
- package/lib/coffeescript/command.js +0 -832
- package/lib/coffeescript/grammar.js +0 -1930
- package/lib/coffeescript/helpers.js +0 -513
- package/lib/coffeescript/index.js +0 -230
- package/lib/coffeescript/lexer.js +0 -2316
- package/lib/coffeescript/nodes.js +0 -9784
- package/lib/coffeescript/optparse.js +0 -258
- package/lib/coffeescript/parser.js +0 -20384
- package/lib/coffeescript/register.js +0 -120
- package/lib/coffeescript/repl.js +0 -328
- package/lib/coffeescript/rewriter.js +0 -1448
- package/lib/coffeescript/scope.js +0 -191
- package/lib/coffeescript/sourcemap.js +0 -244
@@ -6,9 +6,12 @@ const { existsSync, readFileSync } = require('fs');
|
|
6
6
|
const conf = require('../pkgs/conf');
|
7
7
|
const jsYaml = require('js-yaml');
|
8
8
|
const { execOptions } = require('../const/opt');
|
9
|
+
const { REW_FILE_TYPE } = require('../const/ext');
|
9
10
|
|
10
11
|
const cachedFiles = [];
|
11
|
-
|
12
|
+
module.exports.cleanCache = () => {
|
13
|
+
while(cachedFiles.length) cachedFiles.pop();
|
14
|
+
};
|
12
15
|
const lookUpInOtherApps = (fullPath) => {
|
13
16
|
const con = conf({});
|
14
17
|
const name = fullPath.indexOf('/') ? fullPath.split('/')[0] : fullPath;
|
@@ -32,7 +35,7 @@ const lookUpInOtherApps = (fullPath) => {
|
|
32
35
|
module.exports.imp = function (runPath, context) {
|
33
36
|
return function (filename, options = {}) {
|
34
37
|
if (!options) options = {};
|
35
|
-
let type = options.type || 'coffee';
|
38
|
+
let type = options.type || filename.endsWith('.coffee') ? 'coffee' : REW_FILE_TYPE.TYPE;
|
36
39
|
let exports,
|
37
40
|
ispkg = findPackage(filename);
|
38
41
|
|
@@ -43,8 +46,6 @@ module.exports.imp = function (runPath, context) {
|
|
43
46
|
let filepath = path.resolve(path.dirname(context.module.filepath), filename);
|
44
47
|
if(path.extname(filepath) == '.qrew') options.qrew = true;
|
45
48
|
|
46
|
-
// console.log(typeof runPath);
|
47
|
-
|
48
49
|
const lookUp = () => {
|
49
50
|
const otherPath = lookUpInOtherApps(filename);
|
50
51
|
if (!otherPath) throw new Error('Module "' + filename + '" not found');
|
@@ -57,7 +58,7 @@ module.exports.imp = function (runPath, context) {
|
|
57
58
|
exports = foundCache.exports;
|
58
59
|
}
|
59
60
|
|
60
|
-
if (!ispkg && !existsSync(filepath)) {
|
61
|
+
if (!ispkg && !existsSync(filepath) && !foundCache) {
|
61
62
|
if (Array.isArray(execOptions.resolveExtensions) && execOptions.resolveExtensions.length) {
|
62
63
|
const resolve = execOptions.resolveExtensions.find((ext) =>
|
63
64
|
typeof ext == 'string' ? existsSync(filepath + ext) : existsSync(filepath + (ext.ext || '')),
|
@@ -72,8 +73,8 @@ module.exports.imp = function (runPath, context) {
|
|
72
73
|
} else lookUp();
|
73
74
|
}
|
74
75
|
|
75
|
-
const exec = (coptions = {}) =>
|
76
|
-
runPath(
|
76
|
+
const exec = (coptions = {}) => {
|
77
|
+
const r = runPath(
|
77
78
|
filepath,
|
78
79
|
{
|
79
80
|
import: options,
|
@@ -86,13 +87,18 @@ module.exports.imp = function (runPath, context) {
|
|
86
87
|
package: context.app ? context.app.config.package : path.basename(filepath)
|
87
88
|
},
|
88
89
|
execOptions.sharedContext == false ? {} : options.context && options.context == 'new' ? {} : context,
|
89
|
-
)
|
90
|
+
);
|
91
|
+
if(r instanceof Promise){
|
92
|
+
return new Promise((resolve) => r.then(c => resolve(c.context.module.exports)));
|
93
|
+
}
|
94
|
+
return r.context.module.exports;
|
95
|
+
}
|
90
96
|
|
91
97
|
if (ispkg) {
|
92
|
-
const pkg = getPackage(filename)(context);
|
98
|
+
const pkg = getPackage(filename)(context, options);
|
93
99
|
exports = pkg._onImport ? pkg._onImport() : pkg;
|
94
100
|
} else if (foundCache) {
|
95
|
-
} else if (type ==
|
101
|
+
} else if (type == REW_FILE_TYPE.TYPE || type == "coffee") {
|
96
102
|
exports = exec({});
|
97
103
|
} else if (type == 'js') {
|
98
104
|
exports = exec({ compile: false });
|
@@ -111,7 +117,7 @@ module.exports.imp = function (runPath, context) {
|
|
111
117
|
}
|
112
118
|
}
|
113
119
|
|
114
|
-
if (options.save && (type == 'js' || type ==
|
120
|
+
if (options.save && (type == 'js' || type == REW_FILE_TYPE.TYPE || type == "coffee")) {
|
115
121
|
if (typeof options.save == 'string') context[options.save] = exports[i];
|
116
122
|
else
|
117
123
|
for (let i in exports) {
|
@@ -119,8 +125,23 @@ module.exports.imp = function (runPath, context) {
|
|
119
125
|
}
|
120
126
|
}
|
121
127
|
|
122
|
-
|
123
|
-
|
128
|
+
// Hehe, i just had an idea for a
|
129
|
+
// descriptive code
|
130
|
+
// you put them in comment blocks
|
131
|
+
// and name it something
|
132
|
+
// then you can simple see
|
133
|
+
// which part of a code contains a certain
|
134
|
+
// task. cool right?
|
135
|
+
|
136
|
+
//** If is not package, post exec section
|
137
|
+
/**/ if (!ispkg) context.module.imports.push(filepath);
|
138
|
+
/**/ if (!ispkg) cachedFiles.push({ filepath, exports });
|
139
|
+
//**
|
140
|
+
|
141
|
+
//** Mock imports section
|
142
|
+
/**/ if(!exports) exports = options.mock;
|
143
|
+
/**/ if(options.mock === null) return null;
|
144
|
+
//**
|
124
145
|
|
125
146
|
return exports;
|
126
147
|
};
|
@@ -1,10 +1,26 @@
|
|
1
1
|
const fs = require('fs');
|
2
2
|
const path = require('path');
|
3
|
+
const { execOptions } = require('../const/opt');
|
4
|
+
const { wait } = require('./wait');
|
5
|
+
|
6
|
+
const cahcedRequires = {};
|
7
|
+
|
8
|
+
const doImp = (path) => wait(async () => await import(resolvedPath));
|
3
9
|
|
4
10
|
module.exports.customRequire = function customRequire(modulePath, filePath) {
|
11
|
+
let pathname = modulePath;
|
12
|
+
if (modulePath.startsWith('./') || modulePath.startsWith('../') || path.isAbsolute(modulePath)) {
|
13
|
+
pathname = path.resolve(modulePath);
|
14
|
+
}
|
15
|
+
if(cahcedRequires[pathname]) {
|
16
|
+
return cahcedRequires[pathname];
|
17
|
+
}
|
5
18
|
const resolvedPath = resolveModulePath(modulePath, filePath);
|
6
19
|
if(!resolvedPath) throw new Error('Module '+modulePath+' not found');
|
7
|
-
|
20
|
+
const r = cahcedRequires[resolvedPath] ? cahcedRequires[resolvedPath] : execOptions.useImport ? doImp(resolvedPath) : require(resolvedPath);
|
21
|
+
if(!cahcedRequires[resolvedPath]) cahcedRequires[resolvedPath] = r;
|
22
|
+
if(!cahcedRequires[pathname]) cahcedRequires[pathname] = r;
|
23
|
+
return r;
|
8
24
|
};
|
9
25
|
|
10
26
|
function resolveModulePath(modulePath, filePath) {
|
@@ -4,6 +4,10 @@ const print = (module.exports.print = function print(...args) {
|
|
4
4
|
return console.log(...args);
|
5
5
|
});
|
6
6
|
|
7
|
+
module.exports.printf = function printf(buffer, cb) {
|
8
|
+
return process.stdout.write(buffer, cb);
|
9
|
+
};
|
10
|
+
|
7
11
|
print.stdout = process.stdout;
|
8
12
|
print.stdin = process.stdin;
|
9
13
|
|
package/lib/rew/main.js
CHANGED
@@ -1,17 +1,5 @@
|
|
1
|
-
const {
|
2
|
-
const { exec, runPath } = require('./modules/runtime');
|
3
|
-
const fs = require('fs');
|
4
|
-
const { imp } = require('./functions/import');
|
5
|
-
const { FILES } = require('./const/files');
|
1
|
+
const { runPath } = require('./modules/runtime');
|
6
2
|
|
7
3
|
module.exports.run = function (filepath, options = {}, custom_context = {}) {
|
8
|
-
FILES.forEach((file) => {
|
9
|
-
if (fs.existsSync(file.path)) return;
|
10
|
-
if (file.content) {
|
11
|
-
fs.writeFileSync(file.path, file.content);
|
12
|
-
} else {
|
13
|
-
fs.mkdirSync(file.path, { recursive: true });
|
14
|
-
}
|
15
|
-
});
|
16
4
|
return runPath(filepath, options, custom_context);
|
17
5
|
};
|
@@ -1,4 +1,4 @@
|
|
1
|
-
const { compile } = require('../../
|
1
|
+
const { compile: compileCivet } = require('../../civet/main');
|
2
2
|
const { execOptions } = require('../const/opt');
|
3
3
|
const { findAppInfo } = require('../misc/findAppInfo');
|
4
4
|
const { from_qrew } = require('../qrew/compile');
|
@@ -7,6 +7,9 @@ const babel = require('@babel/core');
|
|
7
7
|
const path = require('path');
|
8
8
|
const babelReact = require('@babel/preset-react');
|
9
9
|
const { readFileSync } = require('fs');
|
10
|
+
const { wait } = require('../functions/wait');
|
11
|
+
const { REW_FILE_TYPE } = require('../const/ext');
|
12
|
+
const { USING_DEFAULT } = require('../const/usage');
|
10
13
|
|
11
14
|
function tokenizeCoffeeScript(code) {
|
12
15
|
const tokens = [];
|
@@ -115,18 +118,25 @@ function compileRewStuff(content, options) {
|
|
115
118
|
const token = tokens[i];
|
116
119
|
let { token: nextToken, n } = gnextToken(i, 1, tokens) || {};
|
117
120
|
|
118
|
-
if
|
121
|
+
if(token.type == "COMMENT" && i < 2 && token.value.startsWith('#!')){
|
122
|
+
continue;
|
123
|
+
}
|
124
|
+
|
125
|
+
if (token.type === 'IDENTIFIER' && token.value === 'opt.set') {
|
119
126
|
const { token: nextNextToken } = gnextToken(i, 2, tokens) || {};
|
120
|
-
if (nextNextToken && nextNextToken.value == 'jsxPragma') {
|
127
|
+
if (nextNextToken && nextNextToken.value.slice(1).slice(0, -1) == 'jsxPragma') {
|
121
128
|
const { token: nextLastToken } = gnextToken(i, 5, tokens) || {};
|
122
129
|
execOptions.jsxPragma = nextLastToken.value.slice(1).slice(0, -1);
|
123
130
|
}
|
124
131
|
}
|
125
132
|
|
126
|
-
if (token.type === 'COMMENT' && token.value.slice(1).trim()
|
133
|
+
if (token.type === 'COMMENT' && token.value.slice(1).trim().startsWith('@jsx')) {
|
127
134
|
options.jsx = true;
|
135
|
+
if(token.value.split('@jsx')[1].trim()){
|
136
|
+
options.jsxPragma = token.value.split('@jsx')[1].trim();
|
137
|
+
}
|
128
138
|
}
|
129
|
-
|
139
|
+
|
130
140
|
if (token.type === 'COMMENT' && token.value.slice(1).trim() === '@cls') {
|
131
141
|
options.cls = true;
|
132
142
|
}
|
@@ -145,7 +155,21 @@ function compileRewStuff(content, options) {
|
|
145
155
|
continue;
|
146
156
|
}
|
147
157
|
|
148
|
-
|
158
|
+
|
159
|
+
if (token.type === 'IDENTIFIER' && token.value === 'export' && !options.keepImports) {
|
160
|
+
token.value = 'pub';
|
161
|
+
}
|
162
|
+
|
163
|
+
if (token.type === 'IDENTIFIER' && token.value === 'using' && !options.disableUse) {
|
164
|
+
const next = nextToken.value;
|
165
|
+
if(next in USING_DEFAULT) {
|
166
|
+
const { use } = USING_DEFAULT[next];
|
167
|
+
use?.(options);
|
168
|
+
nextToken.value = `"${nextToken.value}"`
|
169
|
+
}
|
170
|
+
}
|
171
|
+
|
172
|
+
if (token.type === 'IDENTIFIER' && token.value === 'import' && !options.keepImports) {
|
149
173
|
// console.log(nextToken.type);
|
150
174
|
let ind = i + n + 2;
|
151
175
|
|
@@ -160,9 +184,12 @@ function compileRewStuff(content, options) {
|
|
160
184
|
const exportsTokens = tokens.slice(ind, closingBraceToken.ti);
|
161
185
|
const exports = exportsTokens
|
162
186
|
.filter((t) => t.type === 'IDENTIFIER')
|
163
|
-
.map((t) => t.value)
|
187
|
+
.map((t, i, arr) => t.value == 'as' ? [arr[i-1].value +': '+arr[i+1].value] : t.value)
|
188
|
+
.flat(1)
|
189
|
+
.filter((t, i, arr) => !arr[i+1]?.match(':') && !arr[i-1]?.match(':'))
|
164
190
|
.join(', ');
|
165
|
-
|
191
|
+
|
192
|
+
result += `{ ${exports} } ${options.type == 'coffee' ? '=' : ':='} inc ${nameToken.value}`;
|
166
193
|
i = nameToken.ti;
|
167
194
|
}
|
168
195
|
} else if (nextToken.value === '*') {
|
@@ -171,7 +198,7 @@ function compileRewStuff(content, options) {
|
|
171
198
|
if (asToken) {
|
172
199
|
const nextToken = fnextToken(asToken.ti + 1, tokens, 'IDENTIFIER');
|
173
200
|
defaultName = nextToken.value;
|
174
|
-
result += `${defaultName} = inc ${nameToken.value}`;
|
201
|
+
result += `${defaultName} ${options.type == 'coffee' ? '=' : ':='} inc ${nameToken.value}`;
|
175
202
|
i = ind + 6;
|
176
203
|
}
|
177
204
|
} else if (nextToken) {
|
@@ -184,13 +211,15 @@ function compileRewStuff(content, options) {
|
|
184
211
|
const exportsTokens = tokens.slice(ind, closingBraceToken.ti);
|
185
212
|
const exports = exportsTokens
|
186
213
|
.filter((t) => t.type === 'IDENTIFIER')
|
187
|
-
.map((t) => t.value)
|
214
|
+
.map((t, i, arr) => t.value == 'as' ? [arr[i-1].value +': '+arr[i+1].value] : t.value)
|
215
|
+
.flat(1)
|
216
|
+
.filter((t, i, arr) => !arr[i+1]?.match(':') && !arr[i-1]?.match(':'))
|
188
217
|
.join(', ');
|
189
|
-
result += `{ default: ${defaultName}, ${exports} } = inc ${nameToken?.value || ''}`;
|
218
|
+
result += `{ default: ${defaultName}, ${exports} } ${options.type == 'coffee' ? '=' : ':='} inc ${nameToken?.value || ''}`;
|
190
219
|
i = closingBraceToken.ti + 4;
|
191
220
|
}
|
192
221
|
} else {
|
193
|
-
result += `{ default: ${defaultName} } = inc ${nameToken?.value || ''}`;
|
222
|
+
result += `{ default: ${defaultName} } ${options.type == 'coffee' ? '=' : ':='} inc ${nameToken?.value || ''}`;
|
194
223
|
i = ind + 2;
|
195
224
|
}
|
196
225
|
}
|
@@ -211,11 +240,18 @@ function compileRewStuff(content, options) {
|
|
211
240
|
nextToken &&
|
212
241
|
nextToken.type === 'IDENTIFIER' &&
|
213
242
|
nextToken.value &&
|
214
|
-
nextToken.value !== 'undefined'
|
243
|
+
nextToken.value !== 'undefined' && !options.keepImports
|
215
244
|
) {
|
245
|
+
let next = {...nextToken};
|
246
|
+
if(next.value == 'default'){
|
247
|
+
i += 2;
|
248
|
+
}
|
249
|
+
if(next.value == 'class'){
|
250
|
+
next.value = gnextToken(i, n + 1, tokens)?.token.value || "default";
|
251
|
+
}
|
216
252
|
hooks.push({
|
217
253
|
index: i + 1,
|
218
|
-
value: `"${
|
254
|
+
value: `"${next.value}", `,
|
219
255
|
});
|
220
256
|
}
|
221
257
|
|
@@ -235,32 +271,92 @@ function compileRewStuff(content, options) {
|
|
235
271
|
return result;
|
236
272
|
}
|
237
273
|
|
238
|
-
const
|
239
|
-
|
274
|
+
const compileCivetStuff = (file, options) => {
|
275
|
+
const compileOptions = {
|
240
276
|
...options,
|
241
|
-
|
242
|
-
|
277
|
+
bare: true,
|
278
|
+
filename: file.content,
|
243
279
|
inlineMap: false,
|
280
|
+
js: true
|
281
|
+
};
|
282
|
+
|
283
|
+
const prepared = compileRewStuff(file.content, options);
|
284
|
+
|
285
|
+
const compiled = options.async ? compileCivet(prepared, compileOptions) : wait(compileCivet, prepared, compileOptions);
|
286
|
+
|
287
|
+
return {
|
288
|
+
compiled,
|
289
|
+
options
|
290
|
+
};
|
291
|
+
}
|
292
|
+
|
293
|
+
const cpl = (module.exports.compile = function (file, options = {}) {
|
294
|
+
let compiledCode;
|
295
|
+
const result = compileCivetStuff(file, {
|
296
|
+
...options,
|
297
|
+
parseOptions: {
|
298
|
+
coffeeCompat: options.type == "coffee",
|
299
|
+
}
|
244
300
|
});
|
301
|
+
|
302
|
+
options = result.options;
|
303
|
+
compiledCode = result.compiled;
|
304
|
+
|
305
|
+
const babelify = (code, options) => babel.transformSync(code, {
|
306
|
+
presets: [
|
307
|
+
...(doJSX ? [[babelReact, { pragma: options.jsxPragma || execOptions.jsxPragma }]] : [])
|
308
|
+
],
|
309
|
+
plugins: [
|
310
|
+
...(doDecorators ? [[require('@babel/plugin-proposal-decorators'), { version: '2023-05' }], [require('@babel/plugin-proposal-class-properties'), { loose: true }], [require('@babel/plugin-transform-class-static-block'), {}]] : [])
|
311
|
+
],
|
312
|
+
}).code;
|
313
|
+
|
314
|
+
const doJSX = execOptions.jsx || options.jsx;
|
315
|
+
const doTypes = execOptions.typescript || options.typescript;
|
316
|
+
const doDecorators = execOptions.decorators || options.decorators;
|
317
|
+
const doBabel = doJSX || doTypes || doDecorators;
|
318
|
+
|
319
|
+
if(compiledCode instanceof Promise){
|
320
|
+
return compiledCode.then((compiledCode) => {
|
321
|
+
if (doBabel) {
|
322
|
+
compiledCode = babelify(compiledCode, options);
|
323
|
+
}
|
324
|
+
return compiledCode;
|
325
|
+
});
|
326
|
+
}
|
327
|
+
|
245
328
|
// console.log(c);
|
246
|
-
if (
|
247
|
-
|
248
|
-
presets: [[babelReact, { pragma: execOptions.jsxPragma }]],
|
249
|
-
plugins: [],
|
250
|
-
}).code;
|
329
|
+
if (doBabel) {
|
330
|
+
compiledCode = babelify(compiledCode, options);
|
251
331
|
}
|
252
|
-
return
|
332
|
+
return compiledCode;
|
253
333
|
});
|
254
334
|
|
255
335
|
module.exports.compileFile = function (filepath, options = {}) {
|
256
|
-
const f = getFile(filepath);
|
336
|
+
const f = typeof filepath == "object" ? filepath : getFile(filepath);
|
337
|
+
if(typeof filepath == "object") filepath = filepath.path;
|
338
|
+
let qrew = false;
|
257
339
|
|
258
340
|
if(options.qrew || path.extname(filepath) == '.qrew') {
|
341
|
+
qrew = true
|
259
342
|
f.content = from_qrew(readFileSync(f.path), options.package || findAppInfo(filepath)?.config.manifest.package || path.basename(filepath).split('.').slice(0, -1).join('.')).toString();
|
260
|
-
|
343
|
+
options.type = f.content.split('\n')[0]?.match(/"initFile (.+)"/)?.[1]?.split('.').pop();
|
344
|
+
}
|
261
345
|
|
262
346
|
let compiled_code = cpl(f, { ...options });
|
263
347
|
|
348
|
+
if(options.onlyCompile && !qrew){
|
349
|
+
if(compiled_code instanceof Promise){
|
350
|
+
compiled_code.then((r) => {
|
351
|
+
console.log(r);
|
352
|
+
process.exit();
|
353
|
+
});
|
354
|
+
} else {
|
355
|
+
console.log(compiled_code);
|
356
|
+
process.exit();
|
357
|
+
}
|
358
|
+
}
|
359
|
+
|
264
360
|
return {
|
265
361
|
compiled_code,
|
266
362
|
file: f,
|
@@ -7,6 +7,7 @@ const fsLib = require("../functions/fs");
|
|
7
7
|
const pathLib = require("../functions/path");
|
8
8
|
const execLib = require("../functions/exec");
|
9
9
|
const { findAppInfo } = require("../misc/findAppInfo");
|
10
|
+
const { USING_DEFAULT } = require("../const/usage");
|
10
11
|
|
11
12
|
let mainFile = "";
|
12
13
|
const isMainFile = (filepath) => filepath == mainFile;
|
@@ -25,11 +26,12 @@ module.exports.prepareContext = function (
|
|
25
26
|
imports: [],
|
26
27
|
},
|
27
28
|
imports: {
|
28
|
-
meta: {},
|
29
|
+
meta: { url: new URL('file://'+filepath), main: isMainFile(filepath) },
|
29
30
|
assert: options.import ?? {},
|
30
31
|
},
|
31
32
|
app: findAppInfo(filepath),
|
32
33
|
...fsLib(filepath),
|
34
|
+
__using__: {}
|
33
35
|
};
|
34
36
|
if (options.useContext) {
|
35
37
|
context = {
|
@@ -91,6 +93,16 @@ module.exports.prepareContext = function (
|
|
91
93
|
context.pub = pubFunction(context);
|
92
94
|
context.exports = exportsFunction(context);
|
93
95
|
|
96
|
+
context.using = (name, ...params) => {
|
97
|
+
if(USING_DEFAULT[name]){
|
98
|
+
if(USING_DEFAULT[name].param) {
|
99
|
+
context.__using__[name] = USING_DEFAULT[name].param(...params);
|
100
|
+
}
|
101
|
+
} else {
|
102
|
+
context.__using__[name] = params.length ? params.length > 1 ? [...params] : params : true;
|
103
|
+
}
|
104
|
+
};
|
105
|
+
|
94
106
|
if (
|
95
107
|
context.module.main ||
|
96
108
|
(options.fromMain == true && options.as == "main")
|
@@ -1,5 +1,5 @@
|
|
1
1
|
const vm = require('vm');
|
2
|
-
const { compileFile } = require('./compiler');
|
2
|
+
const { compileFile, compile } = require('./compiler');
|
3
3
|
const { prepareContext } = require('./context');
|
4
4
|
const { existsSync, readFileSync } = require('fs');
|
5
5
|
const { CONFIG_PATH } = require('../const/config_path');
|
@@ -7,37 +7,54 @@ const path = require('path');
|
|
7
7
|
|
8
8
|
const preScript = readFileSync(path.join(__dirname, '../const/pre-exec.js'), { encoding: 'utf-8' });
|
9
9
|
|
10
|
-
const exec = (module.exports.exec = function (code, context) {
|
11
|
-
return vm.runInNewContext(code, vm.createContext(context), {
|
10
|
+
const exec = (module.exports.exec = function (code, context, original = '') {
|
11
|
+
return vm.runInNewContext(code, context.do ? null : vm.createContext(context), {
|
12
12
|
filename: context.module.filepath,
|
13
|
-
lineOffset:
|
13
|
+
lineOffset: (original.split('\n').length + preScript.split('\n').length) - code.split('\n').length,
|
14
14
|
displayErrors: true,
|
15
15
|
});
|
16
16
|
});
|
17
17
|
|
18
18
|
module.exports.runPath = function runPath(filepath, options = {}, custom_context = {}) {
|
19
19
|
if(filepath.endsWith('.js')) options.type = 'js';
|
20
|
-
|
21
|
-
|
20
|
+
if(filepath.endsWith('.coffee')) options.type = 'coffee';
|
21
|
+
if(filepath.endsWith('.qrew')) options.type = 'qrew';
|
22
22
|
|
23
|
-
|
24
|
-
|
23
|
+
if(options.import?.async) options.async = true;
|
24
|
+
let { compiled_code, file } = compileFile(options.code ? { content: options.code, path: filepath } : filepath, options);
|
25
|
+
// context.module.compiled = compiled_code;
|
26
|
+
// context.process.exit = (int) => process.exit(int);
|
25
27
|
|
26
|
-
|
27
|
-
const
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
28
|
+
const doCode = () => {
|
29
|
+
const context = options.import?.takeThisContext ? custom_context : prepareContext(custom_context, options, file.path, runPath);
|
30
|
+
|
31
|
+
if(context.app){
|
32
|
+
const p = path.join(CONFIG_PATH, context.app.config.manifest.package, 'app');
|
33
|
+
if(existsSync(p) && context.app.path !== p){
|
34
|
+
console.log("App with the same package name has been installed. Conflicts happened. \nTo fix this, change your app's package name or remove the app making the conflict.");
|
35
|
+
return {
|
36
|
+
context: { module: { exports: null } },
|
37
|
+
returns: null
|
38
|
+
}
|
33
39
|
}
|
34
40
|
}
|
41
|
+
|
42
|
+
compiled_code = preScript+'\n'+compiled_code;
|
43
|
+
|
44
|
+
return {
|
45
|
+
context,
|
46
|
+
returns: exec(compiled_code, context, file.content),
|
47
|
+
};
|
35
48
|
}
|
36
49
|
|
37
|
-
|
50
|
+
if(options.async){
|
51
|
+
return new Promise(async (r, re) => {
|
52
|
+
compiled_code.then((e) => {
|
53
|
+
compiled_code = e;
|
54
|
+
r(doCode());
|
55
|
+
});
|
56
|
+
});
|
57
|
+
}
|
38
58
|
|
39
|
-
return
|
40
|
-
context,
|
41
|
-
returns: exec(compiled_code, context),
|
42
|
-
};
|
59
|
+
return doCode();
|
43
60
|
};
|
package/lib/rew/pkgs/conf.js
CHANGED
@@ -82,7 +82,7 @@ module.exports = (context) => ({
|
|
82
82
|
};
|
83
83
|
|
84
84
|
return {
|
85
|
-
get: (key, defaultValue) => getData(optionCenter, key)
|
85
|
+
get: (key, defaultValue) => getData(optionCenter, key) ?? defaultValue,
|
86
86
|
set: (key, value) => setData(optionCenter, key, value),
|
87
87
|
remove: (key) => removeData(optionCenter, key),
|
88
88
|
reset: () => fs.writeFileSync(optionCenter.root, dumpYaml(defaults)) && (conf[name] = defaults),
|
package/lib/rew/pkgs/rune.js
CHANGED
@@ -169,6 +169,7 @@ const createDB = (dbName, dirname, dbData = {}, encryptionKey) => {
|
|
169
169
|
};
|
170
170
|
|
171
171
|
const update = (caseRecord, newRecord) => {
|
172
|
+
|
172
173
|
let id;
|
173
174
|
if (typeof caseRecord === 'string') {
|
174
175
|
id = caseRecord;
|
@@ -224,6 +225,9 @@ const createDB = (dbName, dirname, dbData = {}, encryptionKey) => {
|
|
224
225
|
if (typeof criteria == 'string') return read(criteria);
|
225
226
|
if (!criteria || typeof criteria !== 'object') return null;
|
226
227
|
|
228
|
+
|
229
|
+
if (!fs.existsSync(collectionFilePath)) writeDataFile(collectionFilePath, []);
|
230
|
+
|
227
231
|
const data = readDataFile(collectionFilePath);
|
228
232
|
const record =
|
229
233
|
data.find((record) => {
|
@@ -240,7 +244,6 @@ const createDB = (dbName, dirname, dbData = {}, encryptionKey) => {
|
|
240
244
|
|
241
245
|
const remove = (id) => {
|
242
246
|
if ('@rune.id' in id) id = id['@rune.id'];
|
243
|
-
if (!fs.existsSync(collectionFilePath)) return false;
|
244
247
|
let data = readDataFile(collectionFilePath);
|
245
248
|
const index = data.findIndex((record) => record['@rune.id'] === id);
|
246
249
|
if (index !== -1) {
|
@@ -293,6 +296,8 @@ const createDB = (dbName, dirname, dbData = {}, encryptionKey) => {
|
|
293
296
|
return sortedData;
|
294
297
|
};
|
295
298
|
|
299
|
+
if (!fs.existsSync(collectionFilePath)) writeDataFile(collectionFilePath, []);
|
300
|
+
|
296
301
|
return {
|
297
302
|
insert,
|
298
303
|
read,
|
@@ -377,6 +382,8 @@ const createDB = (dbName, dirname, dbData = {}, encryptionKey) => {
|
|
377
382
|
return data;
|
378
383
|
};
|
379
384
|
|
385
|
+
if (!fs.existsSync(mapFilePath)) writeDataFile(mapFilePath, {});
|
386
|
+
|
380
387
|
return { set, get, remove, list, transform };
|
381
388
|
};
|
382
389
|
|