cr-react-cli
Version:
Create react files with a single command
64 lines (63 loc) • 3.19 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// Libs
const path_1 = __importDefault(require("path"));
// Constants
const constants_1 = require("../../constants");
// Helpers
const path_2 = require("../../utils/path");
const object_1 = require("../../utils/object");
const template_1 = require("../../utils/template");
const file_path_1 = __importDefault(require("../../file-path"));
// Configuration
const configuration_1 = require("../../configuration");
// Templates
const style_template_1 = __importDefault(require("../../templates/style/style.template"));
const styled_components_style_type_template_1 = __importDefault(require("../../templates/style/styled-components.style.type.template"));
const css_style_type_template_1 = __importDefault(require("../../templates/style/css.style.type.template"));
// Base
const base_action_1 = __importDefault(require("../base.action"));
class CreateStyleAction extends base_action_1.default {
async handle(inputs, options) {
const config = (0, object_1.merge)(options, inputs === null || inputs === void 0 ? void 0 : inputs.configOverride, (0, configuration_1.loadScopeConfiguration)('style'));
const stylingType = constants_1.cssStyleTypes.some((value) => config.type === value)
? 'css'
: 'js';
const jsTypeExtension = config.typescript ? 'ts' : 'js';
const fileExtension = stylingType === 'js' ? jsTypeExtension : config.type;
if (stylingType === 'css' &&
config.modules &&
!config.fileNaming.name.endsWith('.module')) {
config.fileNaming.name += '.module';
}
const filePath = new file_path_1.default({
name: inputs.name,
config: config,
relativeToFilePath: inputs.filePath,
pathPlaceholders: {
'{componentPath}': inputs.filePath,
},
sourcePath: (0, configuration_1.getSourcePath)(),
namePlaceholders: {
...(inputs.namePlaceholders || {}),
'{type}': 'style',
},
fileExtension: fileExtension,
});
const StyleTypeTemplate = stylingType === 'js'
? styled_components_style_type_template_1.default
: css_style_type_template_1.default;
const template = new style_template_1.default(inputs.name, config,
// @ts-ignore
StyleTypeTemplate);
await (0, path_2.handlePathCheck)(filePath.dir);
const importPath = (0, path_2.fixRelativePath)(path_1.default.join(path_1.default.relative(inputs.filePath, filePath.baseDir), filePath.base));
const componentTemplate = template.include(inputs.template, importPath);
const componentFilePath = path_1.default.join(inputs.filePath, inputs.fileName);
await this.create(filePath.full, (0, template_1.formatTemplate)(template.build()));
await this.update(componentFilePath, (0, template_1.formatTemplate)(componentTemplate));
}
}
exports.default = CreateStyleAction;