@makano/rew 1.2.5 → 1.2.6

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -5,7 +5,7 @@ const path = require('path');
5
5
  const { hideBin } = require('yargs/helpers');
6
6
  const { execSync } = require('child_process');
7
7
  const utils = require('./utils');
8
- const { existsSync, readFileSync, writeFileSync, mkdirSync, statSync } = require('fs');
8
+ const { existsSync, readFileSync, writeFileSync, mkdirSync, statSync, unlinkSync } = require('fs');
9
9
  const { log } = require('./log');
10
10
  const os = require('os');
11
11
  const crypto = require('crypto');
@@ -17,6 +17,8 @@ const { print, input } = require('../functions/stdout');
17
17
  const colors = require('colors');
18
18
  const { req } = require('../misc/req');
19
19
  const { gen_key } = require('../misc/bin');
20
+ const { REW_FILE_TYPE } = require('../const/ext');
21
+ const { generateRandomID } = require('../functions/id');
20
22
 
21
23
  if (!existsSync(CONFIG_PATH) || !existsSync(CONFIG_PATH + '/repos.yaml')) {
22
24
  mkdirSync(CONFIG_PATH, { recursive: true });
@@ -61,9 +63,36 @@ yargs(hideBin(process.argv))
61
63
  log('File not found:'.red.bold, argv.file, ':end');
62
64
  return;
63
65
  }
64
- utils.runFileWithArgv(filePath, { onlyCompile: argv.compile, watch: argv.watch });
66
+ utils.runFileWithArgv(filePath, { async: !process.stdin.isTTY, onlyCompile: argv.compile, watch: argv.watch });
65
67
  },
66
68
  )
69
+ .command(
70
+ 'exec [code]',
71
+ 'Executes in REPL',
72
+ (yargs) => {
73
+ yargs
74
+ .option('compile', {
75
+ alias: 'c',
76
+ describe: 'Compile and output the javascript',
77
+ type: 'boolean',
78
+ });
79
+ },
80
+ async (argv) => {
81
+ const replFile = '/tmp/rew-'+generateRandomID()+'-'+Date.now()+'.coffee';
82
+ let code = argv.code;
83
+ if(!process.stdin.isTTY) {
84
+ code = await utils.getAllPipeInput();
85
+ }
86
+ writeFileSync(replFile, code);
87
+ try{
88
+ utils.runFileWithArgv(replFile, { async: !process.stdin.isTTY, onlyCompile: argv.compile });
89
+ } catch(e){
90
+ console.error(e);
91
+ } finally {
92
+ unlinkSync(replFile);
93
+ }
94
+ }
95
+ )
67
96
  .command(
68
97
  'conf <command> [path] [key] [value]',
69
98
  'Configuration management',
@@ -127,7 +156,7 @@ yargs(hideBin(process.argv))
127
156
  type: 'string',
128
157
  })
129
158
  .option('dev', {
130
- describe: 'If your entry file is a .qrew, then just use the .coffee instead',
159
+ describe: `If your entry file is a .qrew, then just use .coffe or ${REW_FILE_TYPE.EXTENSION} instead`,
131
160
  type: 'boolean',
132
161
  })
133
162
  .option('entry', {
@@ -354,7 +383,7 @@ yargs(hideBin(process.argv))
354
383
  })
355
384
  .option('remove', {
356
385
  alias: 'r',
357
- describe: 'Remove all coffee',
386
+ describe: 'Remove all .coffee and '+REW_FILE_TYPE.EXTENSION,
358
387
  type: 'boolean',
359
388
  });
360
389
  },
@@ -362,4 +391,4 @@ yargs(hideBin(process.argv))
362
391
  utils.build(argv);
363
392
  },
364
393
  )
365
- .help(!isFileGiven).argv;
394
+ .help(!isFileGiven).argv;
@@ -18,6 +18,7 @@ const { seededID } = require('../misc/seededid');
18
18
  const loading = require('loading-cli');
19
19
  const sleep = require('../functions/sleep');
20
20
  const { gen_key } = require('../misc/bin');
21
+ const { REW_FILE_TYPE } = require('../const/ext');
21
22
 
22
23
  const binpath = path.join(conf({}).create('').root, '.bin');
