UNPKG

@mongez/gnz

Version:

Generator Z, the next generation of scaffolding tools.

65 lines (63 loc) 3.1 kB
'use strict';var fs=require('@mongez/fs'),reinforcements=require('@mongez/reinforcements'),pluralize=require('pluralize');require('os');var prettifier=require('../../utils/prettifier.js');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var pluralize__default=/*#__PURE__*/_interopDefault(pluralize);async function generateRoutesFile(options) { const saveComponentsIn = options.saveComponentsIn; const singleName = pluralize__default.default(options.name, 1); const imports = [ `import URLS from "apps/${options.appName}/utils/urls";`, `import { guardedRoutes } from "apps/${options.appName}/utils/router";`, `import ${reinforcements.toStudlyCase(options.name)}Page from "./${saveComponentsIn}/${reinforcements.toStudlyCase(options.name)}Page";`, ]; let guardedRoutes = ` guardedRoutes([ { path: URLS.${reinforcements.toCamelCase(options.name)}, component: ${reinforcements.toStudlyCase(options.name)}Page, } ]); `; if (options.withDetailsPage) { imports.push(`import ${reinforcements.toStudlyCase(singleName)}DetailsPage from "./${saveComponentsIn}/${reinforcements.toStudlyCase(singleName)}DetailsPage";`); guardedRoutes = ` guardedRoutes([ { path: URLS.${reinforcements.toCamelCase(options.name)}.list, component: ${reinforcements.toStudlyCase(options.name)}Page, }, { path: URLS.${reinforcements.toCamelCase(options.name)}.viewRoute, component: ${reinforcements.toStudlyCase(singleName)}DetailsPage, }, ]); `; } const contents = ` ${imports.join("\n")} ${guardedRoutes} `; return await prettifier.format.typescript(contents); } async function updateUrlsFile(options) { // get file content const contents = fs.getFile(options.urlsFilePath); // let's prepare first the replacement content let replacementContent = `const URLS = { `; if (options.withDetailsPage) { const singleDetailsName = pluralize__default.default(reinforcements.toCamelCase(options.name), 1); const viewDetailsKey = `${reinforcements.toStudlyCase(singleDetailsName)}`; replacementContent += ` ${reinforcements.toCamelCase(options.name)}: { list: "/${reinforcements.toKebabCase(options.name)}", viewRoute: "/${reinforcements.toKebabCase(options.name)}/:id", view${viewDetailsKey}: (${singleDetailsName}: any) => \`/${reinforcements.toKebabCase(options.name)}/\${${singleDetailsName}.id}\`, }, `; } else { replacementContent += ` ${reinforcements.toCamelCase(options.name)}: "/${reinforcements.toKebabCase(options.name)}", `; } // now look for `const URLS = {` const finalContent = contents.replace(/const URLS = {/, replacementContent.trim()); return await prettifier.format.typescript(finalContent); }exports.generateRoutesFile=generateRoutesFile;exports.updateUrlsFile=updateUrlsFile;//# sourceMappingURL=template.js.map