-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextension.js
274 lines (225 loc) · 7.04 KB
/
extension.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
const vscode = require('vscode');
const Uri = vscode.Uri;
const vsfs = vscode.workspace.fs;
const os = require('os');
const liveServer = require('live-server');
let port = 5555;
const log = console.log;
let panel;
function getLocalNetworkIPAddress() {
const interfaces = os.networkInterfaces();
// Prioritize local network Ethernet and Wi-Fi interfaces
const preferredInterfaces = ['en0', 'en1', 'en2', 'eth0', 'eth1', 'eth2'];
for (const connection of preferredInterfaces) {
if (interfaces[connection]) {
for (const iface of interfaces[connection]) {
if (iface.family === 'IPv4' && !iface.internal) {
return iface.address;
}
}
}
}
// Fallback to any other active interface
for (const connection in interfaces) {
for (const iface of interfaces[connection]) {
if (iface.family === 'IPv4' && !iface.internal) {
return iface.address;
}
}
}
return '0.0.0.0';
}
async function newProject() {
try {
// Prompt the user for a new folder name
const folderName = await vscode.window.showInputBox({
prompt: 'Enter a name for the new p5play project folder',
validateInput: (text) => (text.trim() === '' ? 'Folder name cannot be empty' : null)
});
if (!folderName) return;
// Prompt the user to select a folder
let filePath = await vscode.window.showOpenDialog({
canSelectFiles: false,
canSelectFolders: true,
canSelectMany: false,
message: 'Select a destination folder for the new project'
});
if (!filePath) return;
const dest = Uri.joinPath(Uri.file(filePath[0].path), folderName);
await vscode.workspace.fs.createDirectory(dest);
const src = Uri.joinPath(Uri.file(__dirname), 'p5play-template');
const success = await copyDirectory(src, dest);
if (!success) {
vscode.window.showErrorMessage('Error copying directory.');
}
// Open the new project folder in a new window
await vscode.commands.executeCommand('vscode.openFolder', dest, true);
// Hacky way to actually open the sketch file...
if (process.platform !== 'win32') {
let sketchFile = Uri.joinPath(dest, 'sketch.js').path;
sketchFile = Uri.parse('vscode://file' + sketchFile);
await vscode.env.openExternal(sketchFile);
}
} catch (e) {
console.error(e);
vscode.window.showErrorMessage(e.message);
}
}
async function copyDirectory(srcDir, destDir) {
try {
const entries = await vsfs.readDirectory(srcDir);
for (const [entryName, entryType] of entries) {
const src = Uri.joinPath(srcDir, entryName);
const dest = Uri.joinPath(destDir, entryName);
if (entryType === vscode.FileType.File) {
// Copy a file
const sourceFileData = await vsfs.readFile(src);
await vsfs.writeFile(dest, sourceFileData);
} else if (entryType === vscode.FileType.Directory) {
// Recursively copy a directory
await copyDirectory(src, dest);
}
}
return true;
} catch (error) {
return false;
}
}
let serverStarted = false;
async function startLiveServer() {
if (!vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length === 0) {
return;
}
const workspaceFolder = vscode.workspace.workspaceFolders[0].uri.fsPath;
const maxAttempts = 10; // Maximum number of ports to try
let attempts = 0;
function tryStartServer(port) {
return new Promise((resolve, reject) => {
const params = {
port,
root: workspaceFolder,
open: false, // don't open in the browser
ignore: 'node_modules',
file: 'index.html',
wait: 0 // wait time before reloading
};
liveServer
.start(params)
.on('listening', () => {
resolve(port);
})
.on('error', (err) => {
reject(err);
});
});
}
while (attempts < maxAttempts) {
try {
await tryStartServer(port);
return;
} catch (err) {
attempts++;
port++;
}
}
vscode.window.showErrorMessage('Failed to start live server on any port.');
}
async function openTab() {
if (!serverStarted) await startLiveServer();
panel = vscode.window.createWebviewPanel('p5play', 'p5play', vscode.ViewColumn.Two, {
enableScripts: true,
localResourceRoots: [vscode.Uri.file(__dirname)]
});
const htmlPath = Uri.file(__dirname + '/editor/index.html');
let html = await vsfs.readFile(htmlPath);
html = Buffer.from(html).toString('utf8');
// get sandboxed file path
function getSource(file) {
const path = Uri.file(__dirname + '/editor/' + file);
return panel.webview.asWebviewUri(path);
}
function importHTML(file, fileToReplace) {
html = html.replaceAll(fileToReplace || file, getSource(file));
}
html = html.replace('<link rel="stylesheet" href="icons.css">', '');
importHTML('editor.css');
importHTML('editor.js');
importHTML('../node_modules/@bitjson/qr-code/dist/qr-code.js');
importHTML('../assets/p5play_icon.png');
importHTML('../assets/p5play_logo.svg');
const cssPath = Uri.file(__dirname + '/editor/icons.css');
let style = await vsfs.readFile(cssPath);
style = Buffer.from(style).toString('utf8');
function importStyle(file, fileToReplace) {
style = style.replace(fileToReplace || file, getSource(file));
}
let svgFiles = [
// 'android',
// 'app-store-ios',
// 'apple',
// 'book-open',
'bug-report',
'create-new-folder',
'display',
// 'folder-open',
// 'google-play',
// 'hammer',
// 'language',
'mobile-screen-button',
'play',
// 'share-from-square',
// 'stop'
'refresh'
];
for (const file of svgFiles) {
importStyle('icons/' + file + '.svg');
}
let globals = `
<script>
window.ipAddress = '${getLocalNetworkIPAddress()}';
</script>`;
const startOfHead = html.indexOf('<head>') + 6;
html = html.slice(0, startOfHead) + '<style>' + style + '</style>' + globals + html.slice(startOfHead);
panel.webview.html = html;
// Listen for messages from the webview
panel.webview.onDidReceiveMessage(async (message) => {
switch (message.command) {
case 'newProject':
await newProject();
break;
case 'openInBrowser':
// open the live server link in the default browser
const url = 'http://127.0.0.1:' + port;
await vscode.env.openExternal(vscode.Uri.parse(url));
break;
case 'openDevTools':
vscode.commands.executeCommand('workbench.action.toggleDevTools');
break;
}
});
if (!vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length == 0) {
panel.webview.postMessage({ command: 'workspaceIsEmpty' });
}
}
function activate(context) {
let cmd = vscode.commands.registerCommand('p5play-vscode.newProject', newProject);
context.subscriptions.push(cmd);
cmd = vscode.commands.registerCommand('p5play-vscode.openEditor', openTab);
context.subscriptions.push(cmd);
// for testing, remove this line to disable auto-open
// vscode.commands.executeCommand('p5play-vscode.openEditor');
const statusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 0);
statusBar.text = '$(game) p5play';
statusBar.tooltip = 'Click to open the p5play sidebar.';
statusBar.command = 'p5play-vscode.openEditor';
statusBar.show();
context.subscriptions.push(statusBar);
}
function deactivate() {
if (panel) panel.dispose();
if (serverStarted) liveServer.shutdown();
}
module.exports = {
activate,
deactivate
};