UNPKG

@mongez/gnz

Version:

Generator Z, the next generation of scaffolding tools.

52 lines (51 loc) 3.5 kB
'use strict';var fs=require('@mongez/fs'),reinforcements=require('@mongez/reinforcements'),chalk=require('chalk'),path=require('path');require('os');var prettifier=require('../../utils/prettifier.js'),throwIf=require('../../utils/throw-if.js'),template=require('./template.js');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var chalk__default=/*#__PURE__*/_interopDefault(chalk);var path__default=/*#__PURE__*/_interopDefault(path);const parseServiceName = (serviceName) => { if (!serviceName.endsWith("Service")) { serviceName += "Service"; } return reinforcements.toCamelCase(serviceName); }; const generate = async (options) => { options.name = reinforcements.toStudlyCase(options.name); if (!options.title) { options.title = options.name; } if (options.asPage && !options.name.endsWith("Page")) { options.name += "Page"; } if (options.serviceName) { options.serviceName = parseServiceName(options.serviceName); } if (options.formComponentName) { options.formComponentName = reinforcements.toStudlyCase(options.formComponentName); } const now = Date.now(); const { name: componentName, saveTo } = options; const content = await template.generateSuperTableTemplate(options); fs.ensureDirectory(saveTo); // check now if there is a `withIndex` option // if so, then we'll need to generate an index file if (options.withIndex) { // create the directory const componentDirectoryPath = path__default.default.join(saveTo, componentName); throwIf.throwIf(fs.directoryExists(componentDirectoryPath), `Directory ${chalk__default.default.yellow(componentDirectoryPath)} already exists. Please remove it first, or use a different directory/name.`); fs.makeDirectory(componentDirectoryPath); // create the component file const componentFullPath = path__default.default.join(componentDirectoryPath, `${componentName}.tsx`); fs.putFile(componentFullPath, content); prettifier.eslint.formatFile(path__default.default.join(componentDirectoryPath, `${componentName}.tsx`)); // create the index file const indexPath = path__default.default.join(componentDirectoryPath, "index.ts"); const indexContent = `export { default } from "./${componentName}";`; fs.putFile(indexPath, indexContent); // log for success creation of index file console.log(`Index file has been generated successfully in ${chalk__default.default.cyan(path__default.default.relative(process.cwd(), indexPath).replaceAll("\\", "/"))} ${chalk__default.default.gray(`(${Date.now() - now}ms)`)}`); } else { const componentPath = path__default.default.join(saveTo, `${componentName}.tsx`); throwIf.throwIf(fs.fileExists(componentPath), `Component ${chalk__default.default.green(componentName)} already exists in ${chalk__default.default.yellow(saveTo)}`); fs.putFile(componentPath, content); } console.log(`Component ${chalk__default.default.green(componentName)} has been generated successfully in ${chalk__default.default.cyan(path__default.default .relative(process.cwd(), path__default.default.join(path__default.default.resolve(saveTo), componentName)) .replaceAll("\\", "/"))} ${chalk__default.default.gray(`(${Date.now() - now}ms)`)}`); };exports.generate=generate;exports.parseServiceName=parseServiceName;//# sourceMappingURL=generator.js.map