UNPKG

cr-react-cli

Version:

Create react files with a single command

119 lines (118 loc) 5.16 kB
var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); // Libs const fs_1 = __importDefault(require("fs")); const case_1 = __importDefault(require("case")); const child_process_1 = require("child_process"); // Actions const index_action_1 = __importDefault(require("./index.action")); const style_action_1 = __importDefault(require("./style.action")); const story_action_1 = __importDefault(require("./story.action")); const test_action_1 = __importDefault(require("./test.action")); // Helpers const file_path_1 = __importDefault(require("../../file-path")); const object_1 = require("../../utils/object"); const path_1 = require("../../utils/path"); const template_1 = require("../../utils/template"); // Configuration const configuration_1 = require("../../configuration"); // Templates const redux_template_1 = __importDefault(require("../../templates/redux/redux.template")); const prop_types_template_1 = __importDefault(require("../../templates/prop-types/prop-types.template")); const compnent_template_1 = __importDefault(require("../../templates/component/compnent.template")); // Base const base_action_1 = __importDefault(require("../base.action")); class CreateComponentAction extends base_action_1.default { async handle(inputs, options) { var _a, _b, _c; const scopeConfig = (0, configuration_1.loadScopeConfiguration)('component')[inputs.type]; const config = (0, object_1.merge)({ ...options }, scopeConfig); const compnentTypePostfix = inputs.type === 'default' ? 'component' : inputs.type; const path = new file_path_1.default({ name: inputs.name, fileName: options === null || options === void 0 ? void 0 : options.fileName, config: config, sourcePath: (0, configuration_1.getSourcePath)(), namePlaceholders: { '{componentType}': compnentTypePostfix, '{type}': inputs.type, }, fileExtension: config.typescript ? 'tsx' : 'jsx', }); const componentName = case_1.default.pascal(path.name); let template = new compnent_template_1.default(componentName, config).build(); await (0, path_1.handlePathCheck)(path.baseDir); await (0, path_1.handleFileCheck)(path.full); if (config.inFolder) { this.createComponentFolder(path.dir); } if (config.redux) { template = new redux_template_1.default().include(template, config.typescript); } if (config.proptypes) { template = new prop_types_template_1.default().include(template, componentName); } await this.create(path.full, (0, template_1.formatTemplate)(template)); if (config.style) { const styleInputs = { name: componentName, filePath: path.dir, fileName: path.name + '.' + path.ext, namePlaceholders: { '{name}': path.namePreferred, '{componentType}': inputs.type, }, configOverride: (_a = config.override) === null || _a === void 0 ? void 0 : _a.style, template, }; await new style_action_1.default().handle(styleInputs); } if (config.open) { (0, child_process_1.exec)(path.full); } if (config.index) { const indexInputs = { file: { importName: case_1.default.pascal(path.namePreferred), path: path.full, }, export: config.export.default ? 'default' : 'all', }; await new index_action_1.default().handle(indexInputs); } if (config.story) { const storyInputs = { filePath: path.full, componentName: componentName, componentDefaultImport: config.export.default, componentType: inputs.type, namePlaceholders: { '{name}': path.namePreferred, }, configOverride: (_b = config.override) === null || _b === void 0 ? void 0 : _b.storybook, }; await new story_action_1.default().handle(storyInputs); } if (config.test) { const testInputs = { filePath: path.full, componentName: componentName, componentDefaultImport: config.export.default, namePlaceholders: { '{name}': path.namePreferred, }, configOverride: (_c = config.override) === null || _c === void 0 ? void 0 : _c.test, }; await new test_action_1.default().handle(testInputs); } } createComponentFolder(path) { if (fs_1.default.existsSync(path)) { return; } fs_1.default.mkdirSync(path); } } exports.default = CreateComponentAction;