From 5a7184ad99a7f296f63e6eab3b2b5b718c0fc289 Mon Sep 17 00:00:00 2001 From: Reed von Redwitz Date: Sun, 23 Jun 2024 17:44:07 +0200 Subject: [PATCH] fix: ensure deno task check passes in initialized projects (#2529) There were two issues with initialized projects: 1. not every file was formatted properly 2. linting wasn't passing when using `verbatim-module-syntax`. This isn't enabled right now, due to it not having the `recommended` tag, but it seems like a good idea. Actually initializing a project with this can be done in a separate PR. --- init/src/init.ts | 41 +++++++++++++++++------------------------ init/src/init_test.ts | 32 ++++++++++++++++++++++++++++---- 2 files changed, 45 insertions(+), 28 deletions(-) diff --git a/init/src/init.ts b/init/src/init.ts index 466ca99b..1c8c42d1 100644 --- a/init/src/init.ts +++ b/init/src/init.ts @@ -189,14 +189,13 @@ CMD ["run", "-A", "main.ts"] await writeFile("Dockerfile", DOCKERFILE_TEXT); } - const TAILWIND_CONFIG_TS = `import { type Config } from "tailwindcss"; + const TAILWIND_CONFIG_TS = `import type { Config } from "tailwindcss"; export default { content: [ "{routes,islands,components}/**/*.{ts,tsx}", ], -} satisfies Config; -`; +} satisfies Config;`; if (useTailwind) { await writeFile("tailwind.config.ts", TAILWIND_CONFIG_TS); } @@ -367,8 +366,8 @@ ${GRADIENT_CSS} // Skip this and be silent if there is a network issue. } - const MAIN_TS = `import { App, staticFiles, fsRoutes } from "fresh"; -import { State } from "./utils.ts"; + const MAIN_TS = `import { App, fsRoutes, staticFiles } from "fresh"; +import type { State } from "./utils.ts"; export const app = new App(); app.use(staticFiles()); @@ -383,16 +382,16 @@ await fsRoutes(app, { if (import.meta.main) { await app.listen(); -} -`; +}`; await writeFile("main.ts", MAIN_TS); - const COMPONENTS_BUTTON_TSX = `import { ComponentChildren } from "preact"; + const COMPONENTS_BUTTON_TSX = + `import type { ComponentChildren } from "preact"; export interface ButtonProps { onClick?: () => void; children?: ComponentChildren; - disabled?: boolean + disabled?: boolean; } export function Button(props: ButtonProps) { @@ -402,8 +401,7 @@ export function Button(props: ButtonProps) { class="px-2 py-1 border-gray-500 border-2 rounded bg-white hover:bg-gray-200 transition-colors" /> ); -} -`; +}`; await writeFile("components/Button.tsx", COMPONENTS_BUTTON_TSX); const UTILS_TS = `import { createDefine } from "fresh"; @@ -411,8 +409,7 @@ export function Button(props: ButtonProps) { // deno-lint-ignore no-empty-interface export interface State {} -export const define = createDefine(); -`; +export const define = createDefine();`; await writeFile("utils.ts", UTILS_TS); const ROUTES_HOME = `import { useSignal } from "@preact/signals"; @@ -441,10 +438,10 @@ export default define.page(function Home() { ); -})`; +});`; await writeFile("routes/index.tsx", ROUTES_HOME); - const APP_WRAPPER = `import { type PageProps } from "fresh"; + const APP_WRAPPER = `import type { PageProps } from "fresh"; export default function App({ Component }: PageProps) { return ( @@ -478,24 +475,21 @@ export default function Counter(props: CounterProps) { ); -} -`; +}`; await writeFile("islands/Counter.tsx", ISLANDS_COUNTER_TSX); const DEV_TS = `#!/usr/bin/env -S deno run -A --watch=static/,routes/ -${useTailwind ? `import { tailwind } from "@fresh/plugin-tailwind";\n` : ""}; +${useTailwind ? `import { tailwind } from "@fresh/plugin-tailwind";\n` : ""} import { Builder } from "fresh/dev"; import { app } from "./main.ts"; const builder = new Builder(); -${useTailwind ? "tailwind(builder, app, {});\n" : "\n"} - +${useTailwind ? "tailwind(builder, app, {});" : ""} if (Deno.args.includes("build")) { await builder.build(app); } else { await builder.listen(app); -} -`; +}`; await writeFile("dev.ts", DEV_TS); const denoJson = { @@ -557,8 +551,7 @@ Then start the project in development mode: deno task dev \`\`\` -This will watch the project directory and restart as necessary. -`; +This will watch the project directory and restart as necessary.`; await writeFile("README.md", README_MD); if (useVSCode) { diff --git a/init/src/init_test.ts b/init/src/init_test.ts index 695c0f5c..ebba6232 100644 --- a/init/src/init_test.ts +++ b/init/src/init_test.ts @@ -31,9 +31,11 @@ async function patchProject(dir: string): Promise { json.imports = rootJson.imports; json.imports["fresh"] = "../src/mod.ts"; json.imports["fresh/dev"] = "../src/dev/mod.ts"; - json.imports["@fresh/plugin-tailwind"] = "../plugin-tailwindcss/mod.ts"; + json.imports["@fresh/plugin-tailwind"] = "../plugin-tailwindcss/src/mod.ts"; + // assert with this stricter rule, before adding it to initialized projects + json.lint.rules.include = ["verbatim-module-syntax"]; - await Deno.writeTextFile(jsonPath, JSON.stringify(json, null, 2)); + await Deno.writeTextFile(jsonPath, JSON.stringify(json, null, 2) + "\n"); } function mockUserInput(steps: Record) { @@ -125,7 +127,7 @@ Deno.test("init - with vscode", async () => { }); }); -Deno.test("init - type check project", async () => { +Deno.test("init - fmt, lint, and type check project", async () => { await withTmpDir(async (dir) => { const mock = mockUserInput({ [InitStep.ProjectName]: ".", @@ -137,7 +139,29 @@ Deno.test("init - type check project", async () => { await patchProject(dir); const check = await new Deno.Command(Deno.execPath(), { - args: ["check", "main.ts", "dev.ts"], + args: ["task", "check"], + cwd: dir, + stderr: "inherit", + stdout: "inherit", + }).output(); + expect(check.code).toEqual(0); + }); +}); + +Deno.test("init with tailwind - fmt, lint, and type check project", async () => { + await withTmpDir(async (dir) => { + const mock = mockUserInput({ + [InitStep.ProjectName]: ".", + [InitStep.Tailwind]: true, + }); + await initProject(dir, [], {}, mock.tty); + await expectProjectFile(dir, "main.ts"); + await expectProjectFile(dir, "dev.ts"); + + await patchProject(dir); + + const check = await new Deno.Command(Deno.execPath(), { + args: ["task", "check"], cwd: dir, stderr: "inherit", stdout: "inherit",