|
1 | 1 | /*
|
2 |
| - * Copyright 2020 DiffPlug |
| 2 | + * Copyright 2020-2023 DiffPlug |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 | package com.diffplug.spotless.npm;
|
17 | 17 |
|
18 | 18 | import java.io.File;
|
19 |
| -import java.util.Arrays; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Collections; |
20 | 21 | import java.util.List;
|
21 | 22 | import java.util.Optional;
|
22 | 23 |
|
23 | 24 | public class NpmPathResolver {
|
24 | 25 |
|
25 | 26 | private final File explicitNpmExecutable;
|
26 | 27 |
|
| 28 | + private final File explicitNodeExecutable; |
| 29 | + |
27 | 30 | private final File explicitNpmrcFile;
|
28 | 31 |
|
29 | 32 | private final List<File> additionalNpmrcLocations;
|
30 | 33 |
|
31 |
| - public NpmPathResolver(File explicitNpmExecutable, File explicitNpmrcFile, File... additionalNpmrcLocations) { |
| 34 | + public NpmPathResolver(File explicitNpmExecutable, File explicitNodeExecutable, File explicitNpmrcFile, List<File> additionalNpmrcLocations) { |
32 | 35 | this.explicitNpmExecutable = explicitNpmExecutable;
|
| 36 | + this.explicitNodeExecutable = explicitNodeExecutable; |
33 | 37 | this.explicitNpmrcFile = explicitNpmrcFile;
|
34 |
| - this.additionalNpmrcLocations = Arrays.asList(additionalNpmrcLocations); |
| 38 | + this.additionalNpmrcLocations = Collections.unmodifiableList(new ArrayList<>(additionalNpmrcLocations)); |
35 | 39 | }
|
36 | 40 |
|
| 41 | + /** |
| 42 | + * Finds the npm executable to use. |
| 43 | + * <br> |
| 44 | + * Either the explicit npm executable is returned, or - if an explicit node executable is configured - tries to find |
| 45 | + * the npm executable relative to the node executable. |
| 46 | + * Falls back to looking for npm on the user's system using {@link NpmExecutableResolver} |
| 47 | + * |
| 48 | + * @return the npm executable to use |
| 49 | + * @throws IllegalStateException if no npm executable could be found |
| 50 | + */ |
37 | 51 | public File resolveNpmExecutable() {
|
38 |
| - return Optional.ofNullable(this.explicitNpmExecutable) |
39 |
| - .orElseGet(() -> NpmExecutableResolver.tryFind() |
40 |
| - .orElseThrow(() -> new IllegalStateException("Can't automatically determine npm executable and none was specifically supplied!\n\n" + NpmExecutableResolver.explainMessage()))); |
| 52 | + if (this.explicitNpmExecutable != null) { |
| 53 | + return this.explicitNpmExecutable; |
| 54 | + } |
| 55 | + if (this.explicitNodeExecutable != null) { |
| 56 | + File nodeExecutableCandidate = new File(this.explicitNodeExecutable.getParentFile(), NpmExecutableResolver.npmExecutableName()); |
| 57 | + if (nodeExecutableCandidate.canExecute()) { |
| 58 | + return nodeExecutableCandidate; |
| 59 | + } |
| 60 | + } |
| 61 | + return NpmExecutableResolver.tryFind() |
| 62 | + .orElseThrow(() -> new IllegalStateException("Can't automatically determine npm executable and none was specifically supplied!\n\n" + NpmExecutableResolver.explainMessage())); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Finds the node executable to use. |
| 67 | + * <br> |
| 68 | + * Either the explicit node executable is returned, or tries to find the node executable relative to the npm executable |
| 69 | + * found by {@link #resolveNpmExecutable()}. |
| 70 | + * @return the node executable to use |
| 71 | + * @throws IllegalStateException if no node executable could be found |
| 72 | + */ |
| 73 | + public File resolveNodeExecutable() { |
| 74 | + if (this.explicitNodeExecutable != null) { |
| 75 | + return this.explicitNodeExecutable; |
| 76 | + } |
| 77 | + File npmExecutable = resolveNpmExecutable(); |
| 78 | + return NodeExecutableResolver.tryFindNextTo(npmExecutable) |
| 79 | + .orElseThrow(() -> new IllegalStateException("Can't automatically determine node executable and none was specifically supplied!\n\n" + NodeExecutableResolver.explainMessage())); |
41 | 80 | }
|
42 | 81 |
|
43 | 82 | public String resolveNpmrcContent() {
|
|
0 commit comments