#!/usr/bin/env node
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const klaw_1 = __importDefault(require("klaw"));
const to_1 = require("./to");
const debug_1 = __importDefault(require("debug"));
const ext_1 = require("./ext");
const config_1 = __importDefault(require("./config"));
const commander_1 = __importDefault(require("commander"));
const prettier_1 = __importDefault(require("prettier"));
const request_1 = require("./request");
const fs_extra_1 = require("fs-extra");
const log = debug_1.default('j2t:cli');
commander_1.default
    .version('0.0.1')
    .usage('-c dubbo.json')
    .option('-c, --config [value]', 'specify interpret Config ')
    .parse(process.argv);
(async () => {
    const { res: dubboConfig, err: configErr } = await config_1.default.fromConfigPath(commander_1.default.config);
    if (configErr) {
        console.error('Error reading configuration file');
        console.log(configErr);
        log(configErr);
        return;
    }
    const { res: extInfo, err: extError } = await to_1.to(ext_1.extra(dubboConfig));
    if (extError) {
        console.error('Failed to extract ast from java class');
        console.log(extError);
        log(extError);
        return;
    }
    //setup jar ast path
    console.log('read jar ast file', extInfo.jarInfo);
    dubboConfig.jarInfo = extInfo.jarInfo;
    log(`parse config->${JSON.stringify(dubboConfig, null, 2)}`);
    await new request_1.Request(dubboConfig).work();
    await formatSourceDir(dubboConfig.output);
    console.log('Translation completed');
})();
/**
 * Format the source code
 * @param srcDir
 * @returns {Promise}
 */
async function formatSourceDir(srcDir) {
    log(`Format the source code:${srcDir}`);
    return new Promise((resolve, reject) => {
        klaw_1.default(srcDir)
            .on('data', async (item) => {
            if (item.path.endsWith('.ts')) {
                try {
                    let fileContent = await fs_extra_1.readFile(item.path);
                    await fs_extra_1.writeFile(item.path, prettier_1.default.format(fileContent.toString(), {
                        parser: 'typescript',
                        singleQuote: true,
                        bracketSpacing: false,
                        trailingComma: 'all',
                        semi: true,
                    }));
                    log(`Format the source code successfully:${item.path}`);
                }
                catch (err) {
                    log(`Failed to format the source code:${item.path} ${err}`);
                    console.warn(`Failed to format the source code:${item.path} ${err}`);
                    reject(err);
                }
            }
        })
            .on('end', () => {
            resolve();
        });
    });
}
process.on('uncaughtException', err => {
    console.log(err);
});
process.on('unhandledRejection', err => {
    console.log(err);
});
