@makano/rew 1.1.81 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. package/lib/coffeescript/browser.js +144 -139
  2. package/lib/coffeescript/cake.js +132 -133
  3. package/lib/coffeescript/coffeescript.js +437 -381
  4. package/lib/coffeescript/command.js +806 -724
  5. package/lib/coffeescript/grammar.js +1908 -2474
  6. package/lib/coffeescript/helpers.js +509 -473
  7. package/lib/coffeescript/index.js +228 -215
  8. package/lib/coffeescript/lexer.js +2282 -1909
  9. package/lib/coffeescript/nodes.js +9782 -9202
  10. package/lib/coffeescript/optparse.js +255 -227
  11. package/lib/coffeescript/parser.js +20305 -1265
  12. package/lib/coffeescript/register.js +107 -87
  13. package/lib/coffeescript/repl.js +307 -284
  14. package/lib/coffeescript/rewriter.js +1389 -1079
  15. package/lib/coffeescript/scope.js +176 -172
  16. package/lib/coffeescript/sourcemap.js +242 -227
  17. package/lib/rew/cli/cli.js +296 -239
  18. package/lib/rew/cli/log.js +27 -27
  19. package/lib/rew/cli/run.js +8 -8
  20. package/lib/rew/cli/utils.js +353 -199
  21. package/lib/rew/const/config_path.js +2 -2
  22. package/lib/rew/const/default.js +38 -53
  23. package/lib/rew/const/files.js +11 -14
  24. package/lib/rew/const/opt.js +6 -6
  25. package/lib/rew/css/theme.css +1 -1
  26. package/lib/rew/functions/core.js +55 -55
  27. package/lib/rew/functions/curl.js +23 -0
  28. package/lib/rew/functions/emitter.js +52 -55
  29. package/lib/rew/functions/exec.js +25 -25
  30. package/lib/rew/functions/export.js +17 -17
  31. package/lib/rew/functions/fs.js +57 -59
  32. package/lib/rew/functions/future.js +29 -21
  33. package/lib/rew/functions/id.js +8 -9
  34. package/lib/rew/functions/import.js +106 -122
  35. package/lib/rew/functions/map.js +10 -10
  36. package/lib/rew/functions/match.js +35 -42
  37. package/lib/rew/functions/path.js +8 -8
  38. package/lib/rew/functions/require.js +32 -36
  39. package/lib/rew/functions/sleep.js +2 -2
  40. package/lib/rew/functions/stdout.js +18 -18
  41. package/lib/rew/functions/types.js +82 -106
  42. package/lib/rew/html/ui.html +12 -12
  43. package/lib/rew/html/ui.js +196 -201
  44. package/lib/rew/main.js +14 -14
  45. package/lib/rew/misc/bin.js +37 -0
  46. package/lib/rew/misc/findAppInfo.js +13 -13
  47. package/lib/rew/misc/findAppPath.js +15 -15
  48. package/lib/rew/misc/req.js +7 -0
  49. package/lib/rew/misc/seededid.js +8 -8
  50. package/lib/rew/models/enum.js +12 -12
  51. package/lib/rew/models/struct.js +30 -32
  52. package/lib/rew/modules/compiler.js +237 -209
  53. package/lib/rew/modules/fs.js +10 -10
  54. package/lib/rew/modules/runtime.js +17 -21
  55. package/lib/rew/modules/yaml.js +27 -30
  56. package/lib/rew/pkgs/conf.js +82 -92
  57. package/lib/rew/pkgs/data.js +10 -10
  58. package/lib/rew/pkgs/date.js +27 -27
  59. package/lib/rew/pkgs/env.js +5 -5
  60. package/lib/rew/pkgs/modules/data/bintree.js +51 -51
  61. package/lib/rew/pkgs/modules/data/doublylinked.js +84 -84
  62. package/lib/rew/pkgs/modules/data/linkedList.js +72 -72
  63. package/lib/rew/pkgs/modules/data/queue.js +18 -18
  64. package/lib/rew/pkgs/modules/data/stack.js +18 -18
  65. package/lib/rew/pkgs/modules/threads/worker.js +36 -36
  66. package/lib/rew/pkgs/modules/ui/classes.js +181 -184
  67. package/lib/rew/pkgs/pkgs.js +9 -9
  68. package/lib/rew/pkgs/rune.js +373 -410
  69. package/lib/rew/pkgs/threads.js +62 -66
  70. package/lib/rew/pkgs/ui.js +148 -160
  71. package/lib/rew/qrew/compile.js +12 -0
  72. package/package.json +4 -3
