Skip to content

Commit

Permalink
Exclude always ignored package.json files
Browse files Browse the repository at this point in the history
  • Loading branch information
devinrsmith committed Nov 27, 2023
1 parent de0e134 commit 42aea6c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,43 @@
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<Path> includeDir, Predicate<Path> 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<Path> {
private final Path src;
private final Path dst;
private final Predicate<Path> includeDir;
private final Predicate<Path> includeFile;

public CopyRecursiveVisitor(Path src, Path dst) {
public CopyRecursiveVisitor(Path src, Path dst, Predicate<Path> includeDir, Predicate<Path> 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;
}

@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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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.
Expand Down Expand Up @@ -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 <a href="https://docs.npmjs.com/cli/v6/configuring-npm/package-json#files">package-json#files</a>
* @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) {
Expand Down

0 comments on commit 42aea6c

Please sign in to comment.