-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctionLoader.js
More file actions
204 lines (176 loc) · 5.92 KB
/
Copy pathfunctionLoader.js
File metadata and controls
204 lines (176 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import {Logger} from "./utils/Logger.js";
import {Dialog} from "./utils/dialog.js";
import {AppManager} from "./appManager.js";
import {FileSystem} from "./filesystem/FileSystem.js";
import {Terminal} from "./terminal.js";
const dialog = Dialog.globalInstance;
const terminal = new Terminal();
const os = {
functions: new Map(),
promiseResolve: null,
promiseReject: null,
/**
* @deprecated
* os.dialog.say should be used instead
*/
say: dialog.sayRaw,
/**
* @deprecated
* os.dialog.ask should be used instead
*/
ask: dialog.ask,
/**
* @deprecated
* os.dialog.next can be used instead, or just return
*/
next: dialog.next,
logger: Logger,
dialog: dialog,
fs: FileSystem.instance,
terminal: terminal,
wd: terminal.wd,
}
/**
* Loads the functions into the os
* @returns {Promise<unknown>}
*/
os.load = async () => {
const {appList} = await import('./functions/functionList.js');
await Promise.all(appList.apps.map(async (app) => {
const path = `./${appList.path}/${app}`;
await AppManager.instance.addApp(path);
}));
postMessageAPI();
}
/**
* Runs the command
* @param data {string} The command to run
*/
const runO = (data) => {
return new Promise(async (resolve, reject) => {
let args = data.trim().split(' ');
const command = args.shift().toLowerCase();
let options = {
pipe: false,
};
// check if the command exists
if (!AppManager.instance.apps.has(command)) {
os.dialog.next('function doesnt exist');
Logger.info(` ${command}: function doesnt exist`);
let keys = Array.from(AppManager.instance.apps.keys());
const fuse = new Fuse(keys, {
threshold: 0.8,
});
const result = fuse.search(command);
if (result.length > 0) {
os.dialog.next(`did you mean ${result[0].item}?`);
}
resolve();
return;
}
// check if pipe is used
if (args.includes('|')) {
options.pipe = true;
args = args.filter(e => e !== '|');
}
if (args.includes('>>')) {
options.pipe = true;
options.mode = ">>";
options.path = args[args.indexOf('>>') + 1];
// remove all args after the >>
args = args.slice(0, args.indexOf('>>'));
}
if (args.includes('>')) {
options.pipe = true;
options.mode = ">";
options.path = args[args.indexOf('>') + 1];
// remove all args after the >
args = args.slice(0, args.indexOf('>'));
}
// check if the command has the correct amount of arguments
if (AppManager.instance.apps.get(command).arguments !== -1) {
if (args.length > AppManager.instance.apps.get(command).arguments) {
os.next('too many arguments');
resolve();
return;
}
if (args.length < AppManager.instance.apps.get(command).arguments) {
os.next('not enough arguments');
resolve();
return;
}
}
try {
// run the command
// await AppManager.instance.apps.get(command).execute(this, args);
const output = await AppManager.instance.run(command, os, args, options);
if (options.mode === ">" || options.mode === ">>") {
const file = os.wd.getOrCreateFile(options.path);
if (options.mode === ">>") {
file.appendData(output.join('\n'));
resolve();
}
if (options.mode === ">") {
file.setData(output.join('\n'));
resolve();
}
}
resolve(output);
} catch (error) {
dialog.next(error);
console.log(error);
}
resolve();
});
}
/**
* Split the command by | and run each command
* @param data {string} The commands to run
*/
os.run = (data) => {
return new Promise(async (resolve, reject) => {
const tmp = data.replaceAll('|', '|;:');
const commands = tmp.split(';:');
let lastOutput = [];
for (let command of commands) {
if (command === '') continue;
let string = "";
const parts = command.split(' ')
// check for alias
if (Terminal.instance.alias.has(parts[0])) {
//replace alias with the command
parts[0] = Terminal.instance.alias.get(parts[0]);
command = parts.join(' ');
console.log("alias", command);
}
// find > and >>, remove them from the command and put them at the end of the generated command
if (command.includes('>>')) {
string = ">> " + command.split('>>')[1].trim();
command = command.split('>>')[0].trim();
}
if (command.includes('>')) {
string = "> " + command.split('>')[1].trim();
command = command.split('>')[0].trim();
}
const cmd = `${command} ${lastOutput.join(' ')} ${string}`
lastOutput = await runO(cmd) ?? [];
}
resolve();
});
}
function postMessageAPI() {
// Called sometime after postMessage is called
window.addEventListener("message", async (event) => {
if (event.data === undefined) return;
if (event.data.type === "cmd") {
console.log(event.data);
const args = event.data.data.trim().split(' ');
const command = args.shift();
const output = await AppManager.instance.run(command, os, args, {pipe: true});
event.source.postMessage(output);
return
}
});
}
await os.load();
export default os;