@@ -1,206 +1,360 @@
1
- const path = require("path");
2
- const fs = require("fs");
3
- const conf = require("../pkgs/conf");
4
- const jsYaml = require("js-yaml");
5
- const readline = require("readline");
6
- const { log, logget } = require("./log");
7
- const { execSync, exec } = require("child_process");
8
- const { run } = require("../main");
9
- const { generateRandomID } = require("../functions/id");
1
+ const path = require('path');
2
+ const fs = require('fs');
3
+ const conf = require('../pkgs/conf');
4
+ const jsYaml = require('js-yaml');
5
+ const readline = require('readline');
6
+ const { log, logget } = require('./log');
7
+ const { execSync, exec } = require('child_process');
8
+ const { run } = require('../main');
9
+ const { generateRandomID } = require('../functions/id');
10
+ const { compile } = require('../modules/compiler');
11
+ const { to_qrew } = require('../qrew/compile');
12
+ const { findAppInfo } = require('../misc/findAppInfo');
13
+ const { req } = require('../misc/req');
10
14
 
11
- const npm_package_name = "@makano/rew";
15
+ const npm_package_name = '@makano/rew';
12
16
 
13
17
  module.exports = {
14
- conf(command, fullPath, key, value) {
15
- const con = conf({});
16
- if (command == "get") {
17
- if (!fullPath || fullPath == "list") {
18
- return fs.readdirSync(con.CONFIG_PATH).join("\n");
19
- } else {
20
- const name = fullPath.indexOf("/") ? fullPath.split("/")[0] : fullPath;
21
- const dpath = fullPath.indexOf("/")
22
- ? fullPath.split("/").slice(1).join("/")
23
- : "";
24
- const root = con.create(name);
25
- if (dpath) {
26
- const fp = path.join(root.root, dpath);
27
- if (fs.existsSync(fp) && fs.statSync(fp).isDirectory()) {
28
- return fs.readdirSync(fp).join("\n");
29
- } else {
30
- const o = con.create(name).optionCenter(dpath);
31
- return key ? o.get(key) : o.getAll(true);
32
- }
33
- } else {
34
- return fs.readdirSync(root.root).join("\n");
35
- }
36
- }
37
- } else {
38
- const name = fullPath.indexOf("/") ? fullPath.split("/")[0] : fullPath;
39
- const dpath = fullPath.indexOf("/") ? fullPath.split("/")[1] : "";
40
- if (name && key) {
41
- const root = con.create(name);
42
- const o = dpath ? root.optionCenter(dpath) : root;
43
- if (command == "set") {
44
- if (value) {
45
- o.set(key, value);
46
- } else {
47
- log("Value not specified", ":end");
48
- }
49
- } else {
50
- o.remove(key);
51
- }
52
- } else {
53
- log(!name ? "Path not specified" : "Key not specified", ":end");
54
- }
55
- }
56
- },
57
- createProject: (ppath) => {
58
- const projectPath = path.join(process.cwd(), ppath);
59
- log("Crating at", ppath);
60
- const rl = readline.createInterface({
61
- input: process.stdin,
62
- output: process.stdout,
63
- });
64
- const project = {};
65
- const create = () => {
66
- fs.mkdirSync(projectPath, { recursive: true });
67
- const confPath = path.join(projectPath, "app.yaml");
68
- const entryFile = path.join(projectPath, "main.coffee");
69
- fs.writeFileSync(
70
- confPath,
71
- jsYaml.dump({ package: project.package, entry: "main.coffee" }),
72
- );
73
- fs.writeFileSync(entryFile, `print("Hello World!")`);
74
- if (project.git) {
75
- fs.writeFileSync(
76
- path.join(projectPath, ".gitignore"),
77
- `node_modules/\npackage-lock.json`,
78
- );
79
- execSync("cd " + projectPath + " && git init .");
80
- }
81
- // log('Installing '+npm_package_name);
82
- // exec('cd '+projectPath+' && npm i '+npm_package_name, (err) => {
83
- // if(err){
84
- // console.error(err);
85
- // process.exit(0);
86
- // } else {
87
- // rl.close();
88
- // }
89
- // });
90
- log("Done.", ":end");
91
- rl.close();
92
- };
93
- if (!fs.existsSync(projectPath)) {
94
- rl.question(logget("Package Name: "), (pkg) => {
95
- if (pkg.trim()) {
96
- project.package = pkg.trim();
97
- rl.question(logget("Use git(y/N): "), (use_git) => {
98
- project.git =
99
- use_git.toLowerCase() == "y" || use_git.toLowerCase() == "yes";
100
- create();
101
- });
102
- } else {
103
- rl.close();
104
- }
105
- });
106
- } else {
107
- log(`Project ${ppath} already exists at ${projectPath}`, ":end");
108
- rl.close();
109
- }
110
- },
111
- runApp(pathOrPackage) {
112
- const apppath = path.resolve(process.cwd(), pathOrPackage);
113
- const appConfpath = path.join(apppath, "app.yaml");
18
+ conf(command, fullPath, key, value) {
19
+ const con = conf({});
20
+ if (command == 'get') {
21
+ if (!fullPath || fullPath == 'list') {
22
+ return fs.readdirSync(con.CONFIG_PATH).join('\n');
23
+ } else {
24
+ const name = fullPath.indexOf('/') ? fullPath.split('/')[0] : fullPath;
25
+ const dpath = fullPath.indexOf('/') ? fullPath.split('/').slice(1).join('/') : '';
26
+ const root = con.create(name);
27
+ if (dpath) {
28
+ const fp = path.join(root.root, dpath);
29
+ if (fs.existsSync(fp) && fs.statSync(fp).isDirectory()) {
30
+ return fs.readdirSync(fp).join('\n');
31
+ } else {
32
+ const o = con.create(name).optionCenter(dpath);
33
+ return key ? o.get(key) : o.getAll(true);
34
+ }
35
+ } else {
36
+ return fs.readdirSync(root.root).join('\n');
37
+ }
38
+ }
39
+ } else {
40
+ const name = fullPath.indexOf('/') ? fullPath.split('/')[0] : fullPath;
41
+ const dpath = fullPath.indexOf('/') ? fullPath.split('/')[1] : '';
42
+ if (name && key) {
43
+ const root = con.create(name);
44
+ const o = dpath ? root.optionCenter(dpath) : root;
45
+ if (command == 'set') {
46
+ if (value) {
47
+ o.set(key, value);
48
+ } else {
49
+ log('Value not specified', ':end');
50
+ }
51
+ } else {
52
+ o.remove(key);
53
+ }
54
+ } else {
55
+ log(!name ? 'Path not specified' : 'Key not specified', ':end');
56
+ }
57
+ }
58
+ },
59
+ createProject: (ppath) => {
60
+ const projectPath = path.join(process.cwd(), ppath);
61
+ log(''.cyan, 'Creating at'.blue, ppath.yellow);
62
+ const rl = readline.createInterface({
63
+ input: process.stdin,
64
+ output: process.stdout,
65
+ });
66
+ const project = {};
67
+ const create = () => {
68
+ fs.mkdirSync(projectPath, { recursive: true });
69
+ const confPath = path.join(projectPath, 'app.yaml');
70
+ const entryFile = path.join(projectPath, 'main.coffee');
71
+ fs.writeFileSync(confPath, jsYaml.dump({ package: project.package, entry: 'main.coffee' }));
72
+ fs.writeFileSync(entryFile, `print("Hello World!")`);
73
+ if (project.git) {
74
+ fs.writeFileSync(path.join(projectPath, '.gitignore'), `node_modules/\npackage-lock.json`);
75
+ execSync('cd ' + projectPath + ' && git init . && git branch -m main', { stdio: 'ignore' });
76
+ }
77
+ execSync('cd ' + projectPath + ' && npm init -y', { stdio: 'ignore' });
78
+ // log('Installing '+npm_package_name);
79
+ // exec('cd '+projectPath+' && npm i '+npm_package_name, (err) => {
80
+ // if(err){
81
+ // console.error(err);
82
+ // process.exit(0);
83
+ // } else {
84
+ // rl.close();
85
+ // }
86
+ // });
87
+ log('Done.'.blue.bold, ':end');
88
+ rl.close();
89
+ };
90
+ if (!fs.existsSync(projectPath)) {
91
+ rl.question(logget(' Package Name: '.blue), (pkg) => {
92
+ if (pkg.trim()) {
93
+ project.package = pkg.trim();
94
+ rl.question(logget('󰊢 Use git(y/N): '.yellow.bold), (use_git) => {
95
+ project.git = use_git.toLowerCase() == 'y' || use_git.toLowerCase() == 'yes';
96
+ create();
97
+ });
98
+ } else {
99
+ rl.close();
100
+ }
101
+ });
102
+ } else {
103
+ log(` Project ${ppath} already exists at ${projectPath}`.red.bold, ':end');
104
+ rl.close();
105
+ }
106
+ },
107
+ runApp(pathOrPackage, options) {
108
+ const apppath = path.resolve(process.cwd(), pathOrPackage);
109
+ const appConfpath = path.join(apppath, 'app.yaml');
114
110
 
115
- const runAppRoot = (root, confPath) => {
116
- const c = jsYaml.load(fs.readFileSync(confPath, { encoding: "utf-8" }));
117
- if (c.entry) {
118
- const r = path.resolve(root, c.entry);
119
- const mod_path = path.resolve(root, "snode_moduless/@makano/rew");
120
- const mod_path_lib = path.join(mod_path, "lib/rew/cli");
121
- if (fs.existsSync(mod_path) && __dirname !== mod_path_lib) {
122
- const mod_path_utilsjs = path.join(mod_path_lib, "../main.js");
123
- require(mod_path_utilsjs).run(r);
124
- } else run(r);
125
- }
126
- };
111
+ const runAppRoot = (root, confPath, byPath) => {
112
+ const c = jsYaml.load(fs.readFileSync(confPath, { encoding: 'utf-8' }));
113
+ if (c.entry) {
114
+ if(byPath && options.dev) c.entry = c.entry.endsWith('.qrew') ? c.entry.replace(/\.qrew$/, '.coffee') : c.entry;
115
+ let r = path.resolve(root, c.entry);
116
+ if(options.build) {
117
+ this.build({
118
+ file: r,
119
+ translate: options.translate || false
120
+ });
121
+ r = path.resolve(root, c.entry.replace(new RegExp(path.extname(c.entry).replace('.', '\\.') + '$'), options.translate ? '.js' : '.qrew'));
122
+ }
123
+ const mod_path = path.resolve(root, 'snode_moduless/@makano/rew');
124
+ const mod_path_lib = path.join(mod_path, 'lib/rew/cli');
125
+ if (fs.existsSync(mod_path) && __dirname !== mod_path_lib) {
126
+ const mod_path_utilsjs = path.join(mod_path_lib, '../main.js');
127
+ require(mod_path_utilsjs).run(r);
128
+ } else run(r);
129
+ }
130
+ };
127
131
 
128
- if (fs.existsSync(apppath) && fs.existsSync(appConfpath)) {
129
- runAppRoot(apppath, appConfpath);
130
- } else {
131
- const con = conf({});
132
- const apppath = path.resolve(con.CONFIG_PATH, pathOrPackage, "app");
133
- const appConfpath = path.join(apppath, "app.yaml");
134
- if (fs.existsSync(apppath) && fs.existsSync(appConfpath)) {
135
- runAppRoot(apppath, appConfpath);
136
- }
137
- }
138
- },
139
- installApp(pathname, rmidir, rmidiri) {
140
- if (!pathname) {
141
- return;
142
- }
143
- const apppath = path.resolve(process.cwd(), pathname);
144
- const appConfpath = path.join(apppath, "app.yaml");
145
- const appPackagepath = path.join(apppath, "package.json");
146
- if (fs.existsSync(apppath) && fs.existsSync(appConfpath)) {
147
- const c = jsYaml.load(
148
- fs.readFileSync(appConfpath, { encoding: "utf-8" }),
149
- );
150
- const p = JSON.parse(
151
- fs.readFileSync(appPackagepath, { encoding: "utf-8" }),
152
- );
153
- const pname = c.package;
154
- const installPath = path.join(conf({}).create(pname).root, "app");
155
- const rl = readline.createInterface({
156
- input: process.stdin,
157
- output: process.stdout,
158
- });
159
- log("Installing " + pname);
160
- log("Package: " + p.name + "@" + p.version);
161
- if (p.description) {
162
- log("Description: " + p.description);
163
- }
164
- rl.question(logget("Install " + pname + "? (y/N)"), (f) => {
165
- if (f.toLowerCase() == "y") {
166
- if (fs.existsSync(installPath)) {
167
- execSync(`rm -r ${installPath}`);
168
- }
169
- execSync(`cp -r ${apppath} ${installPath}`);
170
- if (rmidir) {
171
- execSync(`rm -r ${apppath}`);
172
- }
173
- log("Installed " + pname, ":end");
174
- rl.close();
175
- } else {
176
- if (rmidiri) {
177
- execSync(`rm -r ${apppath}`);
178
- }
179
- log("Canceled install", ":end");
180
- rl.close();
181
- }
182
- });
183
- } else {
184
- log("Path is not a rew app", ":end");
185
- }
186
- },
187
- async cloneGit(gitpath) {
188
- const p = gitpath.split("github:")[1];
189
- const url = `https://github.com/${p}`;
190
- const apiurl = `https://api.github.com/repos/${p}`;
191
- return await fetch(apiurl)
192
- .then((r) => {
193
- if (r.status !== 200) return log("Repo not found", ":end");
194
- const tempPath =
195
- "/tmp/rew-git-clone-" +
196
- p.replace(/\//g, "_") +
197
- "-" +
198
- generateRandomID();
199
- execSync(`git clone ${url} ${tempPath}`);
200
- console.log("Installing deps...");
201
- execSync(`cd ${tempPath} && npm i`);
202
- return tempPath;
203
- })
204
- .catch((r) => null);
132
+ if (fs.existsSync(apppath) && fs.existsSync(appConfpath)) {
133
+ runAppRoot(apppath, appConfpath, true);
134
+ } else {
135
+ const con = conf({});
136
+ const apppath = path.resolve(con.CONFIG_PATH, pathOrPackage, 'app');
137
+ const appConfpath = path.join(apppath, 'app.yaml');
138
+ if (fs.existsSync(apppath) && fs.existsSync(appConfpath)) {
139
+ runAppRoot(apppath, appConfpath);
140
+ }
141
+ }
142
+ },
143
+ installApp(pathname, rmidir, rmidiri) {
144
+ if (!pathname) {
145
+ return;
146
+ }
147
+ const apppath = path.resolve(process.cwd(), pathname);
148
+ const appConfpath = path.join(apppath, 'app.yaml');
149
+ const appPackagepath = path.join(apppath, 'package.json');
150
+ if (fs.existsSync(apppath) && fs.existsSync(appConfpath)) {
151
+ const c = jsYaml.load(fs.readFileSync(appConfpath, { encoding: 'utf-8' }));
152
+ const p = JSON.parse(fs.readFileSync(appPackagepath, { encoding: 'utf-8' }));
153
+ const pname = c.package;
154
+ const installPath = path.join(conf({}).create(pname).root, 'app');
155
+ const rl = readline.createInterface({
156
+ input: process.stdin,
157
+ output: process.stdout,
158
+ });
159
+ log(' Installing '.blue + pname.green.bold);
160
+ log(' Package'.blue + ': ' + p.name.green + '@' + p.version.yellow);
161
+ if (p.description) {
162
+ log(' Description'.blue + ': ' + p.description);
163
+ }
164
+ rl.question(logget('Install '.blue + pname.green.bold + '? (y/N) '), (f) => {
165
+ if (f.toLowerCase() == 'y') {
166
+ if (fs.existsSync(installPath)) {
167
+ execSync(`rm -r ${installPath}`);
168
+ }
169
+ execSync(`cp -r ${apppath} ${installPath}`);
170
+ execSync(`chmod 444 ${installPath}/app.yaml`);
171
+ if (rmidir) {
172
+ execSync(`rm -r ${apppath}`);
173
+ }
174
+ log(' Installed '.green + pname.cyan.bold, c.install ? '' : ':end');
175
+ if(c.install){
176
+ if(c.install.build){
177
+ log(' Building'.blue);
178
+ this.build({
179
+ ...c.install.build,
180
+ file: path.join(installPath, c.install.build.file)
181
+ });
182
+ }
183
+ }
184
+ rl.close();
185
+ } else {
186
+ if (rmidiri) {
187
+ execSync(`rm -r ${apppath}`);
188
+ }
189
+ log(' Canceled install'.red.bold, ':end');
190
+ rl.close();
191
+ }
192
+ });
193
+ } else {
194
+ log(' Path is not a rew app'.red.bold, ':end');
195
+ }
196
+ },
197
+ build(argv){
198
+ function readFile(filePath) {
199
+ return fs.readFileSync(filePath, { encoding: 'utf-8' });
200
+ }
201
+
202
+ function extractImports(content) {
203
+ const customImportRegex = /(\w+)\s*=\s*(imp|inc)\s*['"](.+?)['"]/g;
204
+ const jsImportRegex = /import\s+((?:\w+\s*,?\s*)?{?[^{]*}?)\s*from\s*['"](.+?)['"]/g;
205
+ const imports = [];
206
+ let match;
207
+
208
+ while ((match = customImportRegex.exec(content)) !== null) {
209
+ imports.push({ variable: match[1], url: match[3] });
210
+ }
211
+
212
+ while ((match = jsImportRegex.exec(content)) !== null) {
213
+ const variables = match[1].trim().replace(/[{}]/g, '').split(',').map(v => v.trim()).filter(v => v);
214
+ const url = match[2];
215
+ variables.forEach(variable => {
216
+ imports.push({ variable, url });
217
+ });
218
+ }
219
+
220
+ return imports;
221
+ }
222
+
223
+ function writeCompiledFile(filePath, compiledCode) {
224
+ const dirName = outputDir ? outputDir : path.dirname(filePath);
225
+ if (!fs.existsSync(dirName)) fs.mkdirSync(dirName, { recursive: true });
226
+ const baseName = path.basename(filePath, path.extname(filePath));
227
+ const newFilePath = path.join(dirName, `${baseName}.${argv.translate ? 'js' : 'qrew'}`);
228
+ fs.writeFileSync(newFilePath, compiledCode);
229
+ log(`${'Compiled'.green.bold}: ${newFilePath.yellow}`);
230
+ if(argv.remove){
231
+ fs.unlinkSync(filePath);
232
+ log(`${'Removed'.red.bold}: ${filePath.yellow}`);
233
+ }
234
+ }
235
+
236
+ function processFile(filePath, importsArray) {
237
+ const content = readFile(filePath);
238
+ const imports = extractImports(content);
239
+
240
+ imports.forEach((importStatement) => {
241
+ const importedFilePath = path.resolve(path.dirname(filePath), importStatement.url);
242
+ if (!importsArray.some((importObj) => importObj.url === importStatement.url)) {
243
+ if (fs.existsSync(importedFilePath)) {
244
+ importsArray.push(importStatement);
245
+ processFile(importedFilePath, importsArray);
246
+ } else if (fs.existsSync(importedFilePath + '.coffee')) {
247
+ importsArray.push(importStatement);
248
+ processFile(importedFilePath + '.coffee', importsArray);
249
+ } else if (fs.existsSync(importedFilePath + '.js')) {
250
+ importsArray.push(importStatement);
251
+ processFile(importedFilePath + '.js', importsArray);
252
+ }
253
+ }
254
+ });
255
+
256
+ const appPath = findAppInfo(filePath);
257
+
258
+ const compiled = argv.translate ? compile({ content }, {}) : to_qrew(content, appPath?.config?.package || path.basename(filePath).split('.').slice(0, -1).join('.'));
259
+ writeCompiledFile(filePath, compiled);
260
+ }
261
+
262
+ const filePath = path.resolve(process.cwd(), argv.file);
263
+ const importsArray = [];
264
+ const outputDir = argv.output ? path.resolve(process.cwd(), argv.output) : null;
265
+ log(' Start compile at'.yellow, (outputDir || 'default path').green);
266
+ processFile(filePath, importsArray);
267
+ log('󰈔 Compiled'.yellow, (importsArray.length + 1 + '').blue, `file${importsArray.length ? 's' : ''}.`.yellow, ':end');
268
+ },
269
+ async cloneGit(gitpath) {
270
+ const p = gitpath.split('github:')[1];
271
+ const url = `https://github.com/${p}`;
272
+ const apiurl = `https://api.github.com/repos/${p}`;
273
+ try{
274
+ const response = await req(apiurl);
275
+ if (response.status !== 200) return log(' Repo not found'.red.bold, ':end');
276
+ log(''.blue, 'Cloning from github'.yellow);
277
+ const tempPath = '/tmp/rew-git-clone-' + p.replace(/\//g, '_') + '-' + generateRandomID();
278
+ execSync(`git clone ${url} ${tempPath}`, { stdio: 'ignore' });
279
+ log(''.blue, 'Installing deps...'.yellow);
280
+ execSync(`cd ${tempPath} && npm i`);
281
+ return tempPath;
282
+ } catch(e){
283
+ log(' Repo not found'.red.bold, ':end');
284
+ }
285
+ },
286
+ findRepo(repo){
287
+ const repos = conf({}).create('').optionCenter('repos');
288
+ return repos.get(repo);
289
+ },
290
+ async installAppFrom(path){
291
+ if (path.startsWith('github:')) this.installApp(await this.cloneGit(path), true, true);
292
+ else if(path.startsWith('@')) this.fromRepo(path);
293
+ else this.installApp(path);
294
+ },
295
+ async getRepoJson(repoUrl){
296
+ try{
297
+ const text = (await req(repoUrl.startsWith('//.') ? 'http://'+repoUrl.slice(3) : repoUrl.startsWith('//') ? 'https://'+repoUrl : repoUrl)).data;
298
+ const json = text.startsWith('---') || text.startsWith('%YAML') ? jsYaml.loadAll(text)[0] : JSON.parse(text);
299
+ if(Array.isArray(json.include)){
300
+ for(let i of json.include){
301
+ json.packages = {
302
+ ...json.packages,
303
+ ...((await this.getRepoJson(i.startsWith('.') ? path.join(path.dirname(repoUrl), i) : i)).packages || {})
304
+ };
305
+ }
306
+ }
307
+ return json;
308
+ } catch(e){
309
+ log(` Fetch Error. Check your connection.`.red.bold);
310
+ return {};
311
+ }
312
+ },
313
+ async fromRepo(repoAndPkg){
314
+ const [repo, pkg] = repoAndPkg.slice(1).split('/');
315
+ const repoUrl = this.findRepo(repo);
316
+ if(!repoUrl){
317
+ log(` Repository "${repo.green}"`.red.bold, `not found.`.red.bold);
318
+ return log(`Add with:`.yellow, '\n\t$'.green, `rew repo add ${repo} URL`.cyan.bold, ':end');
319
+ } else {
320
+ const repoJson = await this.getRepoJson(repoUrl);
321
+ if(repoJson?.packages?.[pkg]){
322
+ await this.installAppFrom(repoJson.packages[pkg]);
323
+ } else {
324
+ log(` Package "${pkg}" is not in repo "${repo.green}"`.red.bold, ":end");
325
+ }
326
+ }
205
327
  },
328
+ async repo(command, key, value) {
329
+ const confInstance = conf({}).create('').optionCenter('repos') || {};
330
+
331
+ if (command === 'add' || command === 'set') {
332
+ confInstance.set(key, value.replace('https://', '//').replace('http://', '//.'));
333
+ } else if (command === 'get') {
334
+ if (key) {
335
+ console.log(confInstance.get(key) || 'Not found');
336
+ } else {
337
+ console.log(Object.keys(confInstance.getAll()).join('\n'));
338
+ }
339
+ } else if (command === 'view') {
340
+ if (key) {
341
+ const url = confInstance.get(key);
342
+ if(!url) return log(' Repo not found'.red.bold, ':end');
343
+ const json = await this.getRepoJson(url);
344
+ if(json.name) log(json.name);
345
+ log('Packages:'.yellow)
346
+ if(json.packages) Object.keys(json.packages).forEach(name => log(name)) || log(`${Object.keys(json.packages).length} Packages in ${key}`, ':end');
347
+ else log('None'.blue, ':end')
348
+ } else {
349
+ console.log(Object.keys(repos).join('\n'));
350
+ }
351
+ } else if (command === 'delete') {
352
+ confInstance.remove('repos');
353
+ } else {
354
+ log(' Invalid command'.red.bold, ':end');
355
+ }
356
+ },
357
+ initFirst(){
358
+ conf({}).create('').optionCenter('repos').set('rewpkgs', '//raw.githubusercontent.com/kevinJ045/rewpkgs/main/main.yaml');
359
+ }
206
360
  };
@@ -1,4 +1,4 @@
1
- const path = require("path");
1
+ const path = require('path');
2
2
 
3
- const CONFIG_PATH = path.resolve(process.env.HOME, ".config/rew");
3
+ const CONFIG_PATH = path.resolve(process.env.HOME, '.local/share/rew');
4
4
  module.exports.CONFIG_PATH = CONFIG_PATH;