@makano/rew 1.2.5 → 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.
Files changed (38) hide show
  1. package/lib/civet/main.js +17239 -0
  2. package/lib/rew/cli/cli.js +34 -5
  3. package/lib/rew/cli/utils.js +43 -12
  4. package/lib/rew/const/default.js +2 -1
  5. package/lib/rew/const/ext.js +5 -0
  6. package/lib/rew/const/opt.js +7 -2
  7. package/lib/rew/const/usage.js +15 -0
  8. package/lib/rew/functions/export.js +1 -1
  9. package/lib/rew/functions/fs.js +6 -1
  10. package/lib/rew/functions/import.js +17 -12
  11. package/lib/rew/functions/require.js +17 -1
  12. package/lib/rew/functions/stdout.js +4 -0
  13. package/lib/rew/main.js +1 -13
  14. package/lib/rew/modules/compiler.js +103 -34
  15. package/lib/rew/modules/context.js +13 -1
  16. package/lib/rew/modules/runtime.js +37 -20
  17. package/lib/rew/pkgs/serve.js +373 -0
  18. package/lib/rew/pkgs/stream.js +10 -0
  19. package/lib/rew/pkgs/web.js +504 -0
  20. package/package.json +7 -5
  21. package/runtime.d.ts +718 -146
  22. package/jsconfig.json +0 -13
  23. package/lib/coffeescript/browser.js +0 -156
  24. package/lib/coffeescript/cake.js +0 -134
  25. package/lib/coffeescript/coffeescript.js +0 -465
  26. package/lib/coffeescript/command.js +0 -832
  27. package/lib/coffeescript/grammar.js +0 -1930
  28. package/lib/coffeescript/helpers.js +0 -513
  29. package/lib/coffeescript/index.js +0 -230
  30. package/lib/coffeescript/lexer.js +0 -2316
  31. package/lib/coffeescript/nodes.js +0 -9784
  32. package/lib/coffeescript/optparse.js +0 -258
  33. package/lib/coffeescript/parser.js +0 -20384
  34. package/lib/coffeescript/register.js +0 -120
  35. package/lib/coffeescript/repl.js +0 -328
  36. package/lib/coffeescript/rewriter.js +0 -1448
  37. package/lib/coffeescript/scope.js +0 -191
  38. package/lib/coffeescript/sourcemap.js +0 -244
@@ -1,4 +1,4 @@
1
- const { compile } = require('../../coffeescript/coffeescript');
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 (token.type === 'IDENTIFIER' && token.value === 'opt') {
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() === '@jsx') {
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
  }
@@ -146,19 +156,20 @@ function compileRewStuff(content, options) {
146
156
  }
147
157
 
148
158
 
149
- if (token.type === 'IDENTIFIER' && token.value === 'let') {
150
- result += '`'
151
- hooks.push({
152
- index: fnextToken(i, tokens, 'OTHER', ';').ti,
153
- value: `\``,
154
- });
159
+ if (token.type === 'IDENTIFIER' && token.value === 'export' && !options.keepImports) {
160
+ token.value = 'pub';
155
161
  }
156
162
 
157
- if (token.type === 'IDENTIFIER' && token.value === 'export') {
158
- token.value = 'pub';
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
+ }
159
170
  }
160
171
 
161
- if (token.type === 'IDENTIFIER' && token.value === 'import') {
172
+ if (token.type === 'IDENTIFIER' && token.value === 'import' && !options.keepImports) {
162
173
  // console.log(nextToken.type);
163
174
  let ind = i + n + 2;
164
175
 
@@ -173,9 +184,12 @@ function compileRewStuff(content, options) {
173
184
  const exportsTokens = tokens.slice(ind, closingBraceToken.ti);
174
185
  const exports = exportsTokens
175
186
  .filter((t) => t.type === 'IDENTIFIER')
176
- .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(':'))
177
190
  .join(', ');
178
- result += `{ ${exports} } = inc ${nameToken.value}`;
191
+
192
+ result += `{ ${exports} } ${options.type == 'coffee' ? '=' : ':='} inc ${nameToken.value}`;
179
193
  i = nameToken.ti;
180
194
  }
181
195
  } else if (nextToken.value === '*') {
@@ -184,7 +198,7 @@ function compileRewStuff(content, options) {
184
198
  if (asToken) {
185
199
  const nextToken = fnextToken(asToken.ti + 1, tokens, 'IDENTIFIER');
186
200
  defaultName = nextToken.value;
187
- result += `${defaultName} = inc ${nameToken.value}`;
201
+ result += `${defaultName} ${options.type == 'coffee' ? '=' : ':='} inc ${nameToken.value}`;
188
202
  i = ind + 6;
189
203
  }
190
204
  } else if (nextToken) {
@@ -197,13 +211,15 @@ function compileRewStuff(content, options) {
197
211
  const exportsTokens = tokens.slice(ind, closingBraceToken.ti);
198
212
  const exports = exportsTokens
199
213
  .filter((t) => t.type === 'IDENTIFIER')
200
- .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(':'))
201
217
  .join(', ');
202
- result += `{ default: ${defaultName}, ${exports} } = inc ${nameToken?.value || ''}`;
218
+ result += `{ default: ${defaultName}, ${exports} } ${options.type == 'coffee' ? '=' : ':='} inc ${nameToken?.value || ''}`;
203
219
  i = closingBraceToken.ti + 4;
204
220
  }
205
221
  } else {
206
- result += `{ default: ${defaultName} } = inc ${nameToken?.value || ''}`;
222
+ result += `{ default: ${defaultName} } ${options.type == 'coffee' ? '=' : ':='} inc ${nameToken?.value || ''}`;
207
223
  i = ind + 2;
208
224
  }
