forked from HeyPuter/puter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_puter.js
79 lines (71 loc) · 2.63 KB
/
main_puter.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
/*
* Copyright (C) 2024 Puter Technologies Inc.
*
* This file is part of Phoenix Shell.
*
* Phoenix Shell is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { Context } from 'contextlink';
import { launchPuterShell } from './puter-shell/main.js';
import { CreateFilesystemProvider } from './platform/puter/filesystem.js';
import { CreateDriversProvider } from './platform/puter/drivers.js';
import { XDocumentPTT } from './pty/XDocumentPTT.js';
import { CreateEnvProvider } from './platform/puter/env.js';
window.main_shell = async () => {
const config = {};
let resolveConfigured = null;
const configured_ = new Promise(rslv => {
resolveConfigured = rslv;
});
const terminal = puter.ui.parentApp();
if (!terminal) {
console.error('Phoenix cannot run without a parent Terminal. Exiting...');
puter.exit();
return;
}
terminal.on('message', message => {
if (message.$ === 'config') {
const configValues = { ...message };
delete configValues.$;
for ( const k in configValues ) {
config[k] = configValues[k];
}
resolveConfigured();
}
});
terminal.on('close', () => {
console.log('Terminal closed; exiting Phoenix...');
puter.exit();
});
// FIXME: on terminal close, close ourselves
terminal.postMessage({ $: 'ready' });
await configured_;
const puterSDK = globalThis.puter;
if ( config['puter.auth.token'] ) {
await puterSDK.setAuthToken(config['puter.auth.token']);
}
await puterSDK.setAPIOrigin(config['puter.api_origin']);
const ptt = new XDocumentPTT(terminal);
await launchPuterShell(new Context({
ptt,
config, puterSDK,
externs: new Context({ puterSDK }),
platform: new Context({
name: 'puter',
filesystem: CreateFilesystemProvider({ puterSDK }),
drivers: CreateDriversProvider({ puterSDK }),
env: CreateEnvProvider({ config }),
}),
}));
};