From c76f15479f77147de6389c9a3f2412479e79ac76 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Mon, 18 Sep 2023 17:46:34 +0200 Subject: [PATCH] feat: add plugin buildStart and buildEnd hooks (#1770) --- src/dev/dev_command.ts | 8 ++- src/server/types.ts | 9 ++++ tests/cli_update_check_test.ts | 15 +----- tests/fixture_plugin_lifecycle/deno.json | 13 +++++ tests/fixture_plugin_lifecycle/dev.ts | 6 +++ .../fixture_plugin_lifecycle/fresh.config.ts | 24 +++++++++ tests/fixture_plugin_lifecycle/fresh.gen.ts | 15 ++++++ tests/fixture_plugin_lifecycle/main.ts | 11 ++++ .../fixture_plugin_lifecycle/routes/index.tsx | 7 +++ tests/plugin_test.ts | 18 ++++++- tests/test_utils.ts | 50 +++++++++++++++++++ 11 files changed, 161 insertions(+), 15 deletions(-) create mode 100644 tests/fixture_plugin_lifecycle/deno.json create mode 100755 tests/fixture_plugin_lifecycle/dev.ts create mode 100644 tests/fixture_plugin_lifecycle/fresh.config.ts create mode 100644 tests/fixture_plugin_lifecycle/fresh.gen.ts create mode 100644 tests/fixture_plugin_lifecycle/main.ts create mode 100644 tests/fixture_plugin_lifecycle/routes/index.tsx diff --git a/src/dev/dev_command.ts b/src/dev/dev_command.ts index 2dd1ee38..b22a890c 100644 --- a/src/dev/dev_command.ts +++ b/src/dev/dev_command.ts @@ -46,7 +46,13 @@ export async function dev( if (isBuild) { // Ensure that build dir is empty await fs.emptyDir(outDir); - await build(join(dir, "fresh.gen.ts"), options ?? {}); + + options = options ?? {}; + const plugins = options.plugins ?? []; + + await Promise.all(plugins.map((plugin) => plugin.buildStart?.())); + await build(join(dir, "fresh.gen.ts"), options); + await Promise.all(plugins.map((plugin) => plugin.buildEnd?.())); } else if (options) { const ctx = await ServerContext.fromManifest(manifest, { ...options, diff --git a/src/server/types.ts b/src/server/types.ts index 54f7a5ee..2f8598ed 100644 --- a/src/server/types.ts +++ b/src/server/types.ts @@ -427,6 +427,15 @@ export interface Plugin> { */ renderAsync?(ctx: PluginAsyncRenderContext): Promise; + /** + * Called before running the Fresh build task + */ + buildStart?(): Promise | void; + /** + * Called after completing the Fresh build task + */ + buildEnd?(): Promise | void; + routes?: PluginRoute[]; middlewares?: PluginMiddleware[]; diff --git a/tests/cli_update_check_test.ts b/tests/cli_update_check_test.ts index c1e32f24..1ce4158c 100644 --- a/tests/cli_update_check_test.ts +++ b/tests/cli_update_check_test.ts @@ -1,4 +1,4 @@ -import { colors, join } from "../src/server/deps.ts"; +import { join } from "../src/server/deps.ts"; import { assert, assertEquals, @@ -9,18 +9,7 @@ import { import versions from "../versions.json" assert { type: "json" }; import { CheckFile } from "$fresh/src/dev/update_check.ts"; import { WEEK } from "$fresh/src/dev/deps.ts"; - -function getStdOutput( - out: Deno.CommandOutput, -): { stdout: string; stderr: string } { - const decoder = new TextDecoder(); - const stdout = colors.stripColor(decoder.decode(out.stdout)); - - const decoderErr = new TextDecoder(); - const stderr = colors.stripColor(decoderErr.decode(out.stderr)); - - return { stdout, stderr }; -} +import { getStdOutput } from "$fresh/tests/test_utils.ts"; Deno.test({ name: "stores update check file in $HOME/fresh", diff --git a/tests/fixture_plugin_lifecycle/deno.json b/tests/fixture_plugin_lifecycle/deno.json new file mode 100644 index 00000000..2b2fa78e --- /dev/null +++ b/tests/fixture_plugin_lifecycle/deno.json @@ -0,0 +1,13 @@ +{ + "lock": false, + "imports": { + "$fresh/": "../../", + "preact": "https://esm.sh/preact@10.15.1", + "preact/": "https://esm.sh/preact@10.15.1/", + "preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.1" + }, + "compilerOptions": { + "jsx": "react-jsx", + "jsxImportSource": "preact" + } +} diff --git a/tests/fixture_plugin_lifecycle/dev.ts b/tests/fixture_plugin_lifecycle/dev.ts new file mode 100755 index 00000000..1fe3e340 --- /dev/null +++ b/tests/fixture_plugin_lifecycle/dev.ts @@ -0,0 +1,6 @@ +#!/usr/bin/env -S deno run -A --watch=static/,routes/ + +import dev from "$fresh/dev.ts"; +import config from "./fresh.config.ts"; + +await dev(import.meta.url, "./main.ts", config); diff --git a/tests/fixture_plugin_lifecycle/fresh.config.ts b/tests/fixture_plugin_lifecycle/fresh.config.ts new file mode 100644 index 00000000..1a9b40d0 --- /dev/null +++ b/tests/fixture_plugin_lifecycle/fresh.config.ts @@ -0,0 +1,24 @@ +import { defineConfig } from "$fresh/src/server/defines.ts"; + +export default defineConfig({ + plugins: [ + { + name: "a", + buildEnd() { + console.log("Plugin a: buildEnd"); + }, + buildStart() { + console.log("Plugin a: buildStart"); + }, + }, + { + name: "b", + buildEnd() { + console.log("Plugin b: buildEnd"); + }, + buildStart() { + console.log("Plugin b: buildStart"); + }, + }, + ], +}); diff --git a/tests/fixture_plugin_lifecycle/fresh.gen.ts b/tests/fixture_plugin_lifecycle/fresh.gen.ts new file mode 100644 index 00000000..b321c0e0 --- /dev/null +++ b/tests/fixture_plugin_lifecycle/fresh.gen.ts @@ -0,0 +1,15 @@ +// DO NOT EDIT. This file is generated by Fresh. +// This file SHOULD be checked into source version control. +// This file is automatically updated during development when running `dev.ts`. + +import * as $0 from "./routes/index.tsx"; + +const manifest = { + routes: { + "./routes/index.tsx": $0, + }, + islands: {}, + baseUrl: import.meta.url, +}; + +export default manifest; diff --git a/tests/fixture_plugin_lifecycle/main.ts b/tests/fixture_plugin_lifecycle/main.ts new file mode 100644 index 00000000..fc935921 --- /dev/null +++ b/tests/fixture_plugin_lifecycle/main.ts @@ -0,0 +1,11 @@ +/// +/// +/// +/// +/// + +import { start } from "$fresh/server.ts"; +import manifest from "./fresh.gen.ts"; +import config from "./fresh.config.ts"; + +await start(manifest, config); diff --git a/tests/fixture_plugin_lifecycle/routes/index.tsx b/tests/fixture_plugin_lifecycle/routes/index.tsx new file mode 100644 index 00000000..b42a1f58 --- /dev/null +++ b/tests/fixture_plugin_lifecycle/routes/index.tsx @@ -0,0 +1,7 @@ +export default function Home() { + return ( +
+

Hello World

+
+ ); +} diff --git a/tests/plugin_test.ts b/tests/plugin_test.ts index 8f4d298c..340331f2 100644 --- a/tests/plugin_test.ts +++ b/tests/plugin_test.ts @@ -8,7 +8,7 @@ import { } from "./deps.ts"; import manifest from "./fixture_plugin/fresh.gen.ts"; import options from "./fixture_plugin/options.ts"; -import { startFreshServer } from "./test_utils.ts"; +import { runBuild, startFreshServer } from "./test_utils.ts"; const ctx = await ServerContext.fromManifest(manifest, options); const handler = ctx.handler(); @@ -129,3 +129,19 @@ Deno.test({ for await (const _ of lines) { /* noop */ } }, }); + +Deno.test("calls buildStart() and buildEnd()", async () => { + const result = await runBuild("./tests/fixture_plugin_lifecycle/dev.ts"); + + let out = result.stdout.split("\n"); + const idx = out.findIndex((line) => line.startsWith("Plugin")); + const idxEnd = out.findLastIndex((line) => line.startsWith("Plugin")); + out = out.slice(idx, idxEnd + 1); + + assertEquals(out, [ + "Plugin a: buildStart", + "Plugin b: buildStart", + "Plugin a: buildEnd", + "Plugin b: buildEnd", + ]); +}); diff --git a/tests/test_utils.ts b/tests/test_utils.ts index a7e25ce2..486b6dce 100644 --- a/tests/test_utils.ts +++ b/tests/test_utils.ts @@ -1,4 +1,6 @@ import { colors } from "$fresh/src/server/deps.ts"; +import { assert } from "$std/_util/asserts.ts"; +import * as path from "$std/path/mod.ts"; import { assertEquals, delay, @@ -345,3 +347,51 @@ async function spawnServer( return { serverProcess, lines, address, output }; } + +export async function runBuild(fixture: string) { + const outDir = path.join(path.dirname(fixture), "_fresh"); + try { + await Deno.remove(outDir, { recursive: true }); + } catch { + // Ignore + } + + assert( + fixture.endsWith("dev.ts"), + `Build command only works with "dev.ts", but got "${fixture}" instead`, + ); + const res = await new Deno.Command(Deno.execPath(), { + args: [ + "run", + "-A", + fixture, + "build", + ], + env: { + GITHUB_SHA: "__BUILD_ID__", + DENO_DEPLOYMENT_ID: "__BUILD_ID__", + }, + stdin: "null", + stdout: "piped", + stderr: "piped", + }).output(); + + const output = getStdOutput(res); + return { + code: res.code, + stderr: output.stderr, + stdout: output.stdout, + }; +} + +export function getStdOutput( + out: Deno.CommandOutput, +): { stdout: string; stderr: string } { + const decoder = new TextDecoder(); + const stdout = colors.stripColor(decoder.decode(out.stdout)); + + const decoderErr = new TextDecoder(); + const stderr = colors.stripColor(decoderErr.decode(out.stderr)); + + return { stdout, stderr }; +}