Add support for earlier versions of Node (no recursive mkdir)
This commit is contained in:
parent
e2f6d8f68a
commit
c4dfba317e
1 changed files with 13 additions and 2 deletions
15
index.js
15
index.js
|
@ -7,6 +7,17 @@ const util = require('util');
|
||||||
|
|
||||||
const COMPRESSION_LEVEL = 5;
|
const COMPRESSION_LEVEL = 5;
|
||||||
|
|
||||||
|
function mkdirSyncRecursive(dir) {
|
||||||
|
let path = dir.replace(/\/$/, '').split('/');
|
||||||
|
|
||||||
|
for (var i = 1; i <= path.length; i++) {
|
||||||
|
let segment = path.slice(0, i).join('/');
|
||||||
|
if (segment.length > 0 && !fs.existsSync(segment)) {
|
||||||
|
fs.mkdirSync(segment)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// async readdir
|
// async readdir
|
||||||
const readdir = async (path, options) => {
|
const readdir = async (path, options) => {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
|
@ -154,7 +165,7 @@ function streamToBuffer(stream) {
|
||||||
async function unpackDirectory(directory, options = {}) {
|
async function unpackDirectory(directory, options = {}) {
|
||||||
return new Promise(async (resolve) => {
|
return new Promise(async (resolve) => {
|
||||||
if(!fs.existsSync(directory)) {
|
if(!fs.existsSync(directory)) {
|
||||||
fs.mkdirSync(directory);
|
mkdirSyncRecursive(directory);
|
||||||
}
|
}
|
||||||
|
|
||||||
const fileReadStream = getFileReadStream(options);
|
const fileReadStream = getFileReadStream(options);
|
||||||
|
@ -193,7 +204,7 @@ async function unpackDirectory(directory, options = {}) {
|
||||||
const writePath = path.join(directory, header.name);
|
const writePath = path.join(directory, header.name);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
fs.mkdirSync(path.dirname(writePath), { recursive: true });
|
mkdirSyncRecursive(path.dirname(writePath));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Directory exists
|
// Directory exists
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue