-
Notifications
You must be signed in to change notification settings - Fork 719
/
Copy pathprotocol.ts
165 lines (143 loc) · 4.64 KB
/
protocol.ts
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
import * as fs from 'node:fs';
import * as path from 'node:path';
import { app } from 'electron';
import { openFiddle } from './files';
import { ipcMainManager } from './ipc';
import { isDevMode } from './utils/devmode';
import { isValidElectronPath } from './utils/local-version';
import { getOrCreateMainWindow } from './windows';
import { IpcEvents } from '../ipc-events';
const PROTOCOL = 'electron-fiddle';
const squirrelPath = path.resolve(
path.dirname(process.execPath),
'..',
'electron-fiddle.exe',
);
const handlePotentialProtocolLaunch = (url: string) => {
if (!app.isReady()) {
app.once('ready', () => handlePotentialProtocolLaunch(url));
return;
}
const parsed = new URL(url.replace(/\/$/, ''));
if (!parsed.pathname || !parsed.hostname) return;
const pathParts = parsed.pathname.split('/').slice(1);
switch (parsed.hostname) {
// electron-fiddle://gist/blub
case 'gist':
if (pathParts.length === 1) {
// We only have a gist ID
ipcMainManager.send(IpcEvents.LOAD_GIST_REQUEST, [
{
id: pathParts[0],
},
]);
} else if (pathParts.length === 2) {
// We have a gist owner and gist ID, we can ignore the owner
ipcMainManager.send(IpcEvents.LOAD_GIST_REQUEST, [
{
id: pathParts[1],
},
]);
} else {
// This is a super invalid gist launch
return;
}
break;
// electron-fiddle://electron/{tag}/{path}
case 'electron':
if (pathParts.length > 1) {
// First part is the tag name (e.g. v22.0.0)
// Rest is the path to the example
ipcMainManager.send(IpcEvents.LOAD_ELECTRON_EXAMPLE_REQUEST, [
{
tag: pathParts[0],
path: pathParts.slice(1).join('/'),
},
]);
} else {
// This is an invalid electron launch
return;
}
break;
// electron-fiddle://register-local-version/?name={name}&path={path}&version={version}
case 'register-local-version': {
if (pathParts.length === 1) {
const name = parsed.searchParams.get('name');
const localPath = parsed.searchParams.get('path');
const version = parsed.searchParams.get('version');
if (!(name && localPath && version)) {
console.debug('register-local-version: Missing params');
return;
}
if (!isValidElectronPath(localPath)) {
console.debug(`register-local-version: Invalid path ${localPath}`);
return;
}
const toAdd = {
name,
path: localPath,
version,
};
console.debug('register-local-version: Registering version', toAdd);
ipcMainManager.send(IpcEvents.REGISTER_LOCAL_VERSION_FOLDER, [toAdd]);
} else {
// Invalid
return;
}
break;
}
default:
return;
}
getOrCreateMainWindow().then((window) => window.focus());
};
const isProtocolString = (arg: string) => arg.startsWith(`${PROTOCOL}://`);
export const findProtocolArg = (argv: string[]) => {
return argv.find((arg) => isProtocolString(arg));
};
const scanArgv = (argv: Array<string>) => {
const protocolArg = findProtocolArg(argv);
if (protocolArg) {
console.info('Found protocol arg in argv:', protocolArg);
handlePotentialProtocolLaunch(protocolArg);
}
};
const scanNpmArgv = (argv: string) => {
const parsedArgv = JSON.parse(argv);
const { original: args } = parsedArgv;
scanArgv(args);
};
export const listenForProtocolHandler = () => {
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) app.quit();
app.removeAllListeners('open-url');
app.on('open-url', (_, url) => {
if (isProtocolString(url)) {
handlePotentialProtocolLaunch(url);
}
});
app.removeAllListeners('second-instance');
app.on('second-instance', (_event, commandLine, _workingDirectory) => {
// Someone tried to run a second instance
scanArgv(commandLine);
});
app.on('open-file', async (_, path) => {
if (!path || path.length < 1) {
return;
}
const files = await openFiddle(path);
ipcMainManager.send(IpcEvents.FS_OPEN_FIDDLE, [path, files]);
});
// pass protocol URL via npm start args in dev mode
if (isDevMode() && process.env.npm_config_argv) {
scanNpmArgv(process.env.npm_config_argv);
} else if (process.platform === 'win32') {
scanArgv(process.argv);
}
};
export const setupProtocolHandler = () => {
if (process.platform === 'win32' && !fs.existsSync(squirrelPath)) return;
if (!app.isDefaultProtocolClient(PROTOCOL, squirrelPath)) {
app.setAsDefaultProtocolClient(PROTOCOL, squirrelPath);
}
};