forked from redhat-developer/vscode-openshift-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify-tools.ts
More file actions
80 lines (75 loc) · 2.83 KB
/
verify-tools.ts
File metadata and controls
80 lines (75 loc) · 2.83 KB
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
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/
/* eslint-disable no-console */
/* eslint-disable guard-for-in */
/* eslint-disable no-restricted-syntax */
/* eslint-disable no-await-in-loop */
/* eslint-disable @typescript-eslint/no-misused-promises */
import { exit } from 'shelljs';
import { DownloadUtil } from '../src/downloadBinaries/download';
import hasha = require('hasha');
import mkdirp = require('mkdirp');
import fs = require('fs-extra');
import path = require('path');
import cp = require('child_process');
import os = require('os');
import configData = require('../src/tools.json');
/**
* Download reqURL to targetFolder and save it to fileName. Verify the downloaded file sha256 is matching sha256sum
* @param targetFolder
* @param fileName
* @param reqURL
* @param sha256sum
*/
async function downloadFileAndCreateSha256(
targetFolder: string,
fileName: string,
reqURL: string,
sha256sum: string,
): Promise<void> {
if (!fs.existsSync(targetFolder)) {
mkdirp.sync(targetFolder);
}
const currentFile = path.join(targetFolder, fileName);
console.log(`${currentFile} download started from ${reqURL}`);
await DownloadUtil.downloadFile(reqURL, currentFile, (current) => console.log(`${current}%`));
const currentSHA256 = await hasha.fromFile(currentFile, { algorithm: 'sha256' });
if (currentSHA256 === sha256sum) {
console.log(`[INFO] ${currentFile} is downloaded and sha256 is correct`);
} else {
throw Error(`${currentFile} is downloaded and sha256 is not correct`);
}
}
async function verifyTools(): Promise<void> {
for (const key in configData) {
for (const OS in configData[key].platform) {
const targetFolder = path.resolve(os.tmpdir(), OS);
await downloadFileAndCreateSha256(
targetFolder,
configData[key].platform[OS].dlFileName,
configData[key].platform[OS].url,
configData[key].platform[OS].sha256sum,
);
}
}
}
const fileCheckRegex = /\w*tools.json/;
cp.exec('git diff --name-only origin/master -- .', async (error, stdout) => {
if (error) {
throw error;
}
console.log('The changed files:');
console.log(stdout);
if (fileCheckRegex.test(stdout)) {
console.log('tools.json is changed, starting download verification');
try {
await verifyTools();
} catch (err) {
exit(1);
}
} else {
console.log('tools.json is not changed, skipping download verification');
}
});