-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
143 lines (129 loc) · 3.19 KB
/
main.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
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
import './style.css';
import { WebContainer } from '@webcontainer/api';
import { files } from './files';
import { Terminal } from 'xterm';
import 'xterm/css/xterm.css';
/** @type {import('xterm').Terminal} */
let term;
/** @type {import('@webcontainer/api').WebContainer} */
let webcontainerInstance;
window.addEventListener('load', async () => {
// Call only once
webcontainerInstance = await WebContainer.boot();
await webcontainerInstance.mount(files);
await installCoreDependencies();
});
async function installCoreDependencies() {
// Install dependencies
const iProcess = await webcontainerInstance.spawn('npm', ['install']);
iProcess.output.pipeTo(
new WritableStream({
write(data) {
console.log(data);
},
})
);
// Wait for install command to exit
return iProcess.exit;
}
document.getElementById('github-input').addEventListener('change', async () => {
await runApp();
});
async function runApp() {
setUpTerminal();
await gitClone();
await installProjectDependencies();
await buildProject();
await listFiles();
await startDevServer();
}
function setUpTerminal() {
term = new Terminal({ convertEol: true });
term.open(document.getElementById('terminal'));
}
async function gitClone() {
// Git clone
const iProcess = await webcontainerInstance.spawn('node', [
'index.js',
'https://github.com/suryamodulus/moviebuzz',
'main',
'/home/gh-repo',
]);
iProcess.output.pipeTo(
new WritableStream({
write(data) {
console.log(data);
term.write(data);
},
})
);
// Wait for install command to exit
return iProcess.exit;
}
async function installProjectDependencies() {
// Install dependencies
const iProcess = await webcontainerInstance.spawn('npm', [
'--prefix',
'/home/gh-repo/moviebuzz-main',
'install',
]);
iProcess.output.pipeTo(
new WritableStream({
write(data) {
console.log(data);
term.write(data);
},
})
);
// Wait for install command to exit
return iProcess.exit;
}
async function buildProject() {
// Install dependencies
const iProcess = await webcontainerInstance.spawn('npm', [
'--prefix',
'/home/gh-repo/moviebuzz-main',
'run',
'build',
]);
iProcess.output.pipeTo(
new WritableStream({
write(data) {
console.log(data);
term.write(data);
},
})
);
// Wait for install command to exit
return iProcess.exit;
}
async function listFiles() {
//List files
const iProcess = await webcontainerInstance.spawn('ls', [
'-la',
'/home/gh-repo/moviebuzz-main/build',
]);
iProcess.output.pipeTo(
new WritableStream({
write(data) {
console.log(data);
term.write(data);
},
})
);
// Wait for install command to exit
return iProcess.exit;
}
async function startDevServer() {
// Run `npm run start` to start the Express app
await webcontainerInstance.spawn('npx', [
'serve',
'/home/gh-repo/moviebuzz-main/build',
]);
// Wait for `server-ready` event
webcontainerInstance.on('server-ready', (port, url) => {
iframeEl.src = url;
});
}
/** @type {HTMLIFrameElement | null} */
const iframeEl = document.querySelector('iframe');