diff --git a/.github/workflows/playwright.yml b/.github/workflows/housekeeping.yml similarity index 87% rename from .github/workflows/playwright.yml rename to .github/workflows/housekeeping.yml index e4ebc9b..b849acc 100644 --- a/.github/workflows/playwright.yml +++ b/.github/workflows/housekeeping.yml @@ -1,4 +1,4 @@ -name: Playwright Tests +name: lint and test on: push: branches: [ main, master ] @@ -17,6 +17,10 @@ jobs: run: npm install -g pnpm && pnpm install - name: Install Playwright Browsers run: pnpm exec playwright install --with-deps + - name: Lint code + run: pnpm lint + - name: Run unit tests + run: pnpm vitest - name: Start Vite dev server run: pnpm exec vite --port 3000 & - name: Run Playwright tests diff --git a/package.json b/package.json index bb339d6..66ea074 100644 --- a/package.json +++ b/package.json @@ -8,10 +8,12 @@ "lint": "eslint . --fix", "format": "prettier --write --ignore-unknown src", "page": "tsx src/scripts/scafold-pages/script.ts", - "vitest": "vitest --ui", + "vitest": "vitest", + "vitest:ui": "vitest --ui", "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": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0dc2439..fdd8206 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,3 +1,4 @@ + lockfileVersion: '9.0' settings: diff --git a/src/sample.test.ts b/src/sample.test.ts new file mode 100644 index 0000000..f6ca835 --- /dev/null +++ b/src/sample.test.ts @@ -0,0 +1,5 @@ +import { expect, test } from "vitest"; + +test("should pass", () => { + expect(true).toBe(true); +}); diff --git a/src/scripts/scafold-pages/scafold-page.ts b/src/scripts/scafold-pages/scafold-page.ts new file mode 100644 index 0000000..0a76b6a --- /dev/null +++ b/src/scripts/scafold-pages/scafold-page.ts @@ -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); + } + }); + } + } + } +} diff --git a/src/scripts/scafold-pages/script.ts b/src/scripts/scafold-pages/script.ts index 2e0c073..298fcc5 100644 --- a/src/scripts/scafold-pages/script.ts +++ b/src/scripts/scafold-pages/script.ts @@ -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] diff --git a/vite.config.ts b/vite.config.ts index 2fbc465..1cb2c7d 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -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"], }, });