Skip to content

Commit

Permalink
Add dry run script and refactor scaffold page functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
tigawanna committed Dec 1, 2024
1 parent 41b2369 commit 8173d79
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 124 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"playwright": "playwright test --ui",
"test": "npm run vitest && npm run playwright",
"build": "tsc -b && vite build",
"dryrun":"npm run lint && npm run test && npm run build",
"preview": "vite preview"
},
"dependencies": {
Expand Down
5 changes: 5 additions & 0 deletions src/sample.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { expect, test } from "vitest";

test("should pass", () => {
expect(true).toBe(true);
});
125 changes: 125 additions & 0 deletions src/scripts/scafold-pages/scafold-page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { resolve } from "node:path";
import { writeFile, access, mkdir } from "node:fs/promises";
import {
rootPageComponentTemplate,
rootPageListComponentsTemplate,
rootPageTemplate,
} from "./base-templates";
import {
rootPageBaseFormComponentsTemplate,
rootPageCreateFormComponentsTemplate,
rootPageUpdateFormComponentsTemplate,
} from "./form-templates";
import {
rootOnePageComponentsTemplate,
rootOnePageDetailsComponentsTemplate,
rootOnePageTemplate,
} from "./one-page-template";
import { rootPageQeuryOptionsTemplate } from "./query-options-tempaltes";
export async function scaffoldPage(pagename: string, path: string) {
const roootDirpath = `./src/routes${path}`;
// const roootDirpath = resolve("./src/routes",path)
await mkdir(roootDirpath, { recursive: true });
const capitalpagename = pagename.charAt(0).toUpperCase() + pagename.slice(1);
let rootPath = path.trim();
if (rootPath.length == 0) {
throw new Error("Path cannot be empty");
}
if (rootPath.endsWith("/")) {
rootPath = rootPath.slice(0, rootPath.length - 1);
}
if (rootPath.startsWith("./")) {
rootPath = rootPath.slice(2);
}
if (rootPath.startsWith("/")) {
rootPath = rootPath.slice(1);
}

const indexPage = {
path: `${rootPath}/index.tsx`,
component: rootPageTemplate(pagename, rootPath),
};
const indexPageComponent = {
path: `${rootPath}/-components/${capitalpagename}Page.tsx`,
component: rootPageComponentTemplate(pagename, rootPath),
};
const indexPageListComponent = {
path: `${rootPath}/-components/list/${capitalpagename}List.tsx`,
component: rootPageListComponentsTemplate(pagename, rootPath),
};

const baseForm = {
path: `${rootPath}/-components/form/base.tsx`,
component: rootPageBaseFormComponentsTemplate(pagename),
};
const createForm = {
path: `${rootPath}/-components/form/create.tsx`,
component: rootPageCreateFormComponentsTemplate(pagename),
};
const updateForm = {
path: `${rootPath}/-components/form/update.tsx`,
component: rootPageUpdateFormComponentsTemplate(pagename),
};

// const listComponent = {
// path: `${rootPath}/-components/${capitalpagename}List.tsx`,
// component: rootPageListComponentsTemplate(pagename, rootPath),
// };
const onePageComponent = {
path: `${rootPath}/$${pagename}/index.tsx`,
component: rootOnePageTemplate(pagename, rootPath),
};
const onepageComponent = {
path: `${rootPath}/-components/one${pagename}/One${capitalpagename}Page.tsx`,
component: rootOnePageComponentsTemplate(pagename),
};
const onepageDetailsComponent = {
path: `${rootPath}/-components/one${pagename}/One${capitalpagename}Details.tsx`,
component: rootOnePageDetailsComponentsTemplate(pagename, rootPath),
};
const queryOptions = {
path: `${rootPath}/-query-options/${pagename}-query-option.ts`,
component: rootPageQeuryOptionsTemplate(pagename),
};

const allPaths = [
indexPage,
indexPageComponent,
baseForm,
createForm,
updateForm,
indexPageListComponent,
onePageComponent,
onepageComponent,
onepageDetailsComponent,
queryOptions,
];

const allComponentPaths = allPaths.map((path) => {
console.log("path======= > ", path.path);
return ensurePathExistsOrCreate(path.path, path.component);
});
await Promise.all(allComponentPaths);
}

async function ensurePathExistsOrCreate(path: string, component: string) {
const component_path = resolve("./src/routes", path);
try {
await access(component_path);
} catch (err: unknown) {
if (err instanceof Error) {
if (err.message.includes("no such file or directory")) {
await writeFile(component_path, component).catch(async (err) => {
if (err.code === "ENOENT") {
const directryPath = component_path
.split("/")
.slice(0, -1)
.join("/");
await mkdir(directryPath, { recursive: true });
await writeFile(component_path, component);
}
});
}
}
}
}
123 changes: 1 addition & 122 deletions src/scripts/scafold-pages/script.ts
Original file line number Diff line number Diff line change
@@ -1,127 +1,6 @@
import { resolve } from "node:path";
import { rootPageComponentTemplate, rootPageListComponentsTemplate, rootPageTemplate } from "./base-templates"
import { rootPageBaseFormComponentsTemplate, rootPageCreateFormComponentsTemplate, rootPageUpdateFormComponentsTemplate } from "./form-templates"
import { rootOnePageComponentsTemplate, rootOnePageDetailsComponentsTemplate, rootOnePageTemplate } from "./one-page-template";
import { rootPageQeuryOptionsTemplate } from "./query-options-tempaltes"
import { writeFile, access, mkdir } from "node:fs/promises"
import { scaffoldPage } from "./scafold-page"



async function scaffoldPage(pagename: string, path: string) {
const roootDirpath = `./src/routes${ path}`
// const roootDirpath = resolve("./src/routes",path)
await mkdir(roootDirpath, { recursive: true }).catch(() => {
// if (err instanceof Error) {
// if (err.message.includes("EEXIST")) {
// }else{
// throw err
// }
// }

})
const capitalpagename = pagename.charAt(0).toUpperCase() + pagename.slice(1);
let rootPath = path.trim()
if (rootPath.length == 0) {
throw new Error("Path cannot be empty")
}
if (rootPath.endsWith("/")) {
rootPath = rootPath.slice(0, rootPath.length - 1)
}
if (rootPath.startsWith("./")) {
rootPath = rootPath.slice(2)
}
if (rootPath.startsWith("/")) {
rootPath = rootPath.slice(1)
}

const indexPage = {
path: `${rootPath}/index.tsx`,
component: rootPageTemplate(pagename, rootPath),
};
const indexPageComponent = {
path: `${rootPath}/-components/${capitalpagename}Page.tsx`,
component: rootPageComponentTemplate(pagename, rootPath),
};
const indexPageListComponent = {
path: `${rootPath}/-components/list/${capitalpagename}List.tsx`,
component: rootPageListComponentsTemplate(pagename, rootPath),
};

const baseForm = {
path: `${rootPath}/-components/form/base.tsx`,
component: rootPageBaseFormComponentsTemplate(pagename),
};
const createForm = {
path: `${rootPath}/-components/form/create.tsx`,
component: rootPageCreateFormComponentsTemplate(pagename),
};
const updateForm = {
path: `${rootPath}/-components/form/update.tsx`,
component: rootPageUpdateFormComponentsTemplate(pagename),
};

// const listComponent = {
// path: `${rootPath}/-components/${capitalpagename}List.tsx`,
// component: rootPageListComponentsTemplate(pagename, rootPath),
// };
const onePageComponent = {
path: `${rootPath}/$${pagename}/index.tsx`,
component: rootOnePageTemplate(pagename, rootPath),
}
const onepageComponent = {
path: `${rootPath}/-components/one${pagename}/One${capitalpagename}Page.tsx`,
component: rootOnePageComponentsTemplate(pagename),
};
const onepageDetailsComponent = {
path: `${rootPath}/-components/one${pagename}/One${capitalpagename}Details.tsx`,
component: rootOnePageDetailsComponentsTemplate(pagename, rootPath),
}
const queryOptions = {
path: `${rootPath}/-query-options/${pagename}-query-option.ts`,
component: rootPageQeuryOptionsTemplate(pagename),
};

const allPaths = [
indexPage,
indexPageComponent,
baseForm,
createForm,
updateForm,
indexPageListComponent,
onePageComponent,
onepageComponent,
onepageDetailsComponent,
queryOptions
]

const allComponentPaths = allPaths.map((path) => {
console.log("path======= > ", path.path)
return ensurePathExistsOrCreate(path.path, path.component)
})
await Promise.all(allComponentPaths)

}

async function ensurePathExistsOrCreate(path: string, component: string) {
const component_path = resolve("./src/routes",path)
try {
await access(component_path)
} catch (err: unknown) {
if (err instanceof Error) {
if (err.message.includes("no such file or directory")) {
await writeFile(component_path, component)
.catch(async(err) => {
if(err.code === "ENOENT"){
const directryPath = component_path.split("/").slice(0,-1).join("/")
await mkdir(directryPath, { recursive: true })
await writeFile(component_path, component)
}
})
}
}
}
}

function main() {
const components_path = process.argv[2]

Expand Down
4 changes: 2 additions & 2 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default defineConfig({
},
test: {
globals: true,
include: ["./src"],
exclude: ["e2e-tests","node_modules"],
include: ["./src/**/*.{test,spec}.?(c|m)[jt]s?(x)"],
exclude: ["e2e-tests", "node_modules"],
},
});

0 comments on commit 8173d79

Please sign in to comment.