@makano/rew 1.1.0 → 1.1.12

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/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  <h3 align="center">
2
- <img src="https://raw.githubusercontent.com/kevinJ045/rew/main/assets/logo.png" width="100" />
2
+ <img src="https://raw.githubusercontent.com/kevinJ045/rew-docs/main/assets/logo.png" width="100" />
3
3
  <br/>
4
4
  Rew
5
5
  <br/>
@@ -3,9 +3,11 @@
3
3
  const yargs = require('yargs/yargs');
4
4
  const path = require('path');
5
5
  const { hideBin } = require('yargs/helpers');
6
- const { fork } = require('child_process');
6
+ const { fork, exec } = require('child_process');
7
7
  const { watch } = require('chokidar');
8
8
  const utils = require('./utils');
9
+ const { existsSync } = require('fs');
10
+ const { log } = require('./log');
9
11
 
10
12
  yargs(hideBin(process.argv))
11
13
  .command(
@@ -25,6 +27,10 @@ yargs(hideBin(process.argv))
25
27
  },
26
28
  (argv) => {
27
29
  const filePath = path.resolve(process.cwd(), argv.file);
30
+ if(!existsSync(filePath)){
31
+ log('File not found:', argv.file, ':end');
32
+ return;
33
+ }
28
34
  const watching = [];
29
35
  const watchIt = (file) => {
30
36
  if(watching.includes(file)) return;
@@ -104,6 +110,18 @@ yargs(hideBin(process.argv))
104
110
  utils.runApp(argv.path);
105
111
  }
106
112
  )
