-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall.js
119 lines (103 loc) · 3.39 KB
/
install.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const axios = require('axios').default;
const fs = require('fs');
const { pipeline } = require('stream');
const extract = require('extract-zip');
const pjson = require('./package.json');
const TARGET_FILE_PATH = `/bin/datree.zip`;
// possible values: 'arm', 'arm64', 'ia32', 'mips','mipsel', 'ppc', 'ppc64', 's390', 's390x', 'x32', or 'x64'
const mapDatreeAssets = {
darwin: {
arm64: 'darwin_arm64',
x86_64: 'darwin_x86_64',
x64: 'darwin_x86_64',
},
linux: {
arm64: 'linux_arm64',
x86_64: 'linux_x86_64',
386: 'linux_386',
x64: 'linux_x86_64',
},
win32: {
386: 'windows_386',
x86_64: 'windows_x86_64',
x64: 'windows_x86_64',
},
};
async function getDatreeLatestRelease(datreeVersion) {
const url = `https://api.github.com/repos/datreeio/datree/releases/tags/${datreeVersion}`;
console.log(`🌳 Getting latest release from ${url}`);
try {
const response = await axios.get(url, {
headers: {
'User-Agent': 'nodejs',
Accept: 'application/vnd.github.v3+json',
},
});
if (response.status === 200) {
console.log(`🌳 Latest release: ${response.data.tag_name}`);
const platform = process.platform;
let arch = process.arch;
console.log(`🌳 Platform: ${platform}`);
console.log(`🌳 Arch: ${arch}`);
const assetName = mapDatreeAssets[platform][arch];
if (!assetName) {
throw new Error(`🌳 Unsupported platform: ${platform} ${arch}`);
}
console.log(`🌳 Asset name: ${assetName}`);
const assets = response.data.assets;
const matchUrl = assets.find((asset) => {
const browser_download_url = asset.browser_download_url.toLowerCase();
console.log(`🌳 Checking asset: ${browser_download_url}`);
return browser_download_url.includes(assetName);
});
return matchUrl.browser_download_url;
}
} catch (error) {
throw new Error(`🌳 Failed to get latest release: ${error}`);
}
}
async function downloadFile(url, targetFile) {
const response = await axios({
url,
method: 'GET',
responseType: 'stream',
});
const writeStream = fs.createWriteStream(`./${targetFile}`);
const downloadResult = await new Promise((resolve, reject) => {
pipeline(response.data, writeStream, (err) => {
if (err) {
reject(err);
} else {
resolve(response.status);
}
});
});
return downloadResult;
}
async function unzipDatreeInDir() {
console.log('🌳 Unzipping datree...');
const absolutePath = `${process.cwd()}`;
try {
await extract(`${absolutePath}${TARGET_FILE_PATH}`, {
dir: `${absolutePath}/bin`,
});
console.log('🌳 datree unzipped successfully');
} catch (error) {
throw new Error(`🌳 Failed to get unzip datree in dir: ${error}`);
}
}
async function downloadDatree() {
const datreeVersion = pjson.datree_version || 'latest';
fs.existsSync('bin') || fs.mkdirSync('bin');
const downloadUrl = await getDatreeLatestRelease(datreeVersion);
console.log(`🌳 Downloading datree from ${downloadUrl}`);
const downloadResult = await downloadFile(downloadUrl, TARGET_FILE_PATH);
console.log(`🌳 Download result: ${downloadResult}`);
if (downloadResult === 200) {
await unzipDatreeInDir(TARGET_FILE_PATH);
console.log('🌳 datree downloaded successfully');
} else {
console.log('🌳 Failed to download datree');
}
}
downloadDatree();