UNPKG

@mongez/gnz

Version:

Generator Z, the next generation of scaffolding tools.

94 lines (93 loc) 5.2 kB
'use strict';var fs=require('@mongez/fs'),reinforcements=require('@mongez/reinforcements'),chalk=require('chalk'),path=require('path'),pluralize=require('pluralize');require('os'),require('eslint'),require('prettier');var throwIf=require('../../utils/throw-if.js'),main=require('../../main.js'),index=require('../generate-client-restful-service/index.js'),index$2=require('../generate-reactive-form-component/index.js'),index$1=require('../generate-super-table-component/index.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);var pluralize__default=/*#__PURE__*/_interopDefault(pluralize);const generate = async (options) => { options.name = reinforcements.toKebabCase(options.name); const now = Date.now(); const { saveTo } = options; const directoryPath = saveTo + "/" + options.name; options.appName ||= reinforcements.toKebabCase(path__default.default.basename(options.saveTo)); throwIf.throwIf(fs.directoryExists(directoryPath), chalk__default.default.redBright(options.name) + " directory exists in " + chalk__default.default.yellow(saveTo)); fs.ensureDirectory(saveTo + "/" + options.name); // create service file await main.gnz.execute(index.generateGenerateClientRestfulService.execute({ name: options.name, saveTo: directoryPath + "/services", })); fs.makeDirectory(directoryPath + "/utils"); // create a types.ts file fs.putFile(directoryPath + "/utils/types.ts", "// types.ts file"); // create a locales.ts file fs.putFile(directoryPath + "/utils/locales.ts", "// locales.ts file"); fs.makeDirectory(directoryPath + "/components"); let saveComponentsIn = "components"; if (options.usingPages) { fs.makeDirectory(directoryPath + "/pages"); saveComponentsIn = "pages"; } const formComponentName = reinforcements.toStudlyCase(pluralize__default.default(options.name, 1)) + "Form"; // now let's generate the super table page await main.gnz.execute(index$1.generateSuperTableComponent.execute({ saveTo: directoryPath + "/" + saveComponentsIn, name: options.name, asPage: true, columns: options.inputs || { name: "string", }, filters: options.filters || { id: "text", name: "text", }, memo: true, withIndex: true, serviceName: reinforcements.toCamelCase(options.name) + "Service", formComponentName: formComponentName, formComponentPath: `./${formComponentName}`, servicePath: `apps/${options.appName}/${reinforcements.toKebabCase(options.name)}/services/${reinforcements.toKebabCase(options.name)}-service`, })); // now create the reactive form await main.gnz.execute(index$2.generateReactiveFormComponent.execute({ saveTo: directoryPath + "/" + saveComponentsIn + "/" + reinforcements.toStudlyCase(options.name) + "Page", name: formComponentName, memo: true, withIndex: true, singleName: pluralize__default.default(options.name, 1), inputs: options.inputs || { name: "string", }, serviceName: reinforcements.toCamelCase(options.name) + "Service", servicePath: `apps/${options.appName}/${reinforcements.toKebabCase(options.name)}/services/${reinforcements.toKebabCase(options.name)}-service`, })); // now let's create the provider.ts file fs.putFile(directoryPath + "/provider.ts", `import "./routes";`); console.log(`Generating ${chalk__default.default.green("routes.ts")} file in ${chalk__default.default.yellow(directoryPath)}`); // now let's create the routes.ts file fs.putFile(directoryPath + "/routes.ts", await template.generateRoutesFile({ ...options, saveComponentsIn, })); // now check if the app has teh $app-modules.json file // if so, then add to modules array a new object if (options.addToAppModules && fs.fileExists(saveTo + `/${options.appName}-modules.json`)) { const content = fs.getJsonFile(saveTo + `/${options.appName}-modules.json`); content.modules.push({ entry: [`/${reinforcements.toKebabCase(options.name)}`], name: options.name, }); fs.putJsonFile(saveTo + `/${options.appName}-modules.json`, content); } // now let's update the urls.ts file if (options.updateUrls && fs.fileExists(saveTo + "/utils/urls.ts")) { console.log(`Updating ${chalk__default.default.green("urls.ts")} file in ${chalk__default.default.yellow(saveTo)}`); fs.putFile(saveTo + "/utils/urls.ts", await template.updateUrlsFile({ ...options, urlsFilePath: saveTo + "/utils/urls.ts", })); } console.log(`${chalk__default.default.green(options.name)} module has been generated successfully. ${chalk__default.default.gray(`(${Date.now() - now}ms)`)}`); };exports.generate=generate;//# sourceMappingURL=generator.js.map