vue3-structure-builder
Version:
`vue3-structure-builder` is a tool that helps developers quickly generate a modular and scalable file structure for Vue 3 projects. By defining a module structure in a `schema.js` file, this tool automatically generates directories, components, pages, sto
3 lines (2 loc) • 7.33 kB
JavaScript
import n from"fs";import e from"path";const o=()=>'\n<template>\n<div>\n\n</div>\n</template>\n\n<script setup lang="ts">\n<\/script>\n\n<style scoped>\n/* Add your component styles here */\n</style>\n';const t=(n,e,o)=>{return`\n import { RouteRecordRaw } from "vue-router";\n export default [\n {\n path: "${r=n,r.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase()}",\n component: () => import("./layout/index.vue"),\n name: "${t=n,t.split("-").map(((n,e)=>0===e?n:n.charAt(0).toUpperCase()+n.slice(1))).join("")}Layout",\n children:[\n ${o.page.length?o.page.map((n=>`{\n path: "${n}",\n component: () => import("./pages/${n}.vue"),\n },\n `)):""}\n ]\n }\n ] as Array<RouteRecordRaw>;\n `;var t,r},r=n=>`\n <template>\n <div class="">${n} page</div>\n </template>\n <script lang="ts"setup>\n <\/script>\n `,a=()=>'\n <script setup lang="ts">\n // import parent layout what you want and wrap solt inside it to make sited layout \n // import ParentLayout from "../../layouts/*****.vue" \n <\/script>\n \n <template>\n \x3c!-- <pearnt-layout> // id it has --\x3e\n <slot />\n \x3c!-- <pearnt-layout> --\x3e\n </template>\n \n ';function s(n){return n.split("-").map(((n,e)=>0===e?n:n.charAt(0).toUpperCase()+n.slice(1))).join("")}const i=n=>`\n import { defineStore } from "pinia";\n const use${s(n)}Store = defineStore("${s(n)}Store", () => {});\n export default use${s(n)}Store;\n \n `,c=()=>'\nexport default {\n\n}\n// import ar from "../i18n/ar";\n// import en from "../i18n/en";\n// import { useI18n } from "vue-i18n";\n// const { t } = useI18n({\n// messages: {\n// en: en,\n// ar: ar,\n// },\n// }); \n',l=()=>"\nimport enUS from './locales/en.json'\nimport ar from './locales/ar.json'\nimport { createI18n } from 'vue-i18n'\nimport Cookies from \"js-cookie\";\nconst localLang = Cookies.get('lang')\ntype enSchema = typeof enUS\n\nconst i18n = createI18n<[enSchema], 'en' | 'ar'>({\n legacy: false, // you must set false, to use Composition API\n locale: localLang || 'en',\n // @ts-ignore\n messages: {\n 'en': enUS,\n 'ar': ar\n }\n})\nexport default i18n\n}));\n";const p=(n,e,o)=>`\nimport * as VueRouter from "vue-router";\nimport guards from "./guards";\n\n const routes: [\n ${o.map((n=>{return n?`\n {\n path: "${t=n.name,t.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase()}",\n component:()=> import ("@/modules/${n.name}/layout/index.vue"),\n name: "${e=n.name,e&&"string"==typeof e?e.includes("-")?e.split("-").map(((n,e)=>0===e?n:n.charAt(0).toUpperCase()+n.slice(1))).join(""):e:""}",\n ${o.children?"children: []":""}\n }\n `:"";var e,t}))}\n ]\n\n const validRoutes = routes.filter((route): route is VueRouter.RouteRecordRaw => route !== undefined);\n\nconst router = VueRouter.createRouter({\n scrollBehavior: () => {\n return { x: 0, y: 0, behavior: "smooth" };\n },\nroutes: validRoutes,\n});\n\nguards(router);\n\nexport default router;\n`,u=()=>'\n import { Router } from "vue-router";\n \n export default function ProtectedRouter(router: Router) {\n return router.beforeEach((to, from, next) => {\n // add router guards if you has\n });\n }\n \n ',m=()=>'\n// Type Stracture {\n // name: String;\n // store: Boolean;\n // components: string[] ; \n // page: string[] ;\n // children: string[] | null ; \n // }folder1\n // example of schema.js\n export default [\n {\n name: "dashboard", // name of module\n store: true, // is module has store (pinia)\n i18n: false, // is has i18n \n components: ["component-1", "component-2"], // components name for module \n page: ["index", \'add\'], // pages name \n children: null // is has children \n },\n {\n name: "category",\n store: false,\n components: ["components1", "components2"],\n page: ["index"],\n children: [\n {\n name: "children1",\n store: false,\n layout: true,\n components: ["components1", "components2"],\n page: ["index"],\n i18n:true,\n children: null\n },\n {\n name: "children2",\n store: false,\n i18n:false,\n layout: true,\n components: ["components1", "components2"],\n page: ["index"],\n children: [\n {\n name: "children1",\n store: false,\n i18n:true,\n components: ["components1", "components2"],\n page: ["index"],\n children: null\n },\n {\n name: "children2",\n store: false,\n i18n:true,\n components: ["components1", "components2"],\n page: ["index"],\n children: null\n }\n ],\n }\n ],\n },\n]\n\n';const d=(o,t)=>{t.replace(/^.*\/src\//,"src/");const r=function(o){let t=o;for(;t!==e.parse(t).root;){const o=e.join(t,"routes.tsx");if(n.existsSync(o))return o;t=e.dirname(t)}return null}(e.join(e.dirname(t),".."));if(r){let s=n.readFileSync(r,{encoding:"utf8",flag:"r"});console.log("Hex Dump:",s.toString("hex"));try{const i=/children:\s*\[(.*?)\]/s,c=s.match(i),l=e.relative(r,t).replace(/^(\.\.\/)+/,"./"),p=`{ path: "/${a=o,a.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase()}", component: () => import("${l}") }`;if(c){let e=c[1].trim(),o=e?e.split(",").map((n=>n.trim())):[];if(o.includes(p))console.log(`module "${t}" is already in children[]`);else{o.push(p);const e=`children: [\n${o.map((n=>`${n}`)).join(",\n")}\n]\n `;s=s.replace(i,e),n.writeFileSync(r,s,"utf8"),console.log(`Add "${t}" to children[] in ${r}`)}}else s=s.replace(/(children:\s*\[)([\s\S]*?)(\])/,`$1$2\n ${p},$3`),n.writeFileSync(r,s,"utf8"),console.log(`created children[] and added "${p}" in ${r}`)}catch(n){console.error("error reading or modifying the router file:",n)}}else console.error("router.ts or router.js not found in any parent directory .");var a},f=(e,o,t,r,a)=>n.writeFile(`${e}`,t(r,e,a),"utf8",(n=>{n?console.error(`Failed to create Vue file: ${o} => ${r}`,n):(console.log(`${o} => ${r} created successfully! in ${e}`),"route"===o&&d(r,e))}));function h(n,e,s,d){switch(e){case"component":f(n,e,o,s);break;case"page":f(n,e,r,s);break;case"layout":f(n,e,a,s);break;case"store":f(n,e,i,s);break;case"router-config":f(n,e,p,"",d);break;case"router-guards":f(n,e,u,"",d);break;case"route":f(n,e,t,s,d);break;case"i18n":f(n,e,c,s);break;case"schema":f(n,e,m,s);break;case"i18n_conf":f(n,e,l,s);break;default:console.error(`Unknown type: ${e}`)}}export{h as c};
//# sourceMappingURL=check-type-0a835837.js.map