"use strict";
/// <reference path="../../typings/index.d.ts" />
require("source-map-support/register");
var fs = require("fs-extra");
var pathModule = require("path");
var wrench = require("wrench");
var commander = require("commander");
var context_1 = require("./context");
var tsConfigParser_1 = require("./tsConfigParser");
var composer_1 = require("../../composer/server/src/composer");
var colors = require("colors");
var program = commander
    .description("Core util to generate leaf-ts required resources")
    .usage("[path-to-R.d.ts] [options...]")
    .option("-g --global", "created R.ts don't export")
    .option("-l --leaf <leaf>", "relative link to leaf.d.ts")
    .option("-c --create", "create the R.d.ts")
    .option("--root-name <name>", "using custom root namespace instead of \"R\"")
    .option("--composer", "start composer server")
    .option("--composer-port <port>", "port for the composer")
    .option("--composer-style-path <path>", "If your style file isn't at /style/main.css, set it here. The path is relative to R.d.ts or `Root` value of R.d.ts")
    .parse(process.argv);
var defaultTemplate = "type Template = \"./template\"\ntype Output = \"./script/R.ts\"\ntype StyleOutput = \"./style/R.less\"\ntype Root = \"./\"\n";
if (program["create"]) {
    fs.writeFileSync("./R.d.ts", defaultTemplate);
    process.exit(0);
}
var configPath = program.args[0] || "R.d.ts";
configPath = pathModule.resolve(process.cwd() + "/", configPath);
if (!fs.existsSync(configPath)) {
    console.error(colors.red("Can't find config R.d.ts at " + configPath));
    program.help();
    process.exit(1);
}
var parser = new tsConfigParser_1.TSConfigParser(configPath);
if (program["composer"]) {
    var server = new composer_1.ComposerServer(parser);
    if (program["composerStylePath"]) {
        server.setStylePath(program["composerStylePath"]);
    }
    server.start(program["composerPort"] || 3780);
}
else {
    var context_2 = new context_1.Context(parser.getTemplatePath());
    if (program["rootName"]) {
        context_2.rootName = program["rootName"];
    }
    // compile mode
    var files = wrench.readdirSyncRecursive(parser.getTemplatePath()).filter(function (item) { return fs.statSync(pathModule.join(parser.getTemplatePath(), item)).isFile(); });
    var supportedExtnames = [".html", ".widget"];
    for (var _i = 0, files_1 = files; _i < files_1.length; _i++) {
        var file = files_1[_i];
        var ext = pathModule.extname(file);
        if (supportedExtnames.indexOf(ext) < 0)
            continue;
        context_2.includeWidgetByRelativeFilePath(file);
    }
    var output = context_2.compile();
    if (program["leaf"]) {
        output = "///<reference path=\"" + program["leaf"] + "\"/>" + "\n" + output;
    }
    fs.writeFileSync(parser.getOutputPath(), output);
    var styleOutput = parser.getStyleOutputPath();
    if (styleOutput) {
        var content = context_2.compileStyle();
        if (content) {
            fs.writeFileSync(styleOutput, content);
        }
    }
}
//# sourceMappingURL=cli.js.map