@makano/rew 1.1.21 → 1.1.25

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.
@@ -6,8 +6,9 @@ const { hideBin } = require('yargs/helpers');
6
6
  const { fork, exec } = require('child_process');
7
7
  const { watch } = require('chokidar');
8
8
  const utils = require('./utils');
9
- const { existsSync } = require('fs');
9
+ const { existsSync, readFileSync, writeFileSync, mkdirSync } = require('fs');
10
10
  const { log } = require('./log');
11
+ const { compileFile, compile } = require('../modules/compiler');
11
12
 
12
13
  yargs(hideBin(process.argv))
13
14
  .command(
@@ -125,9 +126,69 @@ yargs(hideBin(process.argv))
125
126
  .positional('file', {
126
127
  describe: 'File to build',
127
128
  type: 'string',
129
+ })
130
+ .option('output', {
131
+ alias: 'o',
132
+ describe: 'Output directory',
133
+ type: 'string',
128
134
  });
129
135
  }, (argv) => {
130
- console.log(`Building file: ${argv.file}`);
136
+
137
+ function readFile(filePath) {
138
+ return readFileSync(filePath, { encoding: 'utf-8' });
139
+ }
140
+
141
+ function extractImports(content) {
142
+ const importRegex = /(\w+)\s*=\s*imp\s*['"](.+?)['"]/g;
143
+ const imports = [];
144
+ let match;
145
+ while ((match = importRegex.exec(content)) !== null) {
146
+ imports.push({ variable: match[1], url: match[2] });
147
+ }
148
+ return imports;
149
+ }
150
+
151
+ function writeCompiledFile(filePath, compiledCode) {
152
+ const dirName = outputDir ? outputDir : path.dirname(filePath);
153
+ if(!existsSync(dirName)) mkdirSync(dirName, { recursive: true });
154
+ const baseName = path.basename(filePath, path.extname(filePath));
155
+ const newFilePath = path.join(dirName, `${baseName}.js`);
156
+ writeFileSync(newFilePath, compiledCode, { encoding: 'utf-8' });
157
+ log(`Compiled: ${newFilePath}`);
158
+ }
159
+
160
+ function processFile(filePath, importsArray) {
161
+ const content = readFile(filePath);
162
+ const imports = extractImports(content);
163
+
164
+ imports.forEach(importStatement => {
165
+ const importedFilePath = path.resolve(path.dirname(filePath), importStatement.url);
166
+ if (!importsArray.some(importObj => importObj.url === importStatement.url)) {
167
+
168
+ if(existsSync(importedFilePath)){
169
+ importsArray.push(importStatement);
170
+ processFile(importedFilePath, importsArray);
171
+ } else if(existsSync(importedFilePath+'.coffee')){
172
+ importsArray.push(importStatement);
173
+ processFile(importedFilePath+'.coffee', importsArray);
174
+ } else if(existsSync(importedFilePath+'.js')){
175
+ importsArray.push(importStatement);
176
+ processFile(importedFilePath+'.js', importsArray);
177
+ }
178
+
179
+ }
180
+ });
181
+
182
+ const compiled = compile({ content }, {});
183
+ writeCompiledFile(filePath, compiled);
184
+ }
185
+
186
+ const filePath = path.resolve(process.cwd(), argv.file);
187
+ const importsArray = [];
188
+ const outputDir = argv.output ? path.resolve(process.cwd(), argv.output) : null;
189
+ log('Start compile at', outputDir || 'default path');
190
+ processFile(filePath, importsArray);
191
+ log('Compiled', importsArray.length + 1, 'files.', ':end');
131
192
  })
132
193
  .help()
133
194
  .argv;
@@ -18,7 +18,7 @@ module.exports = (currentFile) => {
18
18
  }
19
19
 
20
20
  function exists(filepath, options){
21
- return fs.existsSync(filepath);
21
+ return fs.existsSync(gp(filepath));
22
22
  }
23
23
 
