"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Fs = require("fs");
const Path = require("path");
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 lastName(path) {
        let lastName = path.split('/').slice(-1).pop();
        if (lastName === '') {
            lastName = path.split('/').slice(-2, -1).pop();
        }
        return lastName;
    }
    FS.lastName = lastName;
})(FS = exports.FS || (exports.FS = {}));
//# sourceMappingURL=fs.js.map