"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const Fs = __importStar(require("fs"));
const Path = __importStar(require("path"));
// import * as Archiver from 'archiver';
const Archiver = require('archiver');
var FS;
(function (FS) {
    //  遍历文件夹内所有文件
    function walk(path, process, error, completion) {
        process = process ? process : () => { };
        error = error ? error : () => { };
        completion = completion ? completion : () => { };
        if (Fs.existsSync(path)) {
            const stats = Fs.statSync(path);
            if (stats.isDirectory()) {
                const subpaths = Fs.readdirSync(path);
                for (const subpath of subpaths) {
                    walk(Path.join(path, subpath), process, error);
                }
            }
            else {
                process(Path.dirname(path), Path.basename(path), stats);
            }
        }
        else {
            error(Error(`文件不存在 [${path}]`));
        }
        completion();
    }
    FS.walk = walk;
    //  拷贝文件或完整目录
    function copy(src, dist) {
        const stat = Fs.statSync(src);
        if (stat.isDirectory()) {
            if (!Fs.existsSync(dist)) {
                mkdirs(dist);
            }
            const subs = Fs.readdirSync(src);
            for (const asub of subs) {
                const substat = Fs.statSync(Path.join(src, asub));
                if (substat.isFile()) {
                    Fs.copyFileSync(Path.join(src, asub), Path.join(dist, asub));
                }
                else if (substat.isDirectory()) {
                    copy(Path.join(src, asub), Path.join(dist, asub));
                }
            }
        }
        else if (stat.isFile()) {
            if (!Fs.existsSync(Path.dirname(dist))) {
                mkdirs(Path.dirname(dist));
            }
            Fs.copyFileSync(src, dist);
        }
    }
    FS.copy = copy;
    //  创建多级目录
    function mkdirs(path) {
        if (Fs.existsSync(path)) {
            return true;
        }
        else {
            if (mkdirs(Path.dirname(path))) {
                Fs.mkdirSync(path);
                return true;
            }
        }
    }
    FS.mkdirs = mkdirs;
    //  清空并删除文件夹
    function rmdirs(path) {
        if (Fs.existsSync(path)) {
            if (Fs.statSync(path).isDirectory()) {
                const subs = Fs.readdirSync(path);
                for (const asub of subs) {
                    rmdirs(Path.join(path, asub));
                }
                Fs.rmdirSync(path);
            }
            else {
                Fs.unlinkSync(path);
            }
        }
    }
    FS.rmdirs = rmdirs;
    //  文件夹压缩
    function zip(srcpath, distpath) {
        return __awaiter(this, void 0, void 0, function* () {
            if (Fs.statSync(srcpath).isDirectory()) {
                const _zip = Archiver('zip', { zlib: { level: 9 } });
                const _output = Fs.createWriteStream(distpath);
                return new Promise((resolve, reject) => {
                    _zip.directory(srcpath, false).on('error', (err) => reject(err)).pipe(_output);
                    _output.on('close', () => resolve());
                    _zip.finalize();
                });
            }
            else {
                return Promise.reject(Error(`暂时只支持压缩文件夹 [${srcpath}]`));
            }
        });
    }
    FS.zip = zip;
})(FS = exports.FS || (exports.FS = {}));
//# sourceMappingURL=fs.js.map