24
24
  function fstat(filepath, options){
@@ -26,7 +26,7 @@ const defaultOptions = {
26
26
  onExit: () => process.exit(),
27
27
  style: '',
28
28
  stylePath: THEME_PATH,
29
- exec: () => {},
29
+ exec: () => { },
30
30
  execContext: {}
31
31
  };
32
32
 
@@ -44,18 +44,22 @@ module.exports = (context) => ({
44
44
 
45
45
  options.runId = runId;
46
46
 
47
- if(fs.existsSync(options.stylePath)) options.style = fs.readFileSync(options.stylePath, { encoding: 'utf-8' }) + '\n' + options.style;
47
+ if (fs.existsSync(options.stylePath)) options.style = fs.readFileSync(options.stylePath, { encoding: 'utf-8' }) + '\n' + options.style;
48
48
 
49
- options.style = ' */\n'+options.style+'\n/* ';
49
+ options.style = ' */\n' + options.style + '\n/* ';
50
50
 
51
51
  const HTML = replaceString(HTML_STRING, options);
52
52
  const JS = replaceString(JS_STRING, options);
53
53
 
54
+ /**
55
+ * Queue for future writes
56
+ * @type {string[]}
57
+ * */
54
58
  const queue = [];
55
59
 
56
60
  const send = (data) => {
57
61
  const content = fs.readFileSync(tmpFile, { encoding: 'utf-8' });
58
- if(content) {
62
+ if (content) {
59
63
  queue.push(data);
60
64
  } else {
61
65
  fs.writeFileSync(tmpFile, typeof data !== "string" ? JSON.stringify(data) : data);
@@ -77,20 +81,20 @@ module.exports = (context) => ({
77
81
  output: process.stdout
78
82
  });
79
83
 
80
- rl.question('', () => {});
84
+ rl.question('', () => { });
81
85
 
82
86
  fs.writeFileSync(tmpFile, '');
83
87
 
84
88
  fs.watch(tmpFile, { encoding: 'utf-8' })
85
- .on('change', () => {
86
- if(queue.length){
87
- send(queue.pop());
88
- }
89
- });
89
+ .on('change', () => {
90
+ if (queue.length) {
91
+ send(queue.pop());
92
+ }
93
+ });
90
94
 
91
95
  const p = spawn(BIN_PATH, [runId]);
92
96
 
93
-
97
+
94
98
 
95
99
  p.on("close", (code) => {
96
100
  rl.close();
@@ -103,12 +107,12 @@ module.exports = (context) => ({
103
107
  });
104
108
 
105
109
  g_emitter.on('recieve', (edata) => {
106
- if(edata.action.startsWith('hook:')){
110
+ if (edata.action.startsWith('hook:')) {
107
111
  const hook = hookedSocketListeners[edata.data.rid];
108
112
  const type = edata.action.split('hook:')[1];
109
- if(hook && hook.type == type) {
113
+ if (hook && hook.type == type) {
110
114
  hookedSocketListeners[edata.data.rid].cb(edata.data.object);
111
- if(hook.once) delete hookedSocketListeners[edata.data.rid];
115
+ if (hook.once) delete hookedSocketListeners[edata.data.rid];
112
116
  }
113
117
  }
114
118
  });
@@ -119,7 +123,7 @@ module.exports = (context) => ({
119
123
  const d = data.toString().split("RESPONSE::")[1];
120
124
  const jd = JSON.parse(d);
121
125
  recieve(jd);
122
- } else if(data.toString().trim().endsWith('SETUP::READY')) {
126
+ } else if (data.toString().trim().endsWith('SETUP::READY')) {
123
127
  console.log('READY');
124
128
  r(uiClasses(context, options, sendEvent, (cb) => {
125
129
  g_emitter.on('recieve', cb);
@@ -128,15 +132,15 @@ module.exports = (context) => ({
128
132
  }, (rid) => { // Remove hook
129
133
  delete hookedSocketListeners[rid];
130
134
  }));
131
- } else if(data.toString().endsWith('SETUP::HTML')) {
132
- send({action: 'JS2', data: JS, isSetup: true});
133
- } else if(data.toString() == 'INIT::READY') {
134
- send({action: 'HTML', data: HTML});
135
+ } else if (data.toString().endsWith('SETUP::HTML')) {
136
+ send({ action: 'JS2', data: JS, isSetup: true });
137
+ } else if (data.toString() == 'INIT::READY') {
138
+ send({ action: 'HTML', data: HTML });
135
139
  } else {
136
140
  console.log(data.toString());
137
141
  }
138
142
  });
139
-
143
+
140
144
  p.stderr.on("data", (data) => {
141
145
  console.error(data.toString());
142
146
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@makano/rew",
3
- "version": "1.1.21",
3
+ "version": "1.1.25",
4
4
  "description": "A simple coffescript runtime",
5
5
  "main": "lib/rew/main.js",
6
6
  "directories": {