From 42aea6c4b62adcac83f944214b32a28017b43662 Mon Sep 17 00:00:00 2001 From: Devin Smith Date: Mon, 27 Nov 2023 15:27:51 -0800 Subject: [PATCH] Exclude always ignored package.json files --- .../io/deephaven/server/jetty/CopyHelper.java | 18 +++++++++++++++--- .../server/jetty/JsPluginsZipFilesystem.java | 19 ++++++++++++++++++- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/server/jetty/src/main/java/io/deephaven/server/jetty/CopyHelper.java b/server/jetty/src/main/java/io/deephaven/server/jetty/CopyHelper.java index 8b13de748a6..3637be43727 100644 --- a/server/jetty/src/main/java/io/deephaven/server/jetty/CopyHelper.java +++ b/server/jetty/src/main/java/io/deephaven/server/jetty/CopyHelper.java @@ -11,24 +11,33 @@ import java.nio.file.StandardCopyOption; import java.nio.file.attribute.BasicFileAttributes; import java.util.Objects; +import java.util.function.Predicate; class CopyHelper { - static void copyRecursive(Path src, Path dst) throws IOException { + static void copyRecursive(Path src, Path dst, Predicate includeDir, Predicate includeFile) + throws IOException { Files.createDirectories(dst.getParent()); - Files.walkFileTree(src, new CopyRecursiveVisitor(src, dst)); + Files.walkFileTree(src, new CopyRecursiveVisitor(src, dst, includeDir, includeFile)); } private static class CopyRecursiveVisitor extends SimpleFileVisitor { private final Path src; private final Path dst; + private final Predicate includeDir; + private final Predicate includeFile; - public CopyRecursiveVisitor(Path src, Path dst) { + public CopyRecursiveVisitor(Path src, Path dst, Predicate includeDir, Predicate includeFile) { this.src = Objects.requireNonNull(src); this.dst = Objects.requireNonNull(dst); + this.includeDir = Objects.requireNonNull(includeDir); + this.includeFile = Objects.requireNonNull(includeFile); } @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { + if (!includeDir.test(dir)) { + return FileVisitResult.SKIP_SUBTREE; + } // Note: toString() necessary for src/dst that don't share the same root FS Files.copy(dir, dst.resolve(src.relativize(dir).toString()), StandardCopyOption.COPY_ATTRIBUTES); return FileVisitResult.CONTINUE; @@ -36,6 +45,9 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) th @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + if (!includeFile.test(file)) { + return FileVisitResult.CONTINUE; + } // Note: toString() necessary for src/dst that don't share the same root FS Files.copy(file, dst.resolve(src.relativize(file).toString()), StandardCopyOption.COPY_ATTRIBUTES); return FileVisitResult.CONTINUE; diff --git a/server/jetty/src/main/java/io/deephaven/server/jetty/JsPluginsZipFilesystem.java b/server/jetty/src/main/java/io/deephaven/server/jetty/JsPluginsZipFilesystem.java index 554ac81098b..97d62204d6c 100644 --- a/server/jetty/src/main/java/io/deephaven/server/jetty/JsPluginsZipFilesystem.java +++ b/server/jetty/src/main/java/io/deephaven/server/jetty/JsPluginsZipFilesystem.java @@ -19,11 +19,15 @@ import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Set; +import java.util.function.Predicate; import static io.deephaven.server.jetty.Json.OBJECT_MAPPER; class JsPluginsZipFilesystem { private static final String ZIP_ROOT = "/"; + private static final Set PACKAGE_JSON_IGNORED_NAMES = Set.of(".git", "CVS", ".svn", ".hg", ".lock-wscript", + ".wafpickle-N", ".DS_Store", "npm-debug.log", ".npmrc", "node_modules", "config.gypi", "package-lock.json"); /** * Creates a new js plugins instance with a temporary zip filesystem. @@ -69,7 +73,20 @@ public synchronized void copyFrom(JsPluginPackagePath srcPackagePath, JsPluginMa } private static void copyRecursive(JsPluginPackagePath src, JsPluginPackagePath dst) throws IOException { - CopyHelper.copyRecursive(src.path(), dst.path()); + CopyHelper.copyRecursive(src.path(), dst.path(), + Predicate.not(JsPluginsZipFilesystem::isPackageJsonIgnored), + Predicate.not(JsPluginsZipFilesystem::isPackageJsonIgnored)); + } + + + /** + * @see package-json#files + * @param path the path + * @return if it is ignored + */ + private static boolean isPackageJsonIgnored(Path path) { + final String fileName = path.getFileName().toString(); + return PACKAGE_JSON_IGNORED_NAMES.contains(fileName) || fileName.contains("*"); } private void checkExisting(JsPluginManifestEntry info) {