"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
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 fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const creator_1 = __importStar(require("./creator"));
const inquirerScripts_1 = require("./util/inquirerScripts");
const util_1 = require("../util");
function create(type, comName, options) {
    return __awaiter(this, void 0, void 0, function* () {
        if (options.proxy) {
            process.env.http_proxy = options.proxy;
        }
        let materialsPath = '';
        switch (type) {
            case creator_1.CreateType.Action: {
                materialsPath = path_1.default.join(process.cwd(), 'src/actions');
                break;
            }
            case creator_1.CreateType.Plugin: {
                materialsPath = path_1.default.join(process.cwd(), 'src/plugins');
                break;
            }
            case creator_1.CreateType.Component: {
                materialsPath = path_1.default.join(process.cwd(), 'src/components');
                break;
            }
            case creator_1.CreateType.Container: {
                materialsPath = path_1.default.join(process.cwd(), 'src/containers');
                break;
            }
            default:
                throw new Error('Invalid create type');
        }
        if (!fs_extra_1.default.pathExistsSync(materialsPath)) {
            throw new Error('请在组件库的根目录执行创建组件的命令');
        }
        const finalMaterialName = comName.charAt(0).toUpperCase() + comName.substring(1);
        const targetDir = path_1.default.join(materialsPath, finalMaterialName);
        yield inquirerScripts_1.initDir(targetDir);
        const creator = new creator_1.default(finalMaterialName, targetDir, type, options);
        yield creator.create();
    });
}
function createComponent(...args) {
    return create(creator_1.CreateType.Component, ...args).catch((err) => {
        util_1.stopSpinner(false);
        util_1.error(err);
        process.exit(1);
    });
}
exports.createComponent = createComponent;
function createPlugin(...args) {
    return create(creator_1.CreateType.Plugin, ...args).catch((err) => {
        util_1.stopSpinner(false);
        util_1.error(err);
        process.exit(1);
    });
}
exports.createPlugin = createPlugin;
function createAction(...args) {
    return create(creator_1.CreateType.Action, ...args).catch((err) => {
        util_1.stopSpinner(false);
        util_1.error(err);
        process.exit(1);
    });
}
exports.createAction = createAction;
function createContainer(...args) {
    return create(creator_1.CreateType.Container, ...args).catch((err) => {
        util_1.stopSpinner(false);
        util_1.error(err);
        process.exit(1);
    });
}
exports.createContainer = createContainer;
//# sourceMappingURL=createMaterial.js.map