"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.setExtension = exports.removeExtensionPosix = exports.removeExtension = exports.ensurePosix = void 0;
exports.fileExists = fileExists;
exports.isMarkdownFileExt = isMarkdownFileExt;
exports.copySyncWithOptions = copySyncWithOptions;
const path_1 = __importDefault(require("path"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const ensure_posix_path_1 = __importDefault(require("ensure-posix-path"));
exports.ensurePosix = ensure_posix_path_1.default;
const markdownFileExts = '.md';
function fileExists(filePath) {
    try {
        // use decodeURIComponent to deal with space (%20) in file path, e.g
        // from docs\images\dev%20diagrams\architecture.png
        // to docs\images\dev diagrams\architecture.png
        return fs_extra_1.default.statSync(decodeURIComponent(filePath)).isFile();
    }
    catch (err) {
        return false;
    }
}
function isMarkdownFileExt(ext) {
    return markdownFileExts === ext;
}
const removeExtension = (filePathWithExt) => path_1.default.join(path_1.default.dirname(filePathWithExt), path_1.default.basename(filePathWithExt, path_1.default.extname(filePathWithExt)));
exports.removeExtension = removeExtension;
const removeExtensionPosix = (filePathWithExt) => (0, ensure_posix_path_1.default)(path_1.default.join(path_1.default.dirname(filePathWithExt), path_1.default.basename(filePathWithExt, path_1.default.extname(filePathWithExt))));
exports.removeExtensionPosix = removeExtensionPosix;
const setExtension = (normalizedFilename, ext) => ((0, exports.removeExtension)(normalizedFilename) + ext);
exports.setExtension = setExtension;
function copySyncWithOptions(src, dest, options) {
    const files = fs_extra_1.default.readdirSync(src);
    files.forEach((file) => {
        const curSource = path_1.default.join(src, file);
        let curDest = path_1.default.join(dest, file);
        if (file === 'gitignore') {
            curDest = path_1.default.join(dest, '.gitignore');
        }
        if (fs_extra_1.default.lstatSync(curSource).isDirectory()) {
            if (!fs_extra_1.default.existsSync(curDest)) {
                fs_extra_1.default.mkdirSync(curDest);
            }
            copySyncWithOptions(curSource, curDest, options);
        }
        else {
            if (options.overwrite === false && fs_extra_1.default.existsSync(curDest)) {
                return;
            }
            fs_extra_1.default.copySync(curSource, curDest);
        }
    });
}
//# sourceMappingURL=fsUtil.js.map