UNPKG

@mongez/gnz

Version:

Generator Z, the next generation of scaffolding tools.

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