-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstall.js
119 lines (80 loc) · 3.98 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 fs = require(`fs`)
const fetch = require(`node-fetch`)
const AdmZip = require(`adm-zip`)
const CONFIG = require(`./config`)
const platform = process.platform
const architecture = process.arch
console.info(`Fetching release assets from GitHub...`)
fetch(`${CONFIG.GitHubReleasesUrl}/${CONFIG.OpenDirectoryDownloaderVersion.releaseId}/assets`).then(res => res.json()).then(assets => {
if (assets?.message?.includes(`API rate limit exceeded`)) {
console.error(`GitHub API rate limit exceeded, cannot fetch OpenDirectoryDownloader executable!`)
process.exit(1)
}
const version = CONFIG.OpenDirectoryDownloaderVersion.version.match(/^v?(\d+\.\d+\.\d+\.\d+)$/)[1]
let releaseName
if (architecture === `arm` && platform === `linux`) {
releaseName = `OpenDirectoryDownloader-${version}-linux-arm-self-contained.zip`
} else if (architecture === `arm64` && platform === `linux`) {
releaseName = `OpenDirectoryDownloader-${version}-linux-arm64-self-contained.zip`
} else if (architecture === `x64` && platform === `linux`) {
releaseName = `OpenDirectoryDownloader-${version}-linux-x64-self-contained.zip`
} else if (architecture === `x64` && platform === `darwin`) {
releaseName = `OpenDirectoryDownloader-${version}-osx-x64-self-contained.zip`
} else if (architecture === `x64` && platform === `win32`) {
releaseName = `OpenDirectoryDownloader-${version}-win-x64-self-contained.zip`
} else {
throw new Error(`Platform '${platform}' on architecture '${architecture}' is not supported by OpenDirectoryDownloader :(\nYou could try requesting support for it at https://github.com/KoalaBear84/OpenDirectoryDownloader ...`)
}
let asset = assets.find(asset => asset.name === releaseName)
if (!asset) {
throw new Error(`Executable not found on GitHub: ${releaseName} for version '${CONFIG.OpenDirectoryDownloaderVersion.version}'`)
}
let downloadUrl = asset.browser_download_url
// console.log(`Creating ODD directory...`)
if (fs.existsSync(CONFIG.OpenDirectoryDownloaderFolder) && fs.lstatSync(CONFIG.OpenDirectoryDownloaderFolder).isDirectory()) {
console.info(`Removing old version of OpenDirectoryDownloader`)
const allDirents = fs.readdirSync(CONFIG.OpenDirectoryDownloaderFolder, {
withFileTypes: true,
})
const direntsToRemove = allDirents.filter(dirent => {
if (dirent.isDirectory()) {
return ![CONFIG.OpenDirectoryDownloaderOutputFolderName].includes(dirent.name)
} else {
return ![].includes(dirent.name)
}
})
for (const dirent of direntsToRemove) {
fs.rmSync(`${CONFIG.OpenDirectoryDownloaderFolder}/${dirent.name}`, {
recursive: true,
})
}
} else {
fs.mkdirSync(CONFIG.OpenDirectoryDownloaderFolder)
}
if (!fs.existsSync(CONFIG.OpenDirectoryDownloaderOutputFolder)) {
fs.mkdirSync(CONFIG.OpenDirectoryDownloaderOutputFolder)
}
console.info(`Starting download of OpenDirectoryDownloader executable...`)
fetch(downloadUrl)
.then(res => {
if (!res.ok) {
throw new Error(`Failed to download executable from GitHub!`)
}
const dest = fs.createWriteStream(`${CONFIG.OpenDirectoryDownloaderFolder}/ODD.zip`)
res.body.pipe(dest)
dest.on(`finish`, () => {
let zip = new AdmZip(`${CONFIG.OpenDirectoryDownloaderFolder}/ODD.zip`)
zip.extractAllTo(CONFIG.OpenDirectoryDownloaderFolder, true)
// console.log(`Deleting zip after extraction...`)
fs.unlinkSync(`${CONFIG.OpenDirectoryDownloaderFolder}/ODD.zip`)
if (platform === `linux`) {
// make executable
console.log(`Making binary executable...`)
fs.chmodSync(`${CONFIG.OpenDirectoryDownloaderFolder}/OpenDirectoryDownloader`, 0o755)
}
console.info(`OpenDirectoryDownloader has been installed successfully to ${CONFIG.OpenDirectoryDownloaderFolder}!`)
})
});
}).catch(err => {
throw new Error(`Failed to retrieve OpenDirectoryDownloader executable from GitHub: ${err}`)
})