@makano/rew 1.1.25 → 1.1.56

Sign up to get free protection for your applications and to get access to all the features.
@@ -72,15 +72,16 @@ module.exports = {
72
72
  fs.writeFileSync(path.join(projectPath, '.gitignore'), `node_modules/\npackage-lock.json`);
73
73
  execSync('cd '+projectPath+' && git init .');
74
74
  }
75
- log('Installing '+npm_package_name);
76
- exec('cd '+projectPath+' && npm i '+npm_package_name, (err) => {
77
- if(err){
78
- console.error(err);
79
- process.exit(0);
80
- } else {
81
- rl.close();
82
- }
83
- });
75
+ // log('Installing '+npm_package_name);
76
+ // exec('cd '+projectPath+' && npm i '+npm_package_name, (err) => {
77
+ // if(err){
78
+ // console.error(err);
79
+ // process.exit(0);
80
+ // } else {
81
+ // rl.close();
82
+ // }
83
+ // });
84
+ log('Done.', ':end');
84
85
  }
85
86
  if (!fs.existsSync(projectPath)) {
86
87
  rl.question(logget('Package Name: '), (pkg) => {
@@ -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
+
@@ -14,17 +14,18 @@ const lookUpInOtherApps = (fullPath) => {
14
14
  const name = fullPath.indexOf('/') ? fullPath.split('/')[0] : fullPath;
15
15
  let dpath = fullPath.indexOf('/') ? fullPath.split('/')[1] : '';
16
16
  let ppath = path.join(con.CONFIG_PATH, name, 'app');
17
- if(!existsSync(ppath)) return null;
18
- if(!dpath){
17
+ if (!existsSync(ppath)) return null;
18
+ if (!dpath) {
19
19
  dpath = jsYaml.load(readFileSync(path.join(ppath, 'app.yaml'), 'utf-8')).entry;
20
20
  }
21
21
  ppath = path.join(ppath, dpath);
22
- if(existsSync(ppath)) return ppath;
22
+ if (existsSync(ppath)) return ppath;
23
23
  else return null;
24
24
  }
25
25
 
26
26
  module.exports.imp = function (runPath, context) {
27
27
  return function (filename, options = {}) {
28
+ if (!options) options = {};
28
29
  let type = options.type || "coffee";
29
30
  let exports,
30
31
  ispkg = findPackage(filename);
@@ -35,24 +36,24 @@ module.exports.imp = function (runPath, context) {
35
36
 
36
37
  const lookUp = () => {
37
38
  const otherPath = lookUpInOtherApps(filename);
38
- if(!otherPath) throw new Error('Module "'+filename+'" not found');
39
+ if (!otherPath) throw new Error('Module "' + filename + '" not found');
39
40
  else filepath = otherPath;
40
41
  }
41
42
 
42
43
  const foundCache = cachedFiles.find(f => f.filepath == filepath);
43
44
 
44
- if(!ispkg && foundCache){
45
+ if (!ispkg && foundCache) {
45
46
  exports = foundCache.exports;
46
47
  }
47
48
 
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) {
49
+ if (!ispkg && !existsSync(filepath)) {
50
+ if (Array.isArray(execOptions.resolveExtensions) && execOptions.resolveExtensions.length) {
51
+ const resolve = execOptions.resolveExtensions.find(ext => typeof ext == "string" ? existsSync(filepath + ext) : existsSync(filepath + (ext.ext || '')));
52
+ if (resolve) {
52
53
  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];
54
+ if (typeof resolve == "object" && resolve.options) {
55
+ if (resolve.options.type) type = resolve.options.type;
56
+ for (let i in resolve.options) options[i] = resolve.options[i];
56
57
  }
57
58
  } else lookUp();
58
59
  } else lookUp();
@@ -60,16 +61,24 @@ module.exports.imp = function (runPath, context) {
60
61
 
61
62
  const exec = (coptions = {}) => runPath(
62
63
  filepath,
63
- { ...options, useContext: execOptions.sharedContext == false ? false : true, ...coptions },
64
- execOptions.sharedContext == false ? {} : context,
64
+ {
65
+ import: options,
66
+ main: false,
67
+ useContext: execOptions.sharedContext == false ? false : !(options.context && options.context == 'new'),
68
+ ...coptions,
69
+ as: options.as == 'main' ? context.module.main ? 'main' : 'parent' : options.as == 'parent' ? 'parent' : 'child',
70
+ fromMain: context.module.main
71
+ },
72
+ execOptions.sharedContext == false ? {} :
73
+ options.context && options.context == 'new' ? {} : context,
65
74
  ).context.module.exports;
66
75
 
67
76
  if (ispkg) {
68
77
  exports = getPackage(filename)(context);
69
- } else if(foundCache) {
70
-
78
+ } else if (foundCache) {
79
+
71
80
  } else if (type == "coffee") {
72
- exports = exec({ });
81
+ exports = exec({});
73
82
  } else if (type == "js") {
74
83
  exports = exec({ compile: false });
75
84
  } else if (type == "yaml" || type == "json" || type == "text") {
@@ -95,8 +104,8 @@ module.exports.imp = function (runPath, context) {
95
104
  }
96
105
  }
97
106
 
98
- if(!ispkg) context.module.imports.push(filepath);
99
- if(!ispkg) cachedFiles.push({ filepath, exports });
107
+ if (!ispkg) context.module.imports.push(filepath);
108
+ if (!ispkg) cachedFiles.push({ filepath, exports });
100
109
 
101
110
  return exports;
102
111
  };
@@ -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,37 +1,46 @@
1
1
  const defaultContext = require("../const/default");
2
2
  const { execOptions } = require("../const/opt");
3
3
  const emitter = require("../functions/emitter");
4
- const { exportsFunction } = require("../functions/export");
4
+ const { exportsFunction, pubFunction } = 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
8
  const pathLib = require('../functions/path');
9
+ const execLib = require('../functions/exec');
9
10
 
11
+ let mainFile = "";
12
+ const isMainFile = filepath => filepath == mainFile;
10
13
  module.exports.prepareContext = function (
11
14
  custom_context,
12
15
  options,
13
16
  filepath = "",
14
17
  runPath = () => {},
15
18
  ) {
19
+ if(mainFile == "") mainFile = filepath;
16
20
  let context = {
17
21
  module: {
18
22
  exports: null,
19
- options,
20
23
  filepath,
24
+ main: isMainFile(filepath),
21
25
  imports: []
22
26
  },
23
- ...fsLib(filepath)
27
+ imports: {
28
+ meta: {},
29
+ assert: options.import ?? {}
30
+ },
31
+ ...fsLib(filepath),
24
32
  };
25
33
  if (options.useContext) {
26
34
  context = {
27
- ...context,
28
35
  ...custom_context,
36
+ ...context
29
37
  };
30
38
  } else {
31
39
  context = {
32
40
  ...context,
33
41
  ...defaultContext,
34
42
  ...pathLib(filepath),
43
+ ...execLib(filepath),
35
44
  require: (package) => {
36
45
  try {
37
46
  return execOptions.nativeRequire || package.startsWith('node:') ? require(package.startsWith('node:') ? package.split('node:')[1] : package) : customRequire(package, filepath);
@@ -39,26 +48,43 @@ module.exports.prepareContext = function (
39
48
  throw new Error("Module "+package+" not found");
40
49
  }
41
50
  },
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
- },
48
51
  ...custom_context,
49
52
  };
50
- context.imp = imp(runPath, context);
51
- context.exports = exportsFunction(context);
52
53
  }
53
- if (!context.global) context.global = context;
54
54
  if (!context.process)
55
55
  context.process = {
56
56
  argv: process.argv,
57
- target: emitter(),
57
+ target: {
58
+ on: (event, listener) => process.on(event, listener),
59
+ off: (event, listener) => process.off(event, listener),
60
+ emit: (event, code) => process.emit(event, code)
61
+ },
58
62
  env: process.env,
59
63
  cwd: () => process.cwd(),
60
64
  arch: process.arch
61
65
  };
62
- // console.log(custom_context);
66
+
67
+ context.global = context;
68
+ context.imports.assert = options.import ?? {};
69
+ context.imp = imp(runPath, context);
70
+ context.inc = (package, asserts) => {
71
+ try{
72
+ if(package.startsWith('node:') || package.startsWith('pkg:')) throw new Error('');
73
+ return context.imp(package, asserts);
74
+ } catch(e) {
75
+ return context.require(package.startsWith('pkg:') ? package.split('pkg:')[1] : package);
76
+ }
77
+ };
78
+ context.pub = pubFunction(context);
79
+ context.exports = exportsFunction(context);
80
+
81
+ if(context.module.main || (options.fromMain == true && options.as == 'main')){
82
+ context.opt = {
83
+ set: (key, value) => execOptions[key] = value,
84
+ get: (key) => execOptions[key],
85
+ push: (key, value) => execOptions[key]?.push(value),
86
+ pop: (key) => execOptions[key]?.pop()
87
+ };
88
+ } else delete context.opt
63
89
  return context;
64
90
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@makano/rew",
3
- "version": "1.1.25",
3
+ "version": "1.1.56",
4
4
  "description": "A simple coffescript runtime",
5
5
  "main": "lib/rew/main.js",
6
6
  "directories": {