Code cleanup, fix race condition, return promise on unpack
This commit is contained in:
parent
05b855a40a
commit
59cc470e1e
1 changed files with 41 additions and 34 deletions
21
index.js
21
index.js
|
@ -126,7 +126,7 @@ async function packDirectory(directory, options = {}) {
|
|||
});
|
||||
|
||||
await writeStream(entry, contents);
|
||||
//console.log(contents)
|
||||
|
||||
entry.end();
|
||||
}catch (e){console.log(e)}
|
||||
}, directory, packRoot);
|
||||
|
@ -154,6 +154,7 @@ function streamToBuffer(stream) {
|
|||
|
||||
|
||||
async function unpackDirectory(directory, options = {}) {
|
||||
return new Promise(async (resolve) => {
|
||||
if(!fs.existsSync(directory)) {
|
||||
fs.mkdirSync(directory);
|
||||
}
|
||||
|
@ -170,12 +171,17 @@ async function unpackDirectory(directory, options = {}) {
|
|||
contents = zstd.decompress(contents);
|
||||
|
||||
if(!/^\./.test(header.name)) {
|
||||
if(header.name == 'index.html') {
|
||||
console.log(String.fromCharCode.apply(null, contents))
|
||||
}
|
||||
const writePath = path.join(directory, header.name);
|
||||
fs.promises.mkdir(path.dirname(writePath), { recursive: true });
|
||||
var fileWriteStream = fs.createWriteStream(writePath);
|
||||
|
||||
var fileWriteStream;
|
||||
try {
|
||||
fileWriteStream = fs.createWriteStream(writePath);
|
||||
} catch (e) {
|
||||
// Assume directory may not exist if an error is thrown
|
||||
fs.mkdirSync(path.dirname(writePath), { recursive: true });
|
||||
fileWriteStream = fs.createWriteStream(writePath);
|
||||
}
|
||||
|
||||
fileWriteStream.write(contents);
|
||||
fileWriteStream.end();
|
||||
next();
|
||||
|
@ -186,10 +192,11 @@ async function unpackDirectory(directory, options = {}) {
|
|||
});
|
||||
|
||||
extract.on('finish', () => {
|
||||
// all entries read
|
||||
resolve(true);
|
||||
});
|
||||
|
||||
fileReadStream.pipe(extract);
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
Loading…
Reference in a new issue