chore(server): add import config script (#11242)
This commit is contained in:
@@ -1,11 +1,18 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
|
|
||||||
import { AppModule as BusinessAppModule } from '../app.module';
|
import { FunctionalityModules } from '../app.module';
|
||||||
import { CreateCommand, NameQuestion } from './commands/create';
|
import { CreateCommand, NameQuestion } from './commands/create';
|
||||||
|
import { ImportConfigCommand } from './commands/import';
|
||||||
import { RevertCommand, RunCommand } from './commands/run';
|
import { RevertCommand, RunCommand } from './commands/run';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [BusinessAppModule],
|
imports: FunctionalityModules,
|
||||||
providers: [NameQuestion, CreateCommand, RunCommand, RevertCommand],
|
providers: [
|
||||||
|
NameQuestion,
|
||||||
|
CreateCommand,
|
||||||
|
RunCommand,
|
||||||
|
RevertCommand,
|
||||||
|
ImportConfigCommand,
|
||||||
|
],
|
||||||
})
|
})
|
||||||
export class CliAppModule {}
|
export class CliAppModule {}
|
||||||
|
|||||||
58
packages/backend/server/src/data/commands/import.ts
Normal file
58
packages/backend/server/src/data/commands/import.ts
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user