fix NpmPackage to handle nested diretories and files in the root directory

This commit is contained in:
Grahame Grieve 2019-12-05 11:44:02 +11:00
parent d94e0d402d
commit 364e47beec

View File

@ -185,7 +185,11 @@ public class NpmPackage {
for (File f : new File(path).listFiles()) { for (File f : new File(path).listFiles()) {
if (f.isDirectory()) { if (f.isDirectory()) {
NpmPackageFolder folder = res.new NpmPackageFolder(f.getName()); String d = f.getName();
if (!d.equals("package")) {
d = Utilities.path("package", d);
}
NpmPackageFolder folder = res.new NpmPackageFolder(d);
folder.folder = f; folder.folder = f;
res.folders.put(f.getName(), folder); res.folders.put(f.getName(), folder);
File ij = new File(Utilities.path(f.getAbsolutePath(), ".index.json")); File ij = new File(Utilities.path(f.getAbsolutePath(), ".index.json"));
@ -196,13 +200,22 @@ public class NpmPackage {
throw new IOException("Error parsing "+ij.getAbsolutePath()+": "+e.getMessage(), e); throw new IOException("Error parsing "+ij.getAbsolutePath()+": "+e.getMessage(), e);
} }
} }
loadSubFolders(res, path, f);
} }
} }
for (File f : new File(Utilities.path(path, "package")).listFiles()) { }
private static void loadSubFolders(NpmPackage res, String rootPath, File dir) throws IOException {
for (File f : dir.listFiles()) {
if (f.isDirectory()) { if (f.isDirectory()) {
NpmPackageFolder folder = res.new NpmPackageFolder(f.getName()); String d = f.getAbsolutePath().substring(rootPath.length()+1);
if (!d.startsWith("package")) {
d = Utilities.path("package", d);
}
NpmPackageFolder folder = res.new NpmPackageFolder(d);
folder.folder = f; folder.folder = f;
res.folders.put(f.getName(), folder); res.folders.put(d, folder);
File ij = new File(Utilities.path(f.getAbsolutePath(), ".index.json")); File ij = new File(Utilities.path(f.getAbsolutePath(), ".index.json"));
if (ij.exists()) { if (ij.exists()) {
try { try {
@ -211,8 +224,10 @@ public class NpmPackage {
throw new IOException("Error parsing "+ij.getAbsolutePath()+": "+e.getMessage(), e); throw new IOException("Error parsing "+ij.getAbsolutePath()+": "+e.getMessage(), e);
} }
} }
loadSubFolders(res, rootPath, f);
} }
} }
} }
public static NpmPackage fromFolder(String folder, PackageType defType, String... exemptions) throws IOException { public static NpmPackage fromFolder(String folder, PackageType defType, String... exemptions) throws IOException {
@ -259,7 +274,7 @@ public class NpmPackage {
dir = dir.substring(8); dir = dir.substring(8);
} }
folders.put(dir, new NpmPackageFolder(dir)); folders.put(dir, new NpmPackageFolder(dir));
} else if (n.contains("/")) { } else {
int count; int count;
byte data[] = new byte[BUFFER_SIZE]; byte data[] = new byte[BUFFER_SIZE];
ByteArrayOutputStream fos = new ByteArrayOutputStream(); ByteArrayOutputStream fos = new ByteArrayOutputStream();
@ -291,7 +306,7 @@ public class NpmPackage {
} }
public void loadFile(String n, byte[] data) throws IOException { public void loadFile(String n, byte[] data) throws IOException {
String dir = n.substring(0, n.lastIndexOf("/")); String dir = n.contains("/") ? n.substring(0, n.lastIndexOf("/")) : "$root";
if (dir.startsWith("package/")) { if (dir.startsWith("package/")) {
dir = dir.substring(8); dir = dir.substring(8);
} }
@ -371,6 +386,8 @@ public class NpmPackage {
List<String> res = new ArrayList<String>(); List<String> res = new ArrayList<String>();
if (folders.containsKey(folder)) { if (folders.containsKey(folder)) {
res.addAll(folders.get(folder).listFiles()); res.addAll(folders.get(folder).listFiles());
} else if (folders.containsKey(Utilities.path("package", folder))) {
res.addAll(folders.get(Utilities.path("package", folder)).listFiles());
} }
return res; return res;
} }
@ -714,22 +731,30 @@ public class NpmPackage {
public void unPack(String dir, boolean withAppend) throws IOException { public void unPack(String dir, boolean withAppend) throws IOException {
for (NpmPackageFolder folder : folders.values()) { for (NpmPackageFolder folder : folders.values()) {
for (String s : folder.content.keySet()) { String dn = folder.getName();
String fn = Utilities.path(dir, folder.name, s); if (!dn.equals("package")) {
String dn = Utilities.getDirectoryForFile(fn); dn = dn.substring(8);
Utilities.createDirectory(dn); }
File f = new File(s); if (dn.equals("$root")) {
dn = dir;
} else {
dn = Utilities.path(dir, dn);
}
Utilities.createDirectory(dn);
for (String s : folder.listFiles()) {
String fn = Utilities.path(dn, s);
File f = new File(fn);
if (withAppend && f.getName().startsWith("_append.")) { if (withAppend && f.getName().startsWith("_append.")) {
String appendFn = Utilities.path(dir, Utilities.getDirectoryForFile(s), f.getName().substring(8)); String appendFn = Utilities.path(dir, Utilities.getDirectoryForFile(s), f.getName().substring(8));
if (new File(appendFn).exists()) if (new File(appendFn).exists())
TextFile.appendBytesToFile(folder.content.get(s), appendFn); TextFile.appendBytesToFile(folder.fetchFile(s), appendFn);
else else
TextFile.bytesToFile(folder.content.get(s), appendFn); TextFile.bytesToFile(folder.fetchFile(s), appendFn);
} else } else
TextFile.bytesToFile(folder.content.get(s), fn); TextFile.bytesToFile(folder.fetchFile(s), fn);
} }
if (path != null) // if (path != null)
FileUtils.copyDirectory(new File(path), new File(dir)); // FileUtils.copyDirectory(new File(path), new File(dir));
} }
} }
} }