forked from getgauge/taiko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateChromium.js
76 lines (70 loc) · 2.53 KB
/
updateChromium.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
const https = require('https');
const { writeFileSync, readFileSync } = require('fs');
const { execSync } = require('child_process');
const BrowserFetcher = require('./lib/browserFetcher');
const supportedPlatforms = BrowserFetcher.supportedPlatforms;
async function checkAvailableRevision(revision, browserFetcher) {
let results = await Promise.all(
supportedPlatforms.map((platform) => browserFetcher.canDownload(revision, platform)),
);
if (results.includes(false)) {
return await checkAvailableRevision(--revision, browserFetcher);
} else {
return revision;
}
}
async function findLatestCommonRevision(chromiumReleases) {
let latestRevisions = chromiumReleases
.filter((release) => supportedPlatforms.includes(release.os))
.map((release) => {
let versions = release.versions.map((version) => parseInt(version.branch_base_position));
return Math.max(...versions);
});
return await checkAvailableRevision(Math.min(...latestRevisions), new BrowserFetcher());
}
function updatePackageJSON(key, value) {
let packageJSON = JSON.parse(readFileSync('./package.json', 'utf-8'));
if (key === 'revision') {
packageJSON.taiko.chromium_revision = `${value}`;
} else {
packageJSON.taiko.chromium_version = `${value}`;
}
writeFileSync('./package.json', `${JSON.stringify(packageJSON, null, ' ')}\n`);
}
async function getChromeReleasesInfo() {
return new Promise((resolve, reject) => {
https
.get('https://omahaproxy.appspot.com/all.json', (res) => {
let data = '';
res.on('data', (datum) => {
data += datum;
});
res.on('end', () => {
resolve(data);
});
})
.on('error', (e) => {
reject(e);
});
});
}
async function main() {
let releasesInfo = await getChromeReleasesInfo();
let revision = await findLatestCommonRevision(JSON.parse(releasesInfo));
let { chromium_revision } = require('./package.json').taiko;
if (chromium_revision >= revision) {
console.log(
`Skipping updating package.json as current chromium revision(${chromium_revision}) is similar or greater than available revision(${revision}).`,
);
return;
}
updatePackageJSON('revision', revision);
execSync('node ./lib/install.js');
let browserFetcher = new BrowserFetcher();
let { executablePath } = browserFetcher.revisionInfo(revision);
let out = execSync(`${executablePath} --version`);
let versionInfo = out.toString();
let version = versionInfo.replace(/[^0-9.]/g, '');
updatePackageJSON('version', version);
}
main();