From 37652e2ed6e635808c34f671bc98e7efda57a2ee Mon Sep 17 00:00:00 2001 From: crummy Date: Sat, 25 Jul 2020 19:39:22 +1200 Subject: [PATCH] Clear error if we cannot find the desired command --- .../jsweet/transpiler/util/ProcessUtil.java | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/transpiler/src/main/java/org/jsweet/transpiler/util/ProcessUtil.java b/transpiler/src/main/java/org/jsweet/transpiler/util/ProcessUtil.java index eae48a2f..44533a96 100644 --- a/transpiler/src/main/java/org/jsweet/transpiler/util/ProcessUtil.java +++ b/transpiler/src/main/java/org/jsweet/transpiler/util/ProcessUtil.java @@ -25,10 +25,7 @@ import java.nio.file.FileVisitOption; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.function.Consumer; import java.util.stream.Stream; @@ -123,14 +120,13 @@ public class ProcessUtil { return unixPath; } - logger.info("cannot find " + command + " - searching in node_modules files"); - unixPath = lookupGlobalNpmPackageExecutablePath(NPM_DIR, command); - - return unixPath; + logger.info("cannot find " + command + " - searching in " + NPM_DIR.getAbsolutePath()); + return lookupGlobalNpmPackageExecutablePath(NPM_DIR, command) + .orElseThrow(() -> new RuntimeException("Could not locate command " + command)); } } - private static String lookupGlobalNpmPackageExecutablePath(File directory, String executableName) { + private static Optional lookupGlobalNpmPackageExecutablePath(File directory, String executableName) { if (directory != null) { File[] children = directory.listFiles(); if (children != null) { @@ -139,12 +135,12 @@ public class ProcessUtil { if (child.getName().equals("bin")) { for (File binFile : child.listFiles()) { if (binFile.getName().equals(executableName)) { - return binFile.getAbsolutePath(); + return Optional.of(binFile.getAbsolutePath()); } } } else { - String foundPath = lookupGlobalNpmPackageExecutablePath(child, executableName); - if (foundPath != null) { + Optional foundPath = lookupGlobalNpmPackageExecutablePath(child, executableName); + if (foundPath.isPresent()) { return foundPath; } } @@ -152,7 +148,7 @@ public class ProcessUtil { } } } - return null; + return Optional.empty(); } /**