mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 21:05:19 +00:00
chore(server): add import config script (#11242)
This commit is contained in:
@@ -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 {}
|
||||
|
||||
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