@makano/rew 1.1.81 → 1.2.1

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