-
Notifications
You must be signed in to change notification settings - Fork 757
Expand file tree
/
Copy pathprotocol.ts
More file actions
132 lines (115 loc) · 3.6 KB
/
Copy pathprotocol.ts
File metadata and controls
132 lines (115 loc) · 3.6 KB
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
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 { 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;
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 {
scanArgv(process.argv);
}
};
export const setupProtocolHandler = () => {
if (process.platform === 'win32' && !fs.existsSync(squirrelPath)) return;
if (!app.isDefaultProtocolClient(PROTOCOL, squirrelPath)) {
app.setAsDefaultProtocolClient(PROTOCOL, squirrelPath);
}
};