UNPKG

@mongez/gnz

Version:

Generator Z, the next generation of scaffolding tools.

38 lines (37 loc) 3.1 kB
'use strict';var fs=require('@mongez/fs'),reinforcements=require('@mongez/reinforcements'),chalk=require('chalk'),path=require('path'),namesFactory=require('../../../factories/names-factory.js');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.gnQwikComponent(options); const { name, saveTo } = options; const componentName = namesFactory.namesFactory.qwikComponent(name); const componentPath = namesFactory.namesFactory.qwikPath(name); 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, componentPath); 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, `${componentPath}.tsx`); fs.putFile(componentFullPath, content); prettifier.eslint.formatFile(path__default.default.join(componentDirectoryPath, `${componentPath}.tsx`)); // create the index file const indexPath = path__default.default.join(componentDirectoryPath, "index.ts"); const indexContent = `export * from "./${componentPath}"; `; 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 componentFullPath = path__default.default.join(saveTo, `${componentPath}.tsx`); throwIf.throwIf(fs.fileExists(componentFullPath), `Component ${chalk__default.default.green(componentName)} already exists in ${chalk__default.default.yellow(saveTo)}`); fs.putFile(componentFullPath, 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), componentPath)) .replaceAll("\\", "/"))} ${chalk__default.default.gray(`(${Date.now() - now}ms)`)}`); };exports.generate=generate;//# sourceMappingURL=generator.js.map