"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.unZipBuffer = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const yauzl_1 = require("yauzl");
function unZipBuffer(buffer, projectName) {
    return new Promise((resolve, reject) => {
        (0, yauzl_1.fromBuffer)(buffer, { lazyEntries: true }, handleZipFile(projectName, resolve, reject));
    });
}
exports.unZipBuffer = unZipBuffer;
function handleZipFile(projectName, resolve, reject) {
    return (err, zipfile) => {
        if (err) {
            throw err;
        }
        // track when we've closed all our file handles
        zipfile.readEntry();
        zipfile.on('entry', (entry) => {
            const segments = entry.fileName.split('/');
            segments[0] = projectName;
            const fileName = segments.join(path.sep);
            if (fileName[fileName.length - 1] === path.sep) {
                // Directory file names end with '/'.
                // Note that entires for directories themselves are optional.
                // An entry's fileName implicitly requires its parent directories to exist.
                zipfile.readEntry();
            }
            else {
                // ensure parent directory exists
                mkdirp(path.dirname(fileName), () => {
                    zipfile.openReadStream(entry, (errL, readStream) => {
                        if (errL) {
                            throw errL;
                        }
                        readStream.on('end', () => {
                            zipfile.readEntry();
                        });
                        // pump file contents
                        readStream.pipe(fs.createWriteStream(fileName));
                    });
                });
            }
        });
        zipfile.once('error', reject);
        zipfile.once('end', () => {
            resolve();
        });
    };
}
function mkdirp(dir, cb) {
    if (dir === '.') {
        return cb();
    }
    fs.stat(dir, err => {
        if (err == null) {
            return cb(); // already exists
        }
        const parent = path.dirname(dir);
        mkdirp(parent, () => {
            fs.mkdir(dir, cb);
        });
    });
}
//# sourceMappingURL=unzip.js.map