prevision / apps /prevision-api /src /scripts /seed-catalog.ts
github-actions[bot]
Automated Deploy: 6d2e55112e3ada832d411bd3301a883ce9eba3a9
c7a38a3
Raw History Blame Contribute Delete
1.49 kB
import 'dotenv/config';
import { resolve } from 'node:path';
import { parseArgs } from 'node:util';
import { NestFactory } from '@nestjs/core';
import { adminCatalogProblems } from '../catalog/admin-catalog.js';
import { CatalogModule } from '../catalog/catalog.module.js';
import { CatalogService } from '../catalog/catalog.service.js';
// `seed-catalog` seeds prisma/catalog.json (run on every start).
// `seed-catalog --file <path> --require-visibility admin` imports an admin-only supplier catalog explicitly.
const { values } = parseArgs({ options: { file: { type: 'string' }, 'require-visibility': { type: 'string' } } });
if (values['require-visibility'] && values['require-visibility'] !== 'admin') {
console.error('Usage: seed-catalog [--file <path> --require-visibility admin]');
process.exit(1);
}
const app = await NestFactory.createApplicationContext(CatalogModule, { logger: ['error'] });
try {
const catalog = app.get(CatalogService);
const entries = await catalog.load(values.file ? resolve(values.file) : undefined);
if (values['require-visibility']) {
const problems = adminCatalogProblems(entries, await catalog.load());
if (problems.length) throw new Error(`catalog: refusing to import\n ${problems.join('\n ')}`);
}
console.log(`catalog: ${await catalog.seed(entries)} tiles inserted or updated`);
} catch (error) {
console.error(error instanceof Error ? error.message : error);
process.exitCode = 1;
} finally {
await app.close();
}