-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopen.js
85 lines (78 loc) · 2.68 KB
/
open.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
const { commands, window, workspace } = require('vscode');
const open = require('open');
const process = require('process');
const fixOpenBrowserLine = (url, browser = '') => {
if (browser === 'chrome') {
switch (process.platform) {
case 'darwin': return 'google chrome';
case 'linux': return 'google-chrome';
case 'win32': return 'chrome';
default: return 'chrome';
}
}
if (browser.startsWith('microsoft-edge')) return `microsoft-edge:${url}`;
return browser;
};
const tryBuiltinBrowser = async url => {
const useBuiltinBrowser = workspace.getConfiguration('hqServer.browser').get('useBuiltinBrowser');
if (useBuiltinBrowser) {
try {
await commands.executeCommand('browser-preview.openPreview', url);
} catch (err) {
console.log(`HQ Live Server: ${err}`);
await window.showErrorMessage(`
Server is started at ${url} but failed to open in Browser Preview.
Make sure Browser Preview extension is installed?
`);
}
return true;
}
return false;
};
const tryAdvancedBrowser = async url => {
const browserCmdLine = workspace.getConfiguration('hqServer.browser').get('browserCmdLine');
if (browserCmdLine) {
const [ browser, ...params ] = browserCmdLine.split('--');
const app = browser ?
[ fixOpenBrowserLine(url, browser.trim()), ...params.map(p => `--${p}`) ] :
[];
await open(url, { app });
return true;
}
return false;
};
const trySpecificBrowser = async url => {
const preferredBrowser = workspace.getConfiguration('hqServer.browser').get('preferredBrowser');
const attachCromeDebugger = workspace.getConfiguration('hqServer').get('attachChromeDebugger');
const app = [];
if (preferredBrowser) {
const [ browser, ...details ] = preferredBrowser.split(':');
app.push(fixOpenBrowserLine(url, browser));
if (details[0] === 'PrivateMode') {
if (browser === 'chrome' || browser === 'blisk') app.push('--incognito');
else if (browser === 'firefox') app.push('--private-window');
}
if ((browser === 'chrome' || browser === 'blisk') && attachCromeDebugger) {
app.push(...[
'--new-window',
'--no-default-browser-check',
'--remote-debugging-port=9222',
`--user-data-dir=${__dirname}`,
]);
}
}
try {
await open(url, { app });
} catch (err) {
console.log(`HQ Live Server: ${err}`);
return window.showErrorMessage(`
Server is started at ${url} but failed to open in Browser Preview.
Make sure Browser Preview extension is installed?
`);
}
return true;
};
exports.openBrowser = async url =>
await tryBuiltinBrowser(url) ||
await tryAdvancedBrowser(url) ||
trySpecificBrowser(url);