Merge pull request #608 from crummy/cannot_find_npm

Clear error if we cannot find the desired command
This commit is contained in:
Renaud Pawlak 2020-07-25 10:20:00 +02:00 committed by GitHub
commit 6a8ac4e5db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -25,10 +25,7 @@ import java.nio.file.FileVisitOption;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Arrays; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -123,14 +120,13 @@ public class ProcessUtil {
return unixPath; return unixPath;
} }
logger.info("cannot find " + command + " - searching in node_modules files"); logger.info("cannot find " + command + " - searching in " + NPM_DIR.getAbsolutePath());
unixPath = lookupGlobalNpmPackageExecutablePath(NPM_DIR, command); return lookupGlobalNpmPackageExecutablePath(NPM_DIR, command)
.orElseThrow(() -> new RuntimeException("Could not locate command " + command));
return unixPath;
} }
} }
private static String lookupGlobalNpmPackageExecutablePath(File directory, String executableName) { private static Optional<String> lookupGlobalNpmPackageExecutablePath(File directory, String executableName) {
if (directory != null) { if (directory != null) {
File[] children = directory.listFiles(); File[] children = directory.listFiles();
if (children != null) { if (children != null) {
@ -139,12 +135,12 @@ public class ProcessUtil {
if (child.getName().equals("bin")) { if (child.getName().equals("bin")) {
for (File binFile : child.listFiles()) { for (File binFile : child.listFiles()) {
if (binFile.getName().equals(executableName)) { if (binFile.getName().equals(executableName)) {
return binFile.getAbsolutePath(); return Optional.of(binFile.getAbsolutePath());
} }
} }
} else { } else {
String foundPath = lookupGlobalNpmPackageExecutablePath(child, executableName); Optional<String> foundPath = lookupGlobalNpmPackageExecutablePath(child, executableName);
if (foundPath != null) { if (foundPath.isPresent()) {
return foundPath; return foundPath;
} }
} }
@ -152,7 +148,7 @@ public class ProcessUtil {
} }
} }
} }
return null; return Optional.empty();
} }
/** /**