-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
worker.js
109 lines (96 loc) · 3.38 KB
/
worker.js
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
let stdout = [];
let stderr = [];
globalThis.wasmGitModuleOverrides = {
'print': (text) => {
console.log(text);
stdout.push(text)
},
'printErr': (text) => {
console.error(text);
stderr.push(text);
}
};
const lg2mod = await import(new URL('lg2.js', import.meta.url));
const lg = await lg2mod.default();
const FS = lg.FS;
const IDBFS = lg.IDBFS;
const username = 'Test User';
const useremail = '[email protected]';
FS.writeFile('/home/web_user/.gitconfig',
`[user]
name = ${username}
email = ${useremail}`);
let currentRepoRootDir;
onmessage = (msg) => {
stderr = [];
stdout = [];
if (msg.data.command === 'writecommitandpush') {
FS.writeFile(msg.data.filename, msg.data.contents);
lg.callMain(['add', '--verbose', msg.data.filename]);
lg.callMain(['commit', '-m', `edited ${msg.data.filename}`]);
lg.callMain(['log']);
FS.syncfs(false, () => {
console.log(currentRepoRootDir, 'stored to indexeddb');
lg.callMain(['push']);
postMessage({ dircontents: FS.readdir('.') });
});
} else if (msg.data.command === 'writefile') {
FS.writeFile(msg.data.filename, msg.data.contents);
FS.syncfs(false, () => {
console.log(currentRepoRootDir, 'stored to indexeddb');
postMessage({ dircontents: FS.readdir('.') });
});
} else if (msg.data.command === 'synclocal') {
currentRepoRootDir = msg.data.url.substring(msg.data.url.lastIndexOf('/') + 1);
console.log('synclocal', currentRepoRootDir);
FS.mkdir(`/${currentRepoRootDir}`);
FS.mount(IDBFS, {}, `/${currentRepoRootDir}`);
FS.syncfs(true, () => {
if (FS.readdir(`/${currentRepoRootDir}`).find(file => file === '.git')) {
FS.chdir(`/${currentRepoRootDir}`);
postMessage({ dircontents: FS.readdir('.') });
console.log(currentRepoRootDir, 'restored from indexeddb');
} else if (msg.data.newrepo) {
FS.chdir(`/${currentRepoRootDir}`);
postMessage({ empty: true });
} else {
FS.chdir('/');
postMessage({ notfound: true });
}
});
} else if (msg.data.command === 'deletelocal') {
FS.unmount(`/${currentRepoRootDir}`);
self.indexedDB.deleteDatabase('/' + currentRepoRootDir);
postMessage({ deleted: currentRepoRootDir });
} else if (msg.data.command === 'dir') {
postMessage({ dircontents: FS.readdir('.') });
} else if (msg.data.command === 'clone') {
currentRepoRootDir = msg.data.url.substring(msg.data.url.lastIndexOf('/') + 1);
lg.callMain(['clone', msg.data.url, currentRepoRootDir]);
FS.chdir(currentRepoRootDir);
FS.syncfs(false, () => {
console.log(currentRepoRootDir, 'stored to indexeddb');
postMessage({ dircontents: FS.readdir('.') });
});
} else if (msg.data.command === 'pull') {
lg.callMain(['fetch', 'origin']);
lg.callMain(['merge', 'origin/master']);
FS.syncfs(false, () => {
console.log(currentRepoRootDir, 'stored to indexeddb');
});
} else if (msg.data.command === 'readfile') {
try {
postMessage({
filename: msg.data.filename,
filecontents: FS.readFile(msg.data.filename, { encoding: 'utf8' })
});
} catch (e) {
postMessage({ 'stderr': JSON.stringify(e) });
}
} else {
const args = msg.data.args || [];
lg.callMain([msg.data.command, ...args]);
postMessage({ stdout: stdout.join('\n'), stderr: stderr.join('\n'), });
}
};
postMessage({ 'ready': true });