Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve nitro.json build info #1930

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
parseNodeModulePath,
resolvePath,
} from "mlly";
import { version as nitroVersion } from "../package.json";
import { generateFSTree } from "./utils/tree";
import { getRollupConfig, RollupConfig } from "./rollup/config";
import {
Expand All @@ -26,7 +27,7 @@ import {
resolvePath as resolveNitroPath,
} from "./utils";
import { GLOB_SCAN_PATTERN, scanHandlers } from "./scan";
import type { Nitro } from "./types";
import type { Nitro, NitroBuildInfo } from "./types";
import { runtimeDir } from "./dirs";
import { snapshotStorage } from "./storage";
import { compressPublicAssets } from "./compress";
Expand Down Expand Up @@ -405,17 +406,21 @@ async function _build(nitro: Nitro, rollupConfig: RollupConfig) {
await build.write(rollupConfig.output);
}

// Write build info
const nitroConfigPath = resolve(nitro.options.output.dir, "nitro.json");
const buildInfo = {
date: new Date(),
// Write .output/nitro.json
const buildInfoPath = resolve(nitro.options.output.dir, "nitro.json");
const buildInfo: NitroBuildInfo = {
date: new Date().toJSON(),
preset: nitro.options.preset,
framework: nitro.options.framework,
versions: {
nitro: nitroVersion,
},
commands: {
preview: nitro.options.commands.preview,
deploy: nitro.options.commands.deploy,
},
};
await writeFile(nitroConfigPath, JSON.stringify(buildInfo, null, 2));
await writeFile(buildInfoPath, JSON.stringify(buildInfo, null, 2));

if (!nitro.options.static) {
nitro.logger.success("Nitro server built");
Expand Down
27 changes: 22 additions & 5 deletions src/dev/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Worker } from "node:worker_threads";
import { existsSync, accessSync, promises as fsp } from "node:fs";
import { writeFile } from "node:fs/promises";
import { debounce } from "perfect-debounce";
import {
App,
Expand All @@ -17,13 +18,14 @@ import serveStatic from "serve-static";
import { resolve } from "pathe";
import { joinURL } from "ufo";
import { FSWatcher, watch } from "chokidar";
import type { Nitro } from "../types";
import type { Nitro, NitroBuildInfo } from "../types";
import { version as nitroVersion } from "../../package.json";
import { createVFSHandler } from "./vfs";
import defaultErrorHandler from "./error";

export interface NitroWorker {
worker: Worker;
address: { host: string; port: number; socketPath?: string };
address: { host: string; port: number } | { socketPath: string };
}

export interface NitroDevServer {
Expand Down Expand Up @@ -95,7 +97,7 @@ async function killWorker(worker: NitroWorker, nitro: Nitro) {
await worker.worker.terminate();
worker.worker = null;
}
if (worker.address.socketPath && existsSync(worker.address.socketPath)) {
if ("socketPath" in worker.address && existsSync(worker.address.socketPath)) {
await fsp.rm(worker.address.socketPath).catch(() => {});
}
}
Expand All @@ -122,6 +124,21 @@ export function createDevServer(nitro: Nitro): NitroDevServer {
await killWorker(oldWorker, nitro);
// Create a new worker
currentWorker = await initWorker(workerEntry);
// Write nitro.json
const buildInfoPath = resolve(nitro.options.buildDir, "nitro.json");
const buildInfo: NitroBuildInfo = {
date: new Date().toJSON(),
preset: nitro.options.preset,
framework: nitro.options.framework,
versions: {
nitro: nitroVersion,
},
dev: {
pid: process.pid,
workerAddress: currentWorker.address,
},
};
await writeFile(buildInfoPath, JSON.stringify(buildInfo, null, 2));
}
const reload = debounce(() => {
reloadPromise = _reload()
Expand Down Expand Up @@ -193,7 +210,7 @@ export function createDevServer(nitro: Nitro): NitroDevServer {
if (!address) {
return;
}
if (address.socketPath) {
if ("socketPath" in address) {
try {
accessSync(address.socketPath);
} catch (err) {
Expand All @@ -213,7 +230,7 @@ export function createDevServer(nitro: Nitro): NitroDevServer {
if (!address) {
return errorHandler(lastError, event);
}
await proxy.handle(event, { target: address }).catch((err) => {
await proxy.handle(event, { target: address as any }).catch((err) => {
lastError = err;
throw err;
});
Expand Down
31 changes: 26 additions & 5 deletions src/types/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,31 @@ export interface WasmOptions {
rollup?: RollupWasmOptions;
}

export interface NitroFrameworkInfo {
// eslint-disable-next-line @typescript-eslint/ban-types
name?: "nitro" | (string & {});
version?: string;
}

/** Build info written to `.output/nitro.json` or `.nitro/dev/nitro.json` */
export interface NitroBuildInfo {
date: string;
preset: string;
framework: NitroFrameworkInfo;
versions: {
nitro: string;
[key: string]: string;
};
commands?: {
preview?: string;
deploy?: string;
};
dev?: {
pid: number;
workerAddress: { host: string; port: number } | { socketPath: string };
};
}

export interface NitroOptions extends PresetOptions {
// Internal
_config: NitroConfig;
Expand Down Expand Up @@ -353,11 +378,7 @@ export interface NitroOptions extends PresetOptions {
};

// Framework
framework: {
// eslint-disable-next-line @typescript-eslint/ban-types
name?: "nitro" | (string & {});
version?: string;
};
framework: NitroFrameworkInfo;

// IIS
iis?: {
Expand Down