'f'
mh-two-thousand-and-two
2024-04-12 26f2711ef9461961fb953e2b497bd314ef95e345
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
"use strict";
const child_process_1 = require("child_process");
const fs = require("fs");
const path = require("path");
const sniffer_linux_1 = require("./sniffer-linux");
const sniffer_mac_1 = require("./sniffer-mac");
const sniffer_windows_1 = require("./sniffer-windows");
const lookupMap = {
    win32: sniffer_windows_1.default,
    darwin: sniffer_mac_1.default,
    linux: sniffer_linux_1.default,
};
let chromeArgs = ['--enable-devtools-experiments', '--no-first-run'];
function launch(url, options = {}) {
    setupUserProfileArg();
    return new Promise((resolve, reject) => {
        lookupForChromePath(options).then(chromeBinaryPath => {
            const chromeProcess = launchChrome(chromeBinaryPath, url, options);
            resolve(chromeProcess);
        }, err => {
            reject(err);
        });
    });
}
function lookupForChromePath(options) {
    return new Promise((resolve, reject) => {
        let chromePath = options.chromePath;
        if (chromePath) {
            try {
                chromePath = path.resolve(chromePath);
                fs.accessSync(chromePath);
                return resolve(chromePath);
            }
            catch (err) {
                return reject(err);
            }
        }
        else {
            const lookupMethod = lookupMap[process.platform];
            lookupMethod().then(value => {
                return resolve(value);
            });
        }
    });
}
function setupUserProfileArg() {
    chromeArgs = chromeArgs.concat([
        `--user-data-dir=${path.resolve(__dirname, '.devProfile')}`,
    ]);
}
function launchChrome(filePath, url, options) {
    let child;
    const argsUrl = chromeArgs.concat([url]);
    try {
        child = child_process_1.spawn(filePath, argsUrl);
    }
    catch (err) {
        onLaunchChromeError(err);
        return err;
    }
    child.on('error', onLaunchChromeError);
    child.on('exit', onChromeExit);
    return child;
    function onLaunchChromeError(err) {
        typeof options.onError === 'function' && options.onError(err);
    }
    function onChromeExit() {
        typeof options.onExit === 'function' && options.onExit();
    }
}
module.exports = { launch };