mh-two-thousand-and-two
2024-04-12 7fc6dbf547b8899d949b67cdec36b96a7d1701c7
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const child_process_1 = require("child_process");
const fs = require("fs");
const path = require("path");
function lookupChromeWindows() {
    return new Promise((resolve, reject) => {
        let chromePath = '';
        const chromeSysRegHKey = '"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\chrome.exe" /v path';
        child_process_1.exec(`REG QUERY ${chromeSysRegHKey}`, {}, (err, stdout) => {
            if (!err) {
                chromePath = downgradLookup();
                if (!chromePath) {
                    reject(err);
                    return;
                }
                else {
                    resolve(chromePath);
                    return;
                }
            }
            if (typeof stdout === 'string') {
                chromePath = parseChromePathFromRegOutput(stdout);
            }
            if (!chromePath) {
                reject(new Error(`无法解析Reg Query输出`));
                return;
            }
            resolve(chromePath);
        });
    });
}
exports.default = lookupChromeWindows;
function downgradLookup() {
    let chromePath = '';
    const suffix = '\\Google\\Chrome\\Application\\chrome.exe';
    const prefixes = [
        process.env.LOCALAPPDATA || '',
        process.env.PROGRAMFILES || '',
        process.env['PROGRAMFILES(X86)'] || '',
    ];
    for (const prefix of prefixes) {
        try {
            chromePath = path.join(prefix, suffix);
            fs.accessSync(chromePath);
            break;
        }
        catch (err) { }
    }
    return chromePath;
}
function parseChromePathFromRegOutput(stdout) {
    const lines = stdout.split('\n');
    let testPath = '', chromePath = '';
    lines.every(line => {
        const targLine = line.match(/path\s+reg_sz\s+(.+)/i);
        if (targLine) {
            testPath = targLine[1];
            return false;
        }
        return true;
    });
    try {
        testPath = path.resolve(testPath, 'chrome.exe');
        fs.accessSync(testPath);
        chromePath = testPath;
    }
    catch (err) { }
    return chromePath;
}