"use strict";
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 OS = __importStar(require("os"));
const NET = __importStar(require("net"));
const Path = __importStar(require("path"));
const FS = __importStar(require("fs"));
const Rimraf = __importStar(require("rimraf"));
const ChildProcess = __importStar(require("child_process"));
const console_1 = require("./console");
var ACUtils;
(function (ACUtils) {
    ACUtils.respath = Path.join(Path.resolve(__dirname, '../../../'), 'res');
    function mkdirs(path) {
        if (FS.existsSync(path)) {
            return true;
        }
        else {
            if (ACUtils.mkdirs(Path.dirname(path))) {
                FS.mkdirSync(path);
                return true;
            }
        }
        return false;
    }
    ACUtils.mkdirs = mkdirs;
    function rmdirs(path) {
        if (FS.existsSync(path)) {
            Rimraf.sync(path);
        }
    }
    ACUtils.rmdirs = rmdirs;
    function dash2hump(text) {
        var arr = text.split('-');
        for (var i = 1; i < arr.length; i++) {
            arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].substring(1);
        }
        return arr.join('');
    }
    ACUtils.dash2hump = dash2hump;
    function dash2hump4style(style) {
        return style;
    }
    ACUtils.dash2hump4style = dash2hump4style;
    function convert2path(text) {
        let result = text;
        result = result.split('.').join('/');
        result = result.split('[').join('/');
        result = result.split(']').join('');
        result = result.trim();
        if (result.lastIndexOf('/') === result.length - 1) {
            result = result.substr(0, result.length - 1);
        }
        result = `/${result}`;
        return result;
    }
    ACUtils.convert2path = convert2path;
    function pickFuncParams(text) {
        const matched = text.match(/\(([\s\S]*)\)/);
        return matched ? matched[1] : '';
    }
    ACUtils.pickFuncParams = pickFuncParams;
    function localPort() {
        return new Promise((resolve, rejects) => {
            const test = NET.createServer();
            test.listen(0);
            test.on('listening', () => {
                const port = test.address().port;
                test.close(() => {
                    resolve(port);
                });
            });
        });
    }
    ACUtils.localPort = localPort;
    function localIP() {
        const ifaces = OS.networkInterfaces();
        const ipv4s = new Array();
        Object.keys(ifaces).forEach(function (ifname) {
            ifaces[ifname].forEach(function (iface) {
                if ('IPv4' !== iface.family || iface.internal !== false) {
                    // skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses
                }
                else {
                    ipv4s.push({ name: ifname, info: iface });
                }
            });
        });
        let ip = '';
        ipv4s.forEach(element => {
            if ('en0' === element.name) {
                ip = element.info.address;
            }
        });
        return ip;
    }
    ACUtils.localIP = localIP;
    function pickProjectPackage(path) {
        return JSON.parse(FS.readFileSync(Path.join(path, 'package.json')).toString());
    }
    ACUtils.pickProjectPackage = pickProjectPackage;
    function substring(text, startLine, startColumn, endLine, endColumn) {
        if (text) {
            startLine -= 1;
            endLine -= 1;
            const lines = text.split('\n');
            let result = lines.slice(startLine, endLine > startLine ? endLine : startLine + 1).join('\n');
            const endLength = lines[endLine].length - endColumn;
            result = result.substring(startColumn, result.length - endLength);
            return result;
        }
        return text;
    }
    ACUtils.substring = substring;
    function spawn(command, params) {
        return Promise.resolve(ChildProcess.spawnSync(command, params));
    }
    ACUtils.spawn = spawn;
    function spawnSync(cmd, args) {
        console_1.Console.info(`执行指令 : ${cmd} ${args ? args : ''}`);
        if (!args) {
            ChildProcess.execSync(cmd);
        }
        else {
            const result = ChildProcess.spawnSync(cmd, args);
            if (result.error) {
                console_1.Console.error(`  ${result.error.toString()}`);
            }
            else {
                if (result.stdout.toString().length > 0) {
                    console_1.Console.info(`  ${result.stdout.toString()}`);
                }
            }
        }
    }
    ACUtils.spawnSync = spawnSync;
})(ACUtils = exports.ACUtils || (exports.ACUtils = {}));
function uuid() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}
exports.uuid = uuid;
function now() {
    return (new Date()).toLocaleString('zh', { hour12: false }).split(',').join('');
}
exports.now = now;
//# sourceMappingURL=utils.js.map