"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.copyFile = exports.unzipSync = exports.iterateFiles = exports.readTextFile = exports.listFiles = exports.exists = exports.existsSync = exports.mkdirsSync = exports.removeSync = exports.getDirectories = exports.getBaseDir = exports.getDistDir = void 0;
var tslib_1 = require("tslib");
/**
 * Copyright 2017-2022 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 AdmZip = require("adm-zip");
var fs = require("fs-extra");
var path = require("path");
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(testPath) {
    return fs.existsSync(testPath);
}
exports.existsSync = existsSync;
function exists(testPath) {
    return new Promise(function (resolve) {
        fs.exists(testPath, function (doesExist) {
            resolve(doesExist);
        });
    });
}
exports.exists = exists;
function listFiles(baseDir, pattern) {
    return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
        var result;
        return (0, 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) {
    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(testPath, failOnPermissonError) {
    if (failOnPermissonError === void 0) { failOnPermissonError = true; }
    try {
        return fs.lstatSync(testPath).isDirectory();
    }
    catch (error) {
        if (!failOnPermissonError && error.code === 'EPERM') {
            return false;
        }
        throw error;
    }
}
function copyFile(source, target) {
    return fs.copy(source, target);
}
exports.copyFile = copyFile;
//# sourceMappingURL=files.js.map