23
24
  const logspath = path.join(conf({}).create('').root, '.logs');
@@ -35,7 +36,7 @@ module.exports = {
35
36
 
36
37
  const runIt = () => {
37
38
  if (options.watch) console.clear();
38
- const imports = execRewFile(filePath, [filePath, ...(argv || [])], { onlyCompile: options?.onlyCompile });
39
+ const imports = execRewFile(filePath, [filePath, ...(argv || [])], { onlyCompile: options?.onlyCompile, async: options?.async });
39
40
  if (options.watch) {
40
41
  imports.forEach((file) => {
41
42
  watchIt(file);
@@ -47,15 +48,21 @@ module.exports = {
47
48
  runIt();
48
49
  },
49
50
  runFileWithArgv(filePath, options = {}, cargv) {
50
- const argv = cargv || process.argv;
51
+ let argv = cargv || process.argv;
51
52
  argv.shift();
52
- if (argv[0].endsWith('rew')) {
53
+ if (argv[0].endsWith(REW_FILE_TYPE.EXTENSION) || argv[0].endsWith('.coffee')) {
53
54
  if (argv[1] == 'run') {
54
55
  argv.splice(0, 3);
55
56
  } else if(argv[1] == '-w' || argv[1] == '--watch'){
56
57
  argv.splice(0, 3);
57
58
  } else argv.splice(0, 2);
58
59
  }
60
+ if (argv[1] == 'exec') {
61
+ argv.splice(0, 2);
62
+ }
63
+ if (argv.includes('--')) {
64
+ argv = argv.slice(argv.indexOf('--') + 1, argv.length);
65
+ }
59
66
  this.runFile(filePath, options, argv)
60
67
  },
61
68
  conf(command, fullPath, key, value) {
@@ -118,8 +125,8 @@ module.exports = {
118
125
  const create = () => {
119
126
  fs.mkdirSync(projectPath, { recursive: true });
120
127
  const confPath = path.join(projectPath, 'app.yaml');
121
- const entryFile = path.join(projectPath, 'main.coffee');
122
- fs.writeFileSync(confPath, jsYaml.dump({ manifest: { package: project.package, private: false }, exec: { entry: 'main.coffee' }, assets: { icon: 'assets/icon.png', folder: './assets' }, install: { requirements: [] } }));
128
+ const entryFile = path.join(projectPath, 'main'+(project.civet ? REW_FILE_TYPE.EXTENSION : '.coffee'));
129
+ fs.writeFileSync(confPath, jsYaml.dump({ manifest: { package: project.package, private: false }, exec: { entry: 'main'+(project.civet ? REW_FILE_TYPE.EXTENSION : '.coffee') }, assets: { icon: 'assets/icon.png', folder: './assets' }, install: { requirements: [] } }));
123
130
  fs.writeFileSync(entryFile, `print("Hello World!")`);
124
131
  fs.mkdirSync(path.join(projectPath, 'assets'), { recursive: true });
125
132
  if (project.git) {
@@ -127,10 +134,14 @@ module.exports = {
127
134
  execSync('cd ' + projectPath + ' && git init . && git branch -m main', { stdio: 'ignore' });
128
135
  }
129
136
  if(project.intellisense){
130
- fs.copyFileSync(path.join(__dirname, '../../../jsconfig.json'), path.join(projectPath, 'jsconfig.json'));
137
+ fs.copyFileSync(path.join(__dirname, '../../../tsconfig.json'), path.join(projectPath, 'tsconfig.json'));
131
138
  fs.copyFileSync(path.join(__dirname, '../../../runtime.d.ts'), path.join(projectPath, 'runtime.d.ts'));
132
139
  }
133
140
  execSync('cd ' + projectPath + ' && npm init -y', { stdio: 'ignore' });
141
+ if(project.civet){
142
+ log('Installing NPM Packages');
143
+ execSync('cd '+projectPath+' && npm i @types/node --no-save', { stdio: 'ignore' });
144
+ }
134
145
  // log('Installing '+npm_package_name);
135
146
  // exec('cd '+projectPath+' && npm i '+npm_package_name, (err) => {
136
147
  // if(err){
@@ -149,9 +160,12 @@ module.exports = {
149
160
  project.package = pkg.trim();
150
161
  rl.question(logget(' Use intellisense declarations ? (y/N): '.magenta.bold), (inteli) => {
151
162
  project.intellisense = inteli.toLowerCase() == 'y' || inteli.toLowerCase() == 'yes';
152
- rl.question(logget('󰊢 Use git ? (y/N): '.yellow.bold), (use_git) => {
153
- project.git = use_git.toLowerCase() == 'y' || use_git.toLowerCase() == 'yes';
154
- create();
163
+ rl.question(logget(' Use Civet For main ? (y/N): '.blue.bold), (civet) => {
164
+ project.civet = civet.toLowerCase() == 'y' || civet.toLowerCase() == 'yes';
165
+ rl.question(logget('󰊢 Use git ? (y/N): '.yellow.bold), (use_git) => {
166
+ project.git = use_git.toLowerCase() == 'y' || use_git.toLowerCase() == 'yes';
167
+ create();
168
+ });
155
169
  });
156
170
  });
157
171
  } else {
@@ -173,8 +187,8 @@ module.exports = {
173
187
  c.exec.entry = c.exec[options.entry] || c.exec.entry;
174
188
  }
175
189
  if (c.exec.entry) {
176
- if (byPath && options.dev) c.exec.entry = c.entry.endsWith('.qrew') ? c.exec.entry.replace(/\.qrew$/, '.coffee') : c.exec.entry;
177
190
  let r = path.resolve(root, c.exec.entry);
191
+ if (byPath && options.dev) r = r.endsWith('.qrew') ? r.replace(/\.qrew$/, (a, b) => fs.existsSync(r.replace(a, '.coffee')) ? '.coffee' : REW_FILE_TYPE.EXTENSION) : r;
178
192
  if (options.build) {
179
193
  this.build({
180
194
  file: r,
@@ -182,7 +196,7 @@ module.exports = {
182
196
  });
183
197
  r = path.resolve(root, c.exec.entry.replace(new RegExp(path.extname(c.exec.entry).replace('.', '\\.') + '$'), options.translate ? '.js' : '.qrew'));
184
198
  }
185
- this.runFileWithArgv(r);
199
+ this.runFileWithArgv(r, { async: !process.stdin.isTTY });
186
200
  }
187
201
  };
188
202
 
@@ -357,6 +371,9 @@ module.exports = {
357
371
  if (fs.existsSync(importedFilePath)) {
358
372
  importsArray.push(importStatement);
359
373
  processFile(importedFilePath, importsArray);
374
+ } else if (fs.existsSync(importedFilePath + REW_FILE_TYPE.EXTENSION)) {
375
+ importsArray.push(importStatement);
376
+ processFile(importedFilePath + REW_FILE_TYPE.EXTENSION, importsArray);
360
377
  } else if (fs.existsSync(importedFilePath + '.coffee')) {
361
378
  importsArray.push(importStatement);
362
379
  processFile(importedFilePath + '.coffee', importsArray);
@@ -369,7 +386,7 @@ module.exports = {
369
386
 
370
387
  const appPath = findAppInfo(filePath);
371
388
 
372
- const compiled = argv.translate ? compile({ content }, {}) : to_qrew(content, appPath?.config?.manifest?.package || path.basename(filePath).split('.').slice(0, -1).join('.'));
389
+ const compiled = argv.translate ? compile({ content }, {}) : to_qrew(`"initFile ${filePath}"\n${path.basename(content)}`, appPath?.config?.manifest?.package || path.basename(filePath).split('.').slice(0, -1).join('.'));
373
390
  writeCompiledFile(filePath, compiled);
374
391
  }
375
392
 
@@ -531,5 +548,19 @@ module.exports = {
531
548
  fs.mkdirSync(binpath, { recursive: true });
532
549
  fs.mkdirSync(cachepath, { recursive: true });
533
550
  fs.mkdirSync(logspath, { recursive: true });
551
+ },
552
+ getAllPipeInput(){
553
+ return new Promise((resolve) => {
554
+ let data = '';
555
+ process.stdin.setEncoding('utf8');
556
+
557
+ process.stdin.on('data', (chunk) => {
558
+ data += chunk;
559
+ });
560
+
561
+ process.stdin.on('end', () => {
562
+ resolve(data);
563
+ });
564
+ });
534
565
  }
535
566
  };
@@ -7,7 +7,7 @@ const { match } = require('../functions/match');
7
7
  const { map } = require('../functions/map');
8
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, clear } = require('../functions/stdout');
10
+ const { print, input, clear, printf } = require('../functions/stdout');
11
11
  const { curl } = require('../functions/curl');
12
12
  const { wait } = require('../functions/wait');
13
13
  const { scheduleFrame } = require('../functions/misc');
@@ -55,5 +55,6 @@ module.exports = {
55
55
  curl,
56
56
 
57
57
  print,
58
+ printf,
58
59
  input,
59
60
  };
@@ -0,0 +1,5 @@
1
+
2
+ module.exports.REW_FILE_TYPE = {
3
+ EXTENSION: '.civet',
4
+ TYPE: 'civet'
5
+ }
@@ -1,10 +1,15 @@
1
+ const { REW_FILE_TYPE } = require("./ext");
2
+
1
3
  const execOptions = {
2
4
  sharedContext: true,
3
- resolveExtensions: ['.coffee', { ext: '.js', options: { type: 'js' } }, { ext: '.qrew', options: { qrew: true } }],
5
+ resolveExtensions: [REW_FILE_TYPE.EXTENSION, ".coffee", { ext: '.js', options: { type: 'js' } }, { ext: '.qrew', options: { qrew: true } }],
4
6
  nativeRequire: false,
7
+ useImport: false,
5
8
  cwdAlias: '$',
6
- jsxPragma: 'createElement',
9
+ jsxPragma: '__using__.JSX.createElement',
7
10
  jsx: false,
11
+ typescript: false,
12
+ decorators: false
8
13
  };
9
14
 
10
15
  module.exports.execOptions = execOptions;
@@ -0,0 +1,15 @@
1
+
2
+
3
+
4
+ module.exports.USING_DEFAULT = {
5
+ JSX: {
6
+ param: (param) => ({ createElement: param }),
7
+ use: (options) => options.jsx = true
8
+ },
9
+ TYPES: {
10
+ use: (options) => options.typescript = true
11
+ },
12
+ DECORATORS: {
13
+ use: (options) => options.decorators = true
14
+ }
15
+ }
@@ -10,7 +10,7 @@ function exportsThe(item, name, context) {
10
10
 
11
11
  module.exports.pubFunction = function (context) {
12
12
  return function (name, item) {
13
- if (name && !item) {
13
+ if (name && item == null) {
14
14
  item = name;
15
15
  name = null;
16
16
  }
@@ -31,7 +31,11 @@ module.exports = (currentFile) => {
31
31
  }
32
32
 
33
33
  function rm(filepath, options) {
34
- return fs.unlinkSync(filepath);
34
+ return fs.rmSync(gp(filepath), { recursive: true,...options });
35
+ }
36
+
37
+ function unlink(filepath, options) {
38
+ return fs.unlinkSync(gp(filepath));
35
39
  }
36
40
 
37
41
  function chmod(filepath, mode, options) {
@@ -51,6 +55,7 @@ module.exports = (currentFile) => {
51
55
  mkdir,
52
56
  chmod,
53
57
  rm,
58
+ unlink,
54
59
  fstat,
55
60
  exists,
56
61
  write,
@@ -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');
@@ -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
- ).context.module.exports;
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 == 'coffee') {
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 == 'coffee')) {
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) {
@@ -134,8 +140,7 @@ module.exports.imp = function (runPath, context) {
134
140
 
135
141
  //** Mock imports section
136
142
  /**/ if(!exports) exports = options.mock;
137
- /**/ if(options.mock == null) return null;
138
- /**/ if(!exports) throw new Error('Import '+filename+' does not export anything. Use the "mock" option to mock a value.');
143
+ /**/ if(options.mock === null) return null;
139
144
  //**
140
145
 
141
146
  return exports;
@@ -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
- return require(resolvedPath);
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 { compileFile } = require('./modules/compiler');
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
  };