-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
65 lines (43 loc) · 1.34 KB
/
index.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
"use strict";
const findChrome = require('./chrome-finder');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const path = require('path');
const readdir = util.promisify(require('fs').readdir);
async function getChromeVersionFromCli() {
let chromePath;
try {
chromePath = findChrome();
} catch (err) {
return null;
}
const res = await exec(chromePath.replace(/ /g, '\\ ') + ' --version');
const version = res.stdout.substr(14).trim();
return version;
}
async function getChromeVersionWin() {
let chromePath;
try {
chromePath = findChrome();
} catch (err) {
return null;
}
const versionPath = path.dirname(chromePath);
const contents = await readdir(versionPath);
const versions = contents.filter(a => /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/g.test(a));
const latest = versions.sort((a,b) => a<b)[0];
return latest;
}
async function getChromeVersion() {
const os = process.platform;
if (os === 'darwin' || os === 'linux') return getChromeVersionFromCli();
if (os.includes('win')) return getChromeVersionWin();
console.log(`${os} is not supported`);
return null;
}
if (require.main == module) {
getChromeVersion().then(v => console.log(v));
}
module.exports = {
getChromeVersion
};