Enable packages containing instructions to append files

This commit is contained in:
Lloyd McKenzie 2019-11-20 23:27:01 +01:00
parent d98c496712
commit 57b6027431
2 changed files with 26 additions and 2 deletions

View File

@ -61,6 +61,9 @@ import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -195,7 +198,12 @@ public class TextFile {
sw.write(bytes); sw.write(bytes);
sw.flush(); sw.flush();
sw.close(); sw.close();
}
public static void appendBytesToFile(byte[] bytes, String path) throws IOException {
byte[] linebreak = new byte[] {13, 10};
Files.write(Paths.get(path), linebreak, StandardOpenOption.APPEND);
Files.write(Paths.get(path), bytes, StandardOpenOption.APPEND);
} }
public static byte[] fileToBytes(String srcFile) throws FileNotFoundException, IOException { public static byte[] fileToBytes(String srcFile) throws FileNotFoundException, IOException {

View File

@ -498,10 +498,26 @@ public class NpmPackage {
} }
public void unPack(String dir) throws IOException { public void unPack(String dir) throws IOException {
unPack (dir, false);
}
public void unPackWithAppend(String dir) throws IOException {
unPack (dir, true);
}
public void unPack(String dir, boolean withAppend) throws IOException {
for (String s : content.keySet()) { for (String s : content.keySet()) {
String fn = Utilities.path(dir, s); String fn = Utilities.path(dir, s);
String dn = Utilities.getDirectoryForFile(fn); String dn = Utilities.getDirectoryForFile(fn);
Utilities.createDirectory(dn); Utilities.createDirectory(dn);
File f = new File(s);
if (withAppend && f.getName().startsWith("_append.")) {
String appendFn = Utilities.path(dir, Utilities.getDirectoryForFile(s), f.getName().substring(8));
if (new File(appendFn).exists())
TextFile.appendBytesToFile(content.get(s), appendFn);
else
TextFile.bytesToFile(content.get(s), appendFn);
} else
TextFile.bytesToFile(content.get(s), fn); TextFile.bytesToFile(content.get(s), fn);
} }
if (path != null) if (path != null)