UNPKG

@mongez/gnz

Version:

Generator Z, the next generation of scaffolding tools.

36 lines (35 loc) 2.9 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 generate = async (options) => { options.name = reinforcements.toStudlyCase(options.name); const now = Date.now(); const content = await template.gnReactComponent(options); const { name: componentName, saveTo } = 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 * from "./${componentName}"; `; fs.putFile(indexPath, indexContent); prettifier.eslint.formatFile(indexPath); // 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;//# sourceMappingURL=generator.js.map