Skip to content

Commit 1692097

Browse files
committed
Wire Nuxt init logging
Generate a Nuxt-specific logging template that exports the LogTape configuration promise without top-level await. Add a Nitro server plugin to await that promise during startup so generated Nuxt apps actually enable Fedify logging. Allow framework initializers to override the default logging template and cover the Nuxt wiring with a generated-file regression test. Fixes #724 Assisted-by: Codex:gpt-5.5
1 parent 1764300 commit 1692097

6 files changed

Lines changed: 94 additions & 2 deletions

File tree

packages/init/src/action/configs.test.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { message } from "@optique/core";
77
import { kvStores, messageQueues } from "../lib.ts";
88
import type { InitCommandData } from "../types.ts";
99
import bareBonesDescription from "../webframeworks/bare-bones.ts";
10+
import nuxtDescription from "../webframeworks/nuxt.ts";
1011
import { loadDenoConfig } from "./configs.ts";
1112
import { patchFiles } from "./patch.ts";
1213

@@ -133,6 +134,28 @@ test("patchFiles creates a Biome config matching the npm package version", async
133134
}
134135
});
135136

137+
test("patchFiles wires Nuxt logging through a Nitro plugin", async () => {
138+
const dir = await mkdtemp(join(tmpdir(), "fedify-init-nuxt-"));
139+
140+
try {
141+
const data = await createNuxtNpmInitData(dir);
142+
await patchFiles(data);
143+
144+
const logging = await readFile(join(dir, "server/logging.ts"), "utf8");
145+
const plugin = await readFile(
146+
join(dir, "server/plugins/logging.ts"),
147+
"utf8",
148+
);
149+
150+
assert.match(logging, /export default configure\(/);
151+
assert.doesNotMatch(logging, /await configure\(/);
152+
assert.match(plugin, /import loggingConfigured from "\.\.\/logging";/);
153+
assert.match(plugin, /await loggingConfigured;/);
154+
} finally {
155+
await rm(dir, { recursive: true, force: true });
156+
}
157+
});
158+
136159
async function createNpmInitData(dir: string): Promise<InitCommandData> {
137160
const initializer = await bareBonesDescription.init({
138161
command: "init",
@@ -166,6 +189,39 @@ async function createNpmInitData(dir: string): Promise<InitCommandData> {
166189
return data;
167190
}
168191

192+
async function createNuxtNpmInitData(dir: string): Promise<InitCommandData> {
193+
const initializer = await nuxtDescription.init({
194+
command: "init",
195+
projectName: "example",
196+
packageManager: "npm",
197+
webFramework: "nuxt",
198+
kvStore: "in-memory",
199+
messageQueue: "in-process",
200+
dryRun: false,
201+
allowNonEmpty: false,
202+
testMode: false,
203+
dir,
204+
});
205+
206+
const data = {
207+
command: "init",
208+
projectName: "example",
209+
packageManager: "npm",
210+
webFramework: "nuxt",
211+
kvStore: "in-memory",
212+
messageQueue: "in-process",
213+
dryRun: false,
214+
allowNonEmpty: false,
215+
testMode: false,
216+
dir,
217+
initializer,
218+
kv: kvStores["in-memory"],
219+
mq: messageQueues["in-process"],
220+
env: {},
221+
} satisfies InitCommandData;
222+
return data;
223+
}
224+
169225
function getSchemaVersion(schema: string): string {
170226
const match = schema.match(/\/schemas\/(\d+\.\d+\.\d+)\//);
171227
assert.ok(match, `Unexpected Biome schema URL: ${schema}`);

packages/init/src/action/templates.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ export const loadFederation = async (
3838
* @param param0 - Destructured object containing the project name
3939
* @returns The complete logging configuration file content as a string
4040
*/
41-
export const loadLogging = async ({ projectName }: InitCommandData) =>
41+
export const loadLogging = async (
42+
{ projectName, initializer }: InitCommandData,
43+
) =>
4244
pipe(
43-
await readTemplate("defaults/logging.ts"),
45+
await readTemplate(initializer.loggingTemplate ?? "defaults/logging.ts"),
4446
replace(/\/\* project name \*\//, JSON.stringify(projectName)),
4547
);
4648

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { configure, getConsoleSink } from "@logtape/logtape";
2+
import { AsyncLocalStorage } from "node:async_hooks";
3+
4+
export default configure({
5+
contextLocalStorage: new AsyncLocalStorage(),
6+
sinks: {
7+
console: getConsoleSink(),
8+
},
9+
filters: {},
10+
loggers: [
11+
{
12+
category: /* project name */,
13+
lowestLevel: "debug",
14+
sinks: ["console"],
15+
},
16+
{ category: "fedify", lowestLevel: "info", sinks: ["console"] },
17+
{
18+
category: ["logtape", "meta"],
19+
lowestLevel: "warning",
20+
sinks: ["console"],
21+
},
22+
],
23+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import loggingConfigured from "../logging";
2+
3+
export default defineNitroPlugin(async () => {
4+
await loggingConfigured;
5+
});

packages/init/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ export interface WebFrameworkInitializer {
7878
federationFile: string;
7979
/** Relative path where the logging configuration file will be created. */
8080
loggingFile: string;
81+
/** Optional template path for the logging configuration file. */
82+
loggingTemplate?: string;
8183
/**
8284
* Additional files to create, keyed by relative path to file content.
8385
* Do not use `".env"` as a key — use the {@link env} property instead so

packages/init/src/webframeworks/nuxt.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ const nuxtDescription: WebFrameworkDescription = {
1919
},
2020
federationFile: "server/federation.ts",
2121
loggingFile: "server/logging.ts",
22+
loggingTemplate: "nuxt/server/logging.ts",
2223
env: testMode ? { HOST: "127.0.0.1" } : {} as Record<string, string>,
2324
files: {
2425
"nuxt.config.ts": await readTemplate("nuxt/nuxt.config.ts"),
26+
"server/plugins/logging.ts": await readTemplate(
27+
"nuxt/server/plugins/logging.ts",
28+
),
2529
...(pm !== "deno" && {
2630
"eslint.config.ts": await readTemplate("defaults/eslint.config.ts"),
2731
}),

0 commit comments

Comments
 (0)