209
225
  }
@@ -224,7 +240,7 @@ function compileRewStuff(content, options) {
224
240
  nextToken &&
225
241
  nextToken.type === 'IDENTIFIER' &&
226
242
  nextToken.value &&
227
- nextToken.value !== 'undefined'
243
+ nextToken.value !== 'undefined' && !options.keepImports
228
244
  ) {
229
245
  let next = {...nextToken};
230
246
  if(next.value == 'default'){
@@ -255,37 +271,90 @@ function compileRewStuff(content, options) {
255
271
  return result;
256
272
  }
257
273
 
258
- const cpl = (module.exports.compile = function (file, options = {}) {
259
- let c = options.type == 'js' || options.compile == false ? file.content : compile(compileRewStuff(file.content, options), {
274
+ const compileCivetStuff = (file, options) => {
275
+ const compileOptions = {
260
276
  ...options,
261
- filename: file.path,
262
- bare: false,
277
+ bare: true,
278
+ filename: file.content,
263
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
+ }
264
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
+
265
328
  // console.log(c);
266
- if (execOptions.jsx || options.jsx) {
267
- c = babel.transformSync(c, {
268
- presets: [[babelReact, { pragma: execOptions.jsxPragma }]],
269
- plugins: [],
270
- }).code;
329
+ if (doBabel) {
330
+ compiledCode = babelify(compiledCode, options);
271
331
  }
272
- return c;
332
+ return compiledCode;
273
333
  });
274
334
 
275
335
  module.exports.compileFile = function (filepath, options = {}) {
276
- const f = getFile(filepath);
336
+ const f = typeof filepath == "object" ? filepath : getFile(filepath);
337
+ if(typeof filepath == "object") filepath = filepath.path;
277
338
  let qrew = false;
278
339
 
279
340
  if(options.qrew || path.extname(filepath) == '.qrew') {
280
341
  qrew = true
281
342
  f.content = from_qrew(readFileSync(f.path), options.package || findAppInfo(filepath)?.config.manifest.package || path.basename(filepath).split('.').slice(0, -1).join('.')).toString();
343
+ options.type = f.content.split('\n')[0]?.match(/"initFile (.+)"/)?.[1]?.split('.').pop();
282
344
  }
283
345
 
284
346
  let compiled_code = cpl(f, { ...options });
285
347
 
286
348
  if(options.onlyCompile && !qrew){
287
- console.log(compiled_code);
288
- process.exit();
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
+ }
289
358
  }
290
359
 
291
360
  return {
@@ -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: -1 - preScript.split('\n').length,
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
- let { compiled_code, file } = compileFile(filepath, options);
21
- const context = prepareContext(custom_context, options, file.path, runPath);
20
+ if(filepath.endsWith('.coffee')) options.type = 'coffee';
21
+ if(filepath.endsWith('.qrew')) options.type = 'qrew';
22
22
 
23
- context.module.compiled = compiled_code;
24
- context.process.exit = (int) => process.exit(int);
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
- if(context.app){
27
- const p = path.join(CONFIG_PATH, context.app.config.manifest.package, 'app');
28
- if(existsSync(p) && context.app.path !== p){
29
- 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.");
30
- return {
31
- context: { exports: null },
32
- returns: null
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
- compiled_code = preScript+compiled_code;
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
  };