"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 path_1 = __importDefault(require("path"));
const chalk_1 = __importDefault(require("chalk"));
const validate_npm_package_name_1 = __importDefault(require("validate-npm-package-name"));
const creator_1 = __importStar(require("./creator"));
const inquirerScripts_1 = require("./util/inquirerScripts");
const util_1 = require("../util");
function create(libName, options) {
    return __awaiter(this, void 0, void 0, function* () {
        if (options.proxy) {
            process.env.HTTP_PROXY = options.proxy;
        }
        const cwd = options.cwd || process.cwd();
        const inCurrent = libName === '.';
        libName = inCurrent ? libName : `materials-${libName}`;
        const name = (inCurrent ? path_1.default.relative('../', cwd) : libName).replace('materials-', '');
        const targetDir = path_1.default.resolve(cwd, libName || '.');
        const result = validate_npm_package_name_1.default(name);
        if (!result.validForNewPackages) {
            console.error(chalk_1.default.red(`Invalid project name: "${name}"`));
            result.errors &&
                result.errors.forEach((err) => {
                    console.error(chalk_1.default.red.dim(`Error: ${err}'`));
                });
            result.warnings &&
                result.warnings.forEach((warn) => {
                    console.error(chalk_1.default.red.dim(`Warning: ${warn}`));
                });
            process.exit(1);
        }
        yield inquirerScripts_1.initDir(targetDir, inCurrent);
        const creator = new creator_1.default(name, targetDir, creator_1.CreateType.Lib, options);
        yield creator.create();
    });
}
exports.default = (...args) => {
    return create(...args).catch((err) => {
        util_1.stopSpinner(false);
        util_1.error(err);
        process.exit(1);
    });
};
//# sourceMappingURL=createLibrary.js.map