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.
This commit is contained in:
Reed von Redwitz
2024-06-23 17:44:07 +02:00
committed by GitHub
parent febbc52727
commit 5a7184ad99
2 changed files with 45 additions and 28 deletions
+17 -24
View File
@@ -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<State>();
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<State>();
`;
export const define = createDefine<State>();`;
await writeFile("utils.ts", UTILS_TS);
const ROUTES_HOME = `import { useSignal } from "@preact/signals";
@@ -441,10 +438,10 @@ export default define.page(function Home() {
</div>
</div>
);
})`;
});`;
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) {
<Button onClick={() => props.count.value += 1}>+1</Button>
</div>
);
}
`;
}`;
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) {
+28 -4
View File
@@ -31,9 +31,11 @@ async function patchProject(dir: string): Promise<void> {
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<string, unknown>) {
@@ -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",