Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | 4x 4x 8x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 3x 3x 3x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import * as path from 'path';
import * as fs from 'fs';
import type { ParsedRoute } from '../routing/route-parser.js';
import type { ParsedSocket } from '../routing/socket-parser.js';
import type { RouteSchema } from '../types/schema.js';
import { extractBodyTypeFromFile, extractQueryTypeFromFile } from '../file-system/extract-type-from-file.js';
import { generateTypesFile, generateServicesFile } from '../types/generate.js';
import { generateSocketTypesFile } from '../types/generate-socket-types.js';
import { generateSocketServicesFile } from '../types/generate-socket-services.js';
import { generateOpenApiFile, generateApiDocsHtml, type OpenApiOptions } from '../openapi/generate.js';
import { generateAsyncApiFile } from '../asyncapi/generate.js';
import { socketsToSchema } from '../types/socket-schema.js';
import {
API_BASE_PATH,
GENERATED_TYPES_FILE,
GENERATED_SERVICES_FILE,
GENERATED_SOCKET_TYPES_FILE,
GENERATED_SOCKET_SERVICES_FILE,
} from '../../shared/constants.js';
function isTypeScriptProject(root: string): boolean {
const tsconfigPath = path.join(root, 'tsconfig.json');
return fs.existsSync(tsconfigPath);
}
export function parsedRoutesToSchema(parsedRoutes: ParsedRoute[]): RouteSchema[] {
return parsedRoutes.map((p) => ({
pattern: p.pattern,
method: p.method,
params: p.params,
file: p.file,
bodyType: extractBodyTypeFromFile(p.file),
queryType: extractQueryTypeFromFile(p.file),
}));
}
export interface RunFileGenerationOptions {
root: string;
schema: RouteSchema[];
sockets: ParsedSocket[];
apiBasePath?: string;
socketBasePath?: string;
openApi?: OpenApiOptions | boolean;
serverPort?: number;
logger?: {
typesGenerated?: (path: string) => void;
servicesGenerated?: (path: string) => void;
info?: (message: string) => void;
warn?: (message: string) => void;
};
/** Callback when generation fails (e.g. OpenAPI). Does not run for types/services write errors. */
onGenerationError?: (error: Error) => void;
}
export async function runFileGeneration(options: RunFileGenerationOptions): Promise<void> {
const {
root,
schema,
sockets,
apiBasePath = API_BASE_PATH,
socketBasePath = `${apiBasePath}/ws`,
openApi,
serverPort = 5173,
logger,
} = options;
const logTypes = logger?.typesGenerated ?? (() => {});
const logServices = logger?.servicesGenerated ?? (() => {});
const logInfo = logger?.info ?? (() => {});
const logWarn = logger?.warn ?? (() => {});
const onGenerationError = options.onGenerationError;
const isTypeScript = isTypeScriptProject(root);
const srcDir = path.join(root, 'src');
Eif (schema.length > 0) {
if (isTypeScript) {
const typesPath = path.join(srcDir, GENERATED_TYPES_FILE);
await generateTypesFile(typesPath, schema, apiBasePath);
logTypes(`./${path.relative(root, typesPath).replace(/\\/g, '/')}`);
}
const servicesFileName = isTypeScript ? GENERATED_SERVICES_FILE : 'api.services.js';
const servicesPath = path.join(srcDir, servicesFileName);
await generateServicesFile(servicesPath, schema, apiBasePath, isTypeScript);
logServices(`./${path.relative(root, servicesPath).replace(/\\/g, '/')}`);
}
Iif (sockets.length > 0) {
const socketSchema = socketsToSchema(sockets);
if (isTypeScript) {
const socketTypesPath = path.join(srcDir, GENERATED_SOCKET_TYPES_FILE);
await generateSocketTypesFile(socketTypesPath, socketSchema, socketBasePath);
logTypes(`./${path.relative(root, socketTypesPath).replace(/\\/g, '/')}`);
}
const socketServicesFileName = isTypeScript ? GENERATED_SOCKET_SERVICES_FILE : 'socket.services.js';
const socketServicesPath = path.join(srcDir, socketServicesFileName);
await generateSocketServicesFile(socketServicesPath, socketSchema, socketBasePath, isTypeScript);
logServices(`./${path.relative(root, socketServicesPath).replace(/\\/g, '/')}`);
}
if (openApi) {
try {
const openApiOptions: OpenApiOptions =
typeof openApi === 'boolean'
? { apiBasePath }
: { ...openApi, apiBasePath: openApi.apiBasePath ?? apiBasePath };
const publicDir = path.join(root, 'public');
Iif (!fs.existsSync(publicDir)) {
fs.mkdirSync(publicDir, { recursive: true });
}
const openApiPath = path.join(publicDir, 'openapi.json');
await generateOpenApiFile(openApiPath, schema, openApiOptions);
logInfo(`OpenAPI spec generated: ./${path.relative(root, openApiPath).replace(/\\/g, '/')}`);
Iif (sockets.length > 0) {
const asyncApiPath = path.join(publicDir, 'asyncapi.json');
await generateAsyncApiFile(asyncApiPath, sockets, socketBasePath, {
serverUrl: `ws://localhost:${serverPort}`,
});
logInfo(`AsyncAPI spec generated: ./${path.relative(root, asyncApiPath).replace(/\\/g, '/')}`);
}
const apiDocsPath = path.join(publicDir, 'api-docs.html');
const title = openApiOptions.info?.title ?? 'Vitek API';
const apiDocsHtml = generateApiDocsHtml('/openapi.json', title, {
asyncApiJsonPath: sockets.length > 0 ? '/asyncapi.json' : undefined,
});
fs.writeFileSync(apiDocsPath, apiDocsHtml, 'utf-8');
logInfo(
`API docs at: ./${path.relative(root, apiDocsPath).replace(/\\/g, '/')} → http://localhost:${serverPort}/api-docs.html`
);
} catch (error) {
const err = error instanceof Error ? error : new Error(String(error));
logWarn(`Failed to generate OpenAPI spec: ${err.message}`);
onGenerationError?.(err);
}
}
}
|