"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.findNodeModules = exports.readdir = exports.generateRandomString = exports.writeFile = exports.loadTemplate = exports.fromUnicode = void 0;
const fs_1 = __importDefault(require("fs"));
const handlebars_1 = __importDefault(require("handlebars"));
const profile_1 = __importDefault(require("./profiles/profile"));
const fromUnicode = (str) => JSON.parse('"' + str.replace(/\"/g, '\\"') + '"');
exports.fromUnicode = fromUnicode;
const loadTemplate = (templatePath) => {
    const template = fs_1.default.readFileSync(templatePath, 'utf8');
    const fn = handlebars_1.default.compile(template);
    return (data) => fn({ profile: profile_1.default, ...data });
};
exports.loadTemplate = loadTemplate;
const writeFile = (path, content) => {
    const parent = path.substring(0, path.lastIndexOf('/'));
    if (!fs_1.default.existsSync(parent)) {
        fs_1.default.mkdirSync(parent, { recursive: true });
    }
    fs_1.default.writeFileSync(path, content);
};
exports.writeFile = writeFile;
const generateRandomString = (length = 10) => Math.random().toString(20).substr(2, length);
exports.generateRandomString = generateRandomString;
const readdir = (path, opts = { recursive: false }, callback = (f) => ({})) => {
    fs_1.default.readdirSync(path).forEach(name => {
        const child = `${path}/${name}`;
        if (fs_1.default.statSync(child).isDirectory() && opts.recursive) {
            (0, exports.readdir)(child, opts, callback);
        }
        else {
            callback(child);
        }
    });
};
exports.readdir = readdir;
const findNodeModules = (path) => {
    if (fs_1.default.existsSync(`${path}/node_modules`)) {
        return `${path}/node_modules`;
    }
    else {
        const splits = path.split('/');
        splits.pop();
        if (splits.length) {
            return (0, exports.findNodeModules)(splits.join('/'));
        }
    }
    return '';
};
exports.findNodeModules = findNodeModules;
//# sourceMappingURL=utils.js.map