Compare commits
1
Commits
v2.2.0
...
04abdc39ee
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
04abdc39ee |
@@ -218,7 +218,10 @@ nvm use --silent
|
|||||||
npm run openapi:generate
|
npm run openapi:generate
|
||||||
```
|
```
|
||||||
|
|
||||||
Команда собирает backend и записывает `openapi.json` в корень репозитория.
|
Команда собирает backend и записывает `openapi.json` в корень репозитория
|
||||||
|
атомарно: через временный файл и rename, без частичной записи при сбое.
|
||||||
|
`npm run openapi:check` только сравнивает runtime schema с файлом и завершается
|
||||||
|
с ошибкой при расхождении, не перезаписывая его.
|
||||||
Сервер и HTTP-порт не запускаются; Nest preview читает metadata без создания
|
Сервер и HTTP-порт не запускаются; Nest preview читает metadata без создания
|
||||||
провайдеров и подключения к БД/T-Bank. Настроенный `.env` и реальные секреты
|
провайдеров и подключения к БД/T-Bank. Настроенный `.env` и реальные секреты
|
||||||
не нужны. `openapi.json` исключён из Git и каждый раз создаётся из DTO/Swagger
|
не нужны. `openapi.json` исключён из Git и каждый раз создаётся из DTO/Swagger
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "prisma generate && nest build",
|
"build": "prisma generate && nest build",
|
||||||
"openapi:generate": "npm run build && node scripts/generate-openapi.cjs",
|
"openapi:generate": "npm run build && node scripts/generate-openapi.cjs",
|
||||||
|
"openapi:check": "npm run build && node scripts/generate-openapi.cjs --check",
|
||||||
"format": "npm run lint:fix",
|
"format": "npm run lint:fix",
|
||||||
"start": "nest start",
|
"start": "nest start",
|
||||||
"start:dev": "NODE_ENV=development nest start --watch --path tsconfig.dev.json",
|
"start:dev": "NODE_ENV=development nest start --watch --path tsconfig.dev.json",
|
||||||
|
|||||||
@@ -1,5 +1,10 @@
|
|||||||
const { randomBytes } = require('node:crypto');
|
const { randomBytes } = require('node:crypto');
|
||||||
const { writeFile } = require('node:fs/promises');
|
const { readFile, rename, rm, writeFile } = require('node:fs/promises');
|
||||||
|
const { resolve } = require('node:path');
|
||||||
|
|
||||||
|
// Relative to the working directory, as before: the contract test runs the script in a temporary directory.
|
||||||
|
const target = resolve(process.cwd(), 'openapi.json');
|
||||||
|
const checkOnly = process.argv.includes('--check');
|
||||||
|
|
||||||
// Schema generation needs no deployment configuration or real credentials.
|
// Schema generation needs no deployment configuration or real credentials.
|
||||||
Object.assign(process.env, {
|
Object.assign(process.env, {
|
||||||
@@ -19,23 +24,48 @@ const { NestFactory } = require('@nestjs/core');
|
|||||||
const { AppModule } = require('../dist/src/app.module.js');
|
const { AppModule } = require('../dist/src/app.module.js');
|
||||||
const { API_PREFIX, createOpenApiDocument } = require('../dist/src/openapi.js');
|
const { API_PREFIX, createOpenApiDocument } = require('../dist/src/openapi.js');
|
||||||
|
|
||||||
async function generate() {
|
async function render() {
|
||||||
const app = await NestFactory.create(AppModule, {
|
const app = await NestFactory.create(AppModule, {
|
||||||
preview: true,
|
preview: true,
|
||||||
logger: false,
|
logger: false,
|
||||||
});
|
});
|
||||||
try {
|
try {
|
||||||
app.setGlobalPrefix(API_PREFIX);
|
app.setGlobalPrefix(API_PREFIX);
|
||||||
await writeFile(
|
return JSON.stringify(createOpenApiDocument(app), null, 2) + '\n';
|
||||||
'openapi.json',
|
|
||||||
JSON.stringify(createOpenApiDocument(app), null, 2) + '\n',
|
|
||||||
);
|
|
||||||
} finally {
|
} finally {
|
||||||
await app.close();
|
await app.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
generate().catch((error) => {
|
// Read-only: compares the runtime document with the committed artifact and never writes.
|
||||||
console.error(error);
|
async function check(content) {
|
||||||
process.exitCode = 1;
|
const current = await readFile(target, 'utf8').catch(() => null);
|
||||||
});
|
if (current !== content) {
|
||||||
|
console.error(
|
||||||
|
'openapi.json is out of date with the runtime schema. Run `npm run openapi:generate`.',
|
||||||
|
);
|
||||||
|
process.exitCode = 1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
console.log('openapi.json matches the runtime schema.');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Atomic update: write a sibling temp file, then rename over the target, so a failed run never leaves partial output.
|
||||||
|
async function update(content) {
|
||||||
|
const temp = `${target}.${process.pid}.tmp`;
|
||||||
|
try {
|
||||||
|
// eslint-disable-next-line security/detect-non-literal-fs-filename -- Operator-selected working directory and fixed artifact name; no request input.
|
||||||
|
await writeFile(temp, content);
|
||||||
|
// eslint-disable-next-line security/detect-non-literal-fs-filename -- Operator-selected working directory and fixed artifact name; no request input.
|
||||||
|
await rename(temp, target);
|
||||||
|
} finally {
|
||||||
|
await rm(temp, { force: true });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
render()
|
||||||
|
.then((content) => (checkOnly ? check(content) : update(content)))
|
||||||
|
.catch((error) => {
|
||||||
|
console.error(error);
|
||||||
|
process.exitCode = 1;
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user