113
+ .command('install <path>', 'Install an app', (yargs) => {
114
+ yargs
115
+ .positional('path', {
116
+ describe: 'Path of the app to install',
117
+ type: 'string',
118
+ });
119
+ },
120
+ async (argv) => {
121
+ if(argv.path.startsWith('github:')) utils.installApp(await utils.cloneGit(argv.path), true, true);
122
+ else utils.installApp(argv.path);
123
+ }
124
+ )
107
125
  .command('build <file>', 'Build the specified file', (yargs) => {
108
126
  yargs
109
127
  .positional('file', {
@@ -4,8 +4,9 @@ const conf = require('../pkgs/conf');
4
4
  const jsYaml = require('js-yaml');
5
5
  const readline = require('readline');
6
6
  const { log, logget } = require('./log');
7
- const { execSync } = require('child_process');
7
+ const { execSync, exec } = require('child_process');
8
8
  const { run } = require('../main');
9
+ const { generateRandomID } = require('../functions/id');
9
10
 
10
11
  const npm_package_name = '@makano/rew';
11
12
 
@@ -68,6 +69,7 @@ module.exports = {
68
69
  fs.writeFileSync(confPath, jsYaml.dump({ package: project.package, entry: 'main.coffee' }));
69
70
  fs.writeFileSync(entryFile, `print("Hello World!")`);
70
71
  if(project.git) {
72
+ fs.writeFileSync(path.join(projectPath, '.gitignore'), `node_modules/\npackage-lock.json`);
71
73
  execSync('cd '+projectPath+' && git init .');
72
74
  }
73
75
  log('Installing '+npm_package_name);
@@ -104,7 +106,14 @@ module.exports = {
104
106
  const runAppRoot = (root, confPath) => {
105
107
  const c = jsYaml.load(fs.readFileSync(confPath, { encoding: 'utf-8' }));
106
108
  if(c.entry){
107
- run(path.resolve(root, c.entry));
109
+ const r = path.resolve(root, c.entry);
110
+ const mod_path = path.resolve(root, 'node_modules/@makano/rew');
111
+ const mod_path_lib = path.join(mod_path, 'lib/rew/cli');
112
+ if(fs.existsSync(mod_path) && __dirname !== mod_path_lib){
113
+ const mod_path_utilsjs = path.join(mod_path_lib, '../main.js');
114
+ require(mod_path_utilsjs)
115
+ .run(r);
116
+ } else run(r);
108
117
  }
109
118
  }
110
119
 
@@ -112,11 +121,70 @@ module.exports = {
112
121
  runAppRoot(apppath, appConfpath);
113
122
  } else {
114
123
  const con = conf({});
115
- const apppath = path.resolve(con.CONFIG_PATH, pathOrPackage, '$app');
124
+ const apppath = path.resolve(con.CONFIG_PATH, pathOrPackage, 'app');
116
125
  const appConfpath = path.join(apppath, 'app.yaml');
117
126
  if(fs.existsSync(apppath) && fs.existsSync(appConfpath)){
118
127
  runAppRoot(apppath, appConfpath);
119
128
  }
120
129
  }
130
+ },
131
+ installApp(pathname, rmidir, rmidiri){
132
+ if(!pathname){
133
+ return;
134
+ }
135
+ const apppath = path.resolve(process.cwd(), pathname);
136
+ const appConfpath = path.join(apppath, 'app.yaml');
137
+ const appPackagepath = path.join(apppath, 'package.json');
138
+ if(fs.existsSync(apppath) && fs.existsSync(appConfpath)){
139
+ const c = jsYaml.load(fs.readFileSync(appConfpath, { encoding: 'utf-8' }));
140
+ const p = JSON.parse(fs.readFileSync(appPackagepath, { encoding: 'utf-8' }));
141
+ const pname = c.package;
142
+ const installPath = path.join(conf({}).create(pname).root, 'app');
143
+ const rl = readline.createInterface({
144
+ input: process.stdin,
145
+ output: process.stdout
146
+ });
147
+ log('Installing '+pname);
148
+ log("Package: "+p.name+'@'+p.version);
149
+ if(p.description){
150
+ log("Description: "+p.description);
151
+ }
152
+ rl.question(logget('Install '+pname+'? (y/N)'), (f) => {
153
+ if(f.toLowerCase() == 'y'){
154
+ if(fs.existsSync(installPath)){
155
+ execSync(`rm -r ${installPath}`);
156
+ }
157
+ execSync(`cp -r ${apppath} ${installPath}`);
158
+ if(rmidir){
159
+ execSync(`rm -r ${apppath}`);
160
+ }
161
+ log('Installed '+pname, ':end');
162
+ rl.close();
163
+ } else {
164
+ if(rmidiri){
165
+ execSync(`rm -r ${apppath}`);
166
+ }
167
+ log('Canceled install', ':end');
168
+ rl.close();
169
+ }
170
+ });
171
+ } else {
172
+ log('Path is not a rew app', ':end');
173
+ }
174
+ },
175
+ async cloneGit(gitpath){
176
+ const p = gitpath.split('github:')[1];
177
+ const url = `https://github.com/${p}`;
178
+ const apiurl = `https://api.github.com/repos/${p}`;
179
+ return await fetch(apiurl)
180
+ .then((r) => {
181
+ if(r.status !== 200) return log('Repo not found', ':end');
182
+ const tempPath = '/tmp/rew-git-clone-'+p.replace(/\//g, '_')+'-'+generateRandomID();
183
+ execSync(`git clone ${url} ${tempPath}`);
184
+ console.log('Installing deps...');
185
+ execSync(`cd ${tempPath} && npm i`);
186
+ return tempPath;
187
+ })
188
+ .catch(r => null);
121
189
  }
122
190
  }
@@ -14,9 +14,10 @@ module.exports = function future(callback, timeout = 0, defData = null) {
14
14
  });
15
15
  return {
16
16
  pipe: (callback) => promise.then(callback),
17
- pipex: (callback) => promise.finally(callback),
17
+ last: (callback) => promise.finally(callback),
18
18
  catch: (callback) => promise.catch(callback),
19
19
  resolve: (data) => listener.emit("resolve", data),
20
20
  reject: (data) => listener.emit("reject", data),
21
+ wait: async () => await promise
21
22
  };
22
23
  };
@@ -2,6 +2,23 @@ const path = require("path");
2
2
  const { getFile, file } = require("../modules/fs");
3
3
  const { importYaml } = require("../modules/yaml");
4
4
  const { findPackage, getPackage } = require("../pkgs/pkgs");
5
+ const { existsSync, readFileSync } = require("fs");
6
+ const conf = require("../pkgs/conf");
7
+ const jsYaml = require("js-yaml");
8
+
9
+ const lookUpInOtherApps = (fullPath) => {
10
+ const con = conf({});
11
+ const name = fullPath.indexOf('/') ? fullPath.split('/')[0] : fullPath;
12
+ let dpath = fullPath.indexOf('/') ? fullPath.split('/')[1] : '';
13
+ let ppath = path.join(con.CONFIG_PATH, name, 'app');
14
+ if(!existsSync(ppath)) return null;
15
+ if(!dpath){
16
+ dpath = jsYaml.load(readFileSync(path.join(ppath, 'app.yaml'), 'utf-8')).entry;
17
+ }
18
+ ppath = path.join(ppath, dpath);
19
+ if(existsSync(ppath)) return ppath;
20
+ else return null;
21
+ }
5
22
 
6
23
  module.exports.imp = function (runPath, context) {
7
24
  return function (filename, options = {}) {
@@ -9,10 +26,16 @@ module.exports.imp = function (runPath, context) {
9
26
  let exports,
10
27
  ispkg = findPackage(filename);
11
28
 
12
- const filepath = path.resolve(path.dirname(context.module.filepath), filename);
29
+ let filepath = path.resolve(path.dirname(context.module.filepath), filename);
13
30
 
14
31
  // console.log(typeof runPath);
15
32
 
33
+ if(!ispkg && !existsSync(filepath)){
34
+ const otherPath = lookUpInOtherApps(filename);
35
+ if(!otherPath) throw new Error('Module "'+filename+'" not found');
36
+ else filepath = otherPath;
37
+ }
38
+
16
39
  if (ispkg) {
17
40
  exports = getPackage(filename)(context);
18
41
  } else if (type == "coffee") {
@@ -0,0 +1,37 @@
1
+ const { parentPort, workerData } = require('worker_threads');
2
+ const { exec } = require('../../../modules/runtime');
3
+
4
+ const target = {};
5
+ target.events = [];
6
+ target.on = (e, cb) => {
7
+ target.events.push({ e, cb });
8
+ }
9
+ target.off = (e) => {
10
+ target.events = target.events.filter(i => i.e !== e);
11
+ }
12
+ target.emit = (e, data) => {
13
+ target.events.filter(i => i.e == e).forEach(i => i.cb(data));
14
+ }
15
+
16
+ parentPort.on('message', (data) => {
17
+ if (data.type === 'event') {
18
+ target.emit(data.event, data.data);
19
+ } else if (data.type === 'start') {
20
+ (async () => {
21
+ try {
22
+ exec(`(${workerData.cb}).call({ process }, context)`, { print: (...a) => console.log(...a), process: {
23
+ on: (e, cb) => { target.on(e, cb) },
24
+ off: (e) => { target.off(e) },
25
+ emit: (e) => { parentPort.postMessage({ type: 'event', event: e, data }) },
26
+ exit: (code) => process.exit(code),
27
+ finish: (data) => {
28
+ parentPort.postMessage({ type: 'result', result: data });
29
+ process.exit(0)
30
+ }
31
+ }, context: workerData.context })
32
+ } catch (e) {
33
+ parentPort.postMessage({ type: 'error', error: e.message });
34
+ }
35
+ })();
36
+ }
37
+ });
@@ -0,0 +1,67 @@
1
+ const { Worker } = require('worker_threads');
2
+ const emitter = require('../functions/emitter');
3
+ const future = require('../functions/future');
4
+ const { struct } = require('../models/struct');
5
+ const path = require('path');
6
+
7
+ module.exports = (context) => ({
8
+ thread: (cb) => {
9
+ const workers = [];
10
+
11
+ const stopWorker = (worker) => {
12
+ workers.splice(workers.indexOf(worker), 1);
13
+ worker.terminate()
14
+ };
15
+
16
+ const athread = {
17
+ stopAll: () => {
18
+ if (!run) return;
19
+ workers.forEach(stopWorker);
20
+ },
21
+ start: (context) => {
22
+ let dataResult = future(() => {}), listener = emitter();
23
+ const worker = new Worker(path.resolve(__dirname, './modules/threads/worker.js'), {
24
+ workerData: { context, cb: cb.toString() }
25
+ });
26
+ workers.push(worker);
27
+
28
+ const stop = () => stopWorker(worker);
29
+
30
+ worker.on('message', (data) => {
31
+ if (data.type === 'result') {
32
+ dataResult.resolve(data.result);
33
+ stop();
34
+ } else if (data.type === 'error') {
35
+ reject(new Error(data.error));
36
+ stop();
37
+ } else if (data.type === 'event') {
38
+ listener.emit(data.event, data.data);
39
+ }
40
+ });
41
+
42
+ worker.on('error', (error) => {
43
+ console.log(error);
44
+ stop();
45
+ });
46
+
47
+ worker.on('exit', (code) => {
48
+ stop();
49
+ if (code !== 0) {
50
+ throw new Error(`Worker stopped with exit code ${code}`);
51
+ }
52
+ });
53
+
54
+ worker.postMessage({ type: 'start' });
55
+
56
+ return {
57
+ on: (e, cb) => listener.on(e, cb),
58
+ off: (e, cb) => listener.off(e, cb),
59
+ emit: (e, data) => worker?.postMessage({ type: 'event', event: e, data }),
60
+ get: () => dataResult.wait()
61
+ };
62
+ }
63
+ };
64
+
65
+ return athread;
66
+ }
67
+ });
package/package.json CHANGED
@@ -1,16 +1,13 @@
1
1
  {
2
2
  "name": "@makano/rew",
3
- "version": "1.1.0",
3
+ "version": "1.1.12",
4
4
  "description": "A simple coffescript runtime",
5
5
  "main": "lib/rew/main.js",
6
6
  "directories": {
7
7
  "lib": "lib"
8
8
  },
9
9
  "scripts": {
10
- "test": "node test/test.js",
11
- "docs:dev": "vitepress dev docs",
12
- "docs:build": "vitepress build docs",
13
- "docs:preview": "vitepress preview docs"
10
+ "test": "node test/test.js"
14
11
  },
15
12
  "files": [
16
13
  "lib/",
@@ -32,7 +29,6 @@
32
29
  "chokidar": "^3.6.0",
33
30
  "js-yaml": "^4.1.0",
34
31
  "vm": "^0.1.0",
35
- "ws": "^8.17.0",
36
32
  "yargs": "^17.7.2"
37
33
  },
38
34
  "devDependencies": {