-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinstallJava.js
183 lines (165 loc) · 4.85 KB
/
installJava.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import path from "path";
import { tmpdir as _tmpdir } from "os";
import { x } from "tar";
import fs from 'fs-extra';
import AdmZip from 'adm-zip';
import { $ } from 'execa';
import { getJrePath, getJreBin, fetch } from "./utils.js";
const unzipDirectory = async (inputFilePath, outputDirectory) => {
const zip = new AdmZip(inputFilePath);
return new Promise((resolve, reject) => {
zip.extractAllToAsync(outputDirectory, true, (error) => {
if (error) {
console.log(error);
reject(error);
} else {
console.log(`Extracted to "${outputDirectory}" successfully`);
resolve();
}
});
});
};
const extractFileName = (contentDisposition) => {
let filename = 'OpenJDK11.zip'; // default
if (typeof contentDisposition === 'string' && contentDisposition.startsWith('attachment;')) {
const parts = contentDisposition.split(';');
const namePart = parts.length > 1 ? parts[1].trimStart() : undefined;
if (typeof namePart === 'string' && (namePart.startsWith('filename=') || namePart.startsWith('"filename"='))) {
const equalIndex = namePart.indexOf('=');
filename = namePart.substring(equalIndex + 1).split(';')[0];
if (filename.startsWith('"') && filename.trimEnd().endsWith('"')) {
filename = filename.substring(1, filename.length - 2);
}
}
};
return filename;
};
const download = async (dir, url) => {
console.log(`Downloading JRE archive from ${url} into ${dir}`);
fs.ensureDirSync(dir);
const response = await fetch(url);
const attachmentName = extractFileName(response?.headers['content-disposition']);
const destFile = path.join(dir, attachmentName);
if (response && response?.data) {
console.log(`Downloaded the JRE archive, saving as '${destFile}'`);
fs.writeFileSync(destFile, response.data);
console.log(`Saved JRE archive in: ${destFile}`);
return destFile
} else {
throw new Error('Download failed :(')
}
};
const move = (file) => {
const newFile = path.join(path.resolve('.'), file.split(path.sep).slice(-1)[0]);
console.log(`Moving ${file} to ${newFile}`);
fs.copyFileSync(file, newFile);
fs.unlinkSync(file);
return newFile;
}
const extractTarGz = async (file, dir) => {
console.log(`Extracting ${file} into ${dir}`);
x({file, cwd: dir});
fs.unlinkSync(file);
return dir;
}
const extract = async (file) => {
const dir = getJrePath();
fs.ensureDirSync(dir);
const extension = path.extname(file);
if (extension === '.zip') {
console.log('Extracting ZIP...');
await unzipDirectory(file, dir);
} else {
console.log('Extracting TarGz...');
await extractTarGz(file, dir);
}
await fs.unlink(file);
return dir;
};
const installJre = async () => {
console.log('Getting fresh copy of JRE...')
const javaDir = await install();
console.log({ javaDir });
return javaDir
};
const install = async () => {
const version = 11;
const options = {
openjdk_impl: "hotspot",
release: "latest",
type: "jre",
heap_size: "normal",
vendor: "adoptopenjdk"
};
const endpoint = "api.adoptopenjdk.net";
const versionPath = "latest/" + version + "/ga";
switch (process.platform) {
case "aix":
options.os = "aix";
break;
case "darwin":
options.os = "mac";
break;
case "linux":
options.os = "linux";
break;
case "sunos":
options.os = "solaris";
break;
case "win32":
options.os = "windows";
break;
default:
throw new Error(`Unsupported operating system ${process.platform}`)
}
if (/^ppc64|s390x|x32|x64$/g.test(process.arch)) options.arch = process.arch;
else if (process.arch === "ia32") options.arch = "x32";
else if (process.arch === "arm64") options.arch = "aarch64";
else
throw new Error(`Unsupported architecture ${process.arch}`)
const url =
"https://" +
endpoint +
"/v3/binary/" +
versionPath +
"/" +
options.os +
"/" +
options.arch +
"/" +
options.type +
"/" +
options.openjdk_impl +
"/" +
options.heap_size +
"/" +
options.vendor;
const tmpdir = path.join(_tmpdir(), "jre");
console.log("Java URL: " + url);
const file = await download(tmpdir, url);
const newFile = move(file);
return await extract(newFile);
};
const isJavaInstalled = async () => {
const jreBin = getJreBin()
if (jreBin) {
const result = await $`${jreBin} -version`;
return result.exitCode === 0;
} else {
return false;
}
};
const ensure = async () => {
let javaInstalled = false;
console.log('Checking for JRE...');
javaInstalled = await isJavaInstalled();
if (!javaInstalled) {
const javaDir = await installJre();
if (javaDir) {
javaInstalled = await isJavaInstalled();
}
}
console.log({ javaInstalled });
return javaInstalled;
};
ensure();