-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Expand file tree
/
Copy pathgenerate-metadata.ts
More file actions
32 lines (28 loc) · 1.28 KB
/
generate-metadata.ts
File metadata and controls
32 lines (28 loc) · 1.28 KB
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
/**
* This file is responsible for generating Nest.js metadata for the API.
* Metadata generation is required when using SWC with Nest.js due to SWC
* not natively supporting Typescript, which is required to use the `reflect-metadata`
* API and in turn, resolve types for the OpenAPI specification.
*
* @see https://docs.nestjs.com/recipes/swc#monorepo-and-cli-plugins
*/
import fs from 'node:fs';
import path from 'node:path';
import { PluginMetadataGenerator } from '@nestjs/cli/lib/compiler/plugins/plugin-metadata-generator';
import { ReadonlyVisitor } from '@nestjs/swagger/dist/plugin';
const tsconfigPath = 'tsconfig.build.json';
const srcPath = path.join(__dirname, '..', 'src');
const metadataPath = path.join(srcPath, 'metadata.ts');
/*
* We create an empty metadata file to ensure that files importing `metadata.ts`
* will compile successfully before the metadata generation occurs.
*/
const defaultContent = `export default async () => { return {}; };`;
fs.writeFileSync(metadataPath, defaultContent, 'utf8');
console.log('metadata.ts file has been generated with default content.');
const generator = new PluginMetadataGenerator();
generator.generate({
visitors: [new ReadonlyVisitor({ introspectComments: true, pathToSource: srcPath })],
outputDir: srcPath,
tsconfigPath,
});