chore(server): add import config script (#11242)

This commit is contained in:
forehalo
2025-03-27 12:32:29 +00:00
parent 0ea38680fa
commit 504a74d512
2 changed files with 68 additions and 3 deletions

View File

@@ -1,11 +1,18 @@
import { Module } from '@nestjs/common';
import { AppModule as BusinessAppModule } from '../app.module';
import { FunctionalityModules } from '../app.module';
import { CreateCommand, NameQuestion } from './commands/create';
import { ImportConfigCommand } from './commands/import';
import { RevertCommand, RunCommand } from './commands/run';
@Module({
imports: [BusinessAppModule],
providers: [NameQuestion, CreateCommand, RunCommand, RevertCommand],
imports: FunctionalityModules,
providers: [
NameQuestion,
CreateCommand,
RunCommand,
RevertCommand,
ImportConfigCommand,
],
})
export class CliAppModule {}

View File

@@ -0,0 +1,58 @@
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { Logger } from '@nestjs/common';
import { Command, CommandRunner } from 'nest-commander';
import { ConfigFactory } from '../../base';
import { Models } from '../../models';
@Command({
name: 'import-config',
arguments: '[name]',
description: 'import config from a file',
})
export class ImportConfigCommand extends CommandRunner {
logger = new Logger(ImportConfigCommand.name);
constructor(
private readonly models: Models,
private readonly configFactory: ConfigFactory
) {
super();
}
override async run(inputs: string[]): Promise<void> {
let path = inputs[0];
path = resolve(process.cwd(), path);
const overrides: Record<string, Record<string, any>> = JSON.parse(
readFileSync(path, 'utf-8')
);
const forValidation: { module: string; key: string; value: any }[] = [];
const forSaving: { key: string; value: any }[] = [];
Object.entries(overrides).forEach(([module, config]) => {
if (module === '$schema') {
return;
}
Object.entries(config).forEach(([key, value]) => {
forValidation.push({
module,
key,
value,
});
forSaving.push({
key: `${module}.${key}`,
value,
});
});
});
this.configFactory.validate(forValidation);
// @ts-expect-error null as user id
await this.models.appConfig.save(null, forSaving);
}
}