"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
/**
 * Copyright 2017-2020 Plexus Interop Deutsche Bank AG
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
var fs = require("fs-extra");
var path = require("path");
var AdmZip = require("adm-zip");
function getDistDir() {
    return path.resolve(getBaseDir(), 'dist');
}
exports.getDistDir = getDistDir;
function getBaseDir() {
    return path.join(__dirname, '..', '..', '..', '..');
}
exports.getBaseDir = getBaseDir;
function getDirectories(dirPath) {
    return fs.readdirSync(dirPath).filter(function (file) { return fs.statSync(path.join(dirPath, file)).isDirectory(); });
}
exports.getDirectories = getDirectories;
function removeSync(file) {
    fs.removeSync(file);
}
exports.removeSync = removeSync;
function mkdirsSync(dir) {
    fs.mkdirsSync(dir);
}
exports.mkdirsSync = mkdirsSync;
function existsSync(path) {
    return fs.existsSync(path);
}
exports.existsSync = existsSync;
function exists(path) {
    return new Promise(function (resolve, reject) {
        fs.exists(path, function (exists) {
            resolve(exists);
        });
    });
}
exports.exists = exists;
function listFiles(baseDir, pattern) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var result;
        return tslib_1.__generator(this, function (_a) {
            result = [];
            iterateFiles(baseDir, pattern, function (f) { return result.push(f); });
            return [2 /*return*/, result];
        });
    });
}
exports.listFiles = listFiles;
function readTextFile(filePath) {
    return fs.readFile(filePath, 'utf8');
}
exports.readTextFile = readTextFile;
function iterateFiles(baseDir, pattern, callback, recursive) {
    if (recursive === void 0) { recursive = true; }
    if (!fs.existsSync(baseDir)) {
        return;
    }
    var files = fs.readdirSync(baseDir);
    files.forEach(function (f) {
        var fileName = path.join(baseDir, f);
        if (isDirectory(fileName, false)) {
            iterateFiles(fileName, pattern, callback);
        }
        else if (pattern.test(fileName)) {
            callback(fileName);
        }
    });
}
exports.iterateFiles = iterateFiles;
function unzipSync(zipPath, dir, removeZip) {
    if (removeZip === void 0) { removeZip = true; }
    console.log("Extracting " + zipPath + " to " + dir);
    var zipArchive = new AdmZip(zipPath);
    zipArchive.extractAllTo(dir, true);
    console.log("Extracted zip to " + dir);
    if (removeZip) {
        console.log("Clearing zip " + zipPath);
        removeSync(zipPath);
    }
}
exports.unzipSync = unzipSync;
function isDirectory(path, failOnPermissonError) {
    if (failOnPermissonError === void 0) { failOnPermissonError = true; }
    try {
        return fs.lstatSync(path).isDirectory();
    }
    catch (error) {
        if (!failOnPermissonError && error.code === 'EPERM') {
            return false;
        }
        else {
            throw error;
        }
    }
}
function copyFile(source, target) {
    return fs.copy(source, target);
}
exports.copyFile = copyFile;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZmlsZXMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi9zcmMvY29tbW9uL2ZpbGVzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7OztBQUFBOzs7Ozs7Ozs7Ozs7Ozs7R0FlRztBQUNILDZCQUErQjtBQUMvQiwyQkFBNkI7QUFDN0IsZ0NBQWtDO0FBRWxDLFNBQWdCLFVBQVU7SUFDdEIsT0FBTyxJQUFJLENBQUMsT0FBTyxDQUFDLFVBQVUsRUFBRSxFQUFFLE1BQU0sQ0FBQyxDQUFDO0FBQzlDLENBQUM7QUFGRCxnQ0FFQztBQUVELFNBQWdCLFVBQVU7SUFDdEIsT0FBTyxJQUFJLENBQUMsSUFBSSxDQUFDLFNBQVMsRUFBRSxJQUFJLEVBQUUsSUFBSSxFQUFFLElBQUksRUFBRSxJQUFJLENBQUMsQ0FBQztBQUN4RCxDQUFDO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