/
NodeInstaller.java
317 lines (259 loc) · 13 KB
/
NodeInstaller.java
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package com.github.eirslett.maven.plugins.frontend.lib;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class NodeInstaller {
public static final String INSTALL_PATH = "/node";
public static final String DEFAULT_NODEJS_DOWNLOAD_ROOT = "https://nodejs.org/dist/";
private static final Object LOCK = new Object();
private String npmVersion, nodeVersion, nodeDownloadRoot, userName, password;
private final Logger logger;
private final InstallConfig config;
private final ArchiveExtractor archiveExtractor;
private final FileDownloader fileDownloader;
NodeInstaller(InstallConfig config, ArchiveExtractor archiveExtractor, FileDownloader fileDownloader) {
this.logger = LoggerFactory.getLogger(getClass());
this.config = config;
this.archiveExtractor = archiveExtractor;
this.fileDownloader = fileDownloader;
}
public NodeInstaller setNodeVersion(String nodeVersion) {
this.nodeVersion = nodeVersion;
return this;
}
public NodeInstaller setNodeDownloadRoot(String nodeDownloadRoot) {
this.nodeDownloadRoot = nodeDownloadRoot;
return this;
}
public NodeInstaller setNpmVersion(String npmVersion) {
this.npmVersion = npmVersion;
return this;
}
public NodeInstaller setUserName(String userName) {
this.userName = userName;
return this;
}
public NodeInstaller setPassword(String password) {
this.password = password;
return this;
}
private boolean npmProvided() throws InstallationException {
if (this.npmVersion != null) {
if ("provided".equals(this.npmVersion)) {
if (Integer.parseInt(this.nodeVersion.replace("v", "").split("[.]")[0]) < 4) {
throw new InstallationException("NPM version is '" + this.npmVersion
+ "' but Node didn't include NPM prior to v4.0.0");
}
return true;
}
return false;
}
return false;
}
public void install() throws InstallationException {
// use static lock object for a synchronized block
synchronized (LOCK) {
if (this.nodeDownloadRoot == null || this.nodeDownloadRoot.isEmpty()) {
this.nodeDownloadRoot = DEFAULT_NODEJS_DOWNLOAD_ROOT;
}
if (!nodeIsAlreadyInstalled()) {
this.logger.info("Installing node version {}", this.nodeVersion);
if (!this.nodeVersion.startsWith("v")) {
this.logger.warn("Node version does not start with naming convention 'v'.");
}
if (this.config.getPlatform().isWindows()) {
if (npmProvided()) {
installNodeWithNpmForWindows();
} else {
installNodeForWindows();
}
} else {
installNodeDefault();
}
}
}
}
private boolean nodeIsAlreadyInstalled() {
try {
NodeExecutorConfig executorConfig = new InstallNodeExecutorConfig(this.config);
File nodeFile = executorConfig.getNodePath();
if (nodeFile.exists()) {
final String version =
new NodeExecutor(executorConfig, Arrays.asList("--version"), null).executeAndGetResult(logger);
if (version.equals(this.nodeVersion)) {
this.logger.info("Node {} is already installed.", version);
return true;
} else {
this.logger.info("Node {} was installed, but we need version {}", version,
this.nodeVersion);
return false;
}
} else {
return false;
}
} catch (ProcessExecutionException e) {
return false;
}
}
private void installNodeDefault() throws InstallationException {
try {
final String longNodeFilename =
this.config.getPlatform().getLongNodeFilename(this.nodeVersion, false);
String downloadUrl = this.nodeDownloadRoot
+ this.config.getPlatform().getNodeDownloadFilename(this.nodeVersion, false);
String classifier = this.config.getPlatform().getNodeClassifier();
File tmpDirectory = getTempDirectory();
CacheDescriptor cacheDescriptor = new CacheDescriptor("node", this.nodeVersion, classifier,
this.config.getPlatform().getArchiveExtension());
File archive = this.config.getCacheResolver().resolve(cacheDescriptor);
downloadFileIfMissing(downloadUrl, archive, this.userName, this.password);
extractFile(archive, tmpDirectory);
// Search for the node binary
File nodeBinary =
new File(tmpDirectory, longNodeFilename + File.separator + "bin" + File.separator + "node");
if (!nodeBinary.exists()) {
throw new FileNotFoundException(
"Could not find the downloaded Node.js binary in " + nodeBinary);
} else {
File destinationDirectory = getInstallDirectory();
File destination = new File(destinationDirectory, "node");
this.logger.info("Copying node binary from {} to {}", nodeBinary, destination);
if (!nodeBinary.renameTo(destination)) {
throw new InstallationException("Could not install Node: Was not allowed to rename "
+ nodeBinary + " to " + destination);
}
if (!destination.setExecutable(true, false)) {
throw new InstallationException(
"Could not install Node: Was not allowed to make " + destination + " executable.");
}
if (npmProvided()) {
File tmpNodeModulesDir = new File(tmpDirectory,
longNodeFilename + File.separator + "lib" + File.separator + "node_modules");
File nodeModulesDirectory = new File(destinationDirectory, "node_modules");
File npmDirectory = new File(nodeModulesDirectory, "npm");
FileUtils.copyDirectory(tmpNodeModulesDir, nodeModulesDirectory);
this.logger.info("Extracting NPM");
// create a copy of the npm scripts next to the node executable
for (String script : Arrays.asList("npm", "npm.cmd")) {
File scriptFile = new File(npmDirectory, "bin" + File.separator + script);
if (scriptFile.exists()) {
scriptFile.setExecutable(true);
}
}
}
deleteTempDirectory(tmpDirectory);
this.logger.info("Installed node locally.");
}
} catch (IOException e) {
throw new InstallationException("Could not install Node", e);
} catch (DownloadException e) {
throw new InstallationException("Could not download Node.js", e);
} catch (ArchiveExtractionException e) {
throw new InstallationException("Could not extract the Node archive", e);
}
}
private void installNodeWithNpmForWindows() throws InstallationException {
try {
final String longNodeFilename =
this.config.getPlatform().getLongNodeFilename(this.nodeVersion, true);
String downloadUrl = this.nodeDownloadRoot
+ this.config.getPlatform().getNodeDownloadFilename(this.nodeVersion, true);
String classifier = this.config.getPlatform().getNodeClassifier();
File tmpDirectory = getTempDirectory();
CacheDescriptor cacheDescriptor = new CacheDescriptor("node", this.nodeVersion, classifier,
this.config.getPlatform().getArchiveExtension());
File archive = this.config.getCacheResolver().resolve(cacheDescriptor);
downloadFileIfMissing(downloadUrl, archive, this.userName, this.password);
extractFile(archive, tmpDirectory);
// Search for the node binary
File nodeBinary = new File(tmpDirectory, longNodeFilename + File.separator + "node.exe");
if (!nodeBinary.exists()) {
throw new FileNotFoundException(
"Could not find the downloaded Node.js binary in " + nodeBinary);
} else {
File destinationDirectory = getInstallDirectory();
File destination = new File(destinationDirectory, "node.exe");
this.logger.info("Copying node binary from {} to {}", nodeBinary, destination);
if (!nodeBinary.renameTo(destination)) {
throw new InstallationException("Could not install Node: Was not allowed to rename "
+ nodeBinary + " to " + destination);
}
if ("provided".equals(this.npmVersion)) {
File tmpNodeModulesDir =
new File(tmpDirectory, longNodeFilename + File.separator + "node_modules");
File nodeModulesDirectory = new File(destinationDirectory, "node_modules");
FileUtils.copyDirectory(tmpNodeModulesDir, nodeModulesDirectory);
}
deleteTempDirectory(tmpDirectory);
this.logger.info("Installed node locally.");
}
} catch (IOException e) {
throw new InstallationException("Could not install Node", e);
} catch (DownloadException e) {
throw new InstallationException("Could not download Node.js", e);
} catch (ArchiveExtractionException e) {
throw new InstallationException("Could not extract the Node archive", e);
}
}
private void installNodeForWindows() throws InstallationException {
final String downloadUrl = this.nodeDownloadRoot
+ this.config.getPlatform().getNodeDownloadFilename(this.nodeVersion, false);
try {
File destinationDirectory = getInstallDirectory();
File destination = new File(destinationDirectory, "node.exe");
String classifier = this.config.getPlatform().getNodeClassifier();
CacheDescriptor cacheDescriptor =
new CacheDescriptor("node", this.nodeVersion, classifier, "exe");
File binary = this.config.getCacheResolver().resolve(cacheDescriptor);
downloadFileIfMissing(downloadUrl, binary, this.userName, this.password);
this.logger.info("Copying node binary from {} to {}", binary, destination);
FileUtils.copyFile(binary, destination);
this.logger.info("Installed node locally.");
} catch (DownloadException e) {
throw new InstallationException("Could not download Node.js from: " + downloadUrl, e);
} catch (IOException e) {
throw new InstallationException("Could not install Node.js", e);
}
}
private File getTempDirectory() {
File tmpDirectory = new File(getInstallDirectory(), "tmp");
if (!tmpDirectory.exists()) {
this.logger.debug("Creating temporary directory {}", tmpDirectory);
tmpDirectory.mkdirs();
}
return tmpDirectory;
}
private File getInstallDirectory() {
File installDirectory = new File(this.config.getInstallDirectory(), INSTALL_PATH);
if (!installDirectory.exists()) {
this.logger.debug("Creating install directory {}", installDirectory);
installDirectory.mkdirs();
}
return installDirectory;
}
private void deleteTempDirectory(File tmpDirectory) throws IOException {
if (tmpDirectory != null && tmpDirectory.exists()) {
this.logger.debug("Deleting temporary directory {}", tmpDirectory);
FileUtils.deleteDirectory(tmpDirectory);
}
}
private void extractFile(File archive, File destinationDirectory) throws ArchiveExtractionException {
this.logger.info("Unpacking {} into {}", archive, destinationDirectory);
this.archiveExtractor.extract(archive.getPath(), destinationDirectory.getPath());
}
private void downloadFileIfMissing(String downloadUrl, File destination, String userName, String password)
throws DownloadException {
if (!destination.exists()) {
downloadFile(downloadUrl, destination, userName, password);
}
}
private void downloadFile(String downloadUrl, File destination, String userName, String password)
throws DownloadException {
this.logger.info("Downloading {} to {}", downloadUrl, destination);
this.fileDownloader.download(downloadUrl, destination.getPath(), userName, password);
}
}