-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(theme): create an standalone module
- Loading branch information
Showing
8 changed files
with
138 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { Controller } from '@nestjs/common'; | ||
|
||
@Controller('theme') | ||
export class ThemeController {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ThemeController } from './theme.controller'; | ||
import { ThemeService } from './theme.service'; | ||
|
||
@Module({ | ||
controllers: [ThemeController], | ||
providers: [ThemeService] | ||
}) | ||
export class ThemeModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { BadRequestException, Injectable, Logger } from '@nestjs/common'; | ||
import { YAML } from 'zx-cjs'; | ||
import { THEME_DIR } from '~/constants/path.constant'; | ||
import { ThemeDto } from '../configs/configs.dto'; | ||
import { ConfigsService } from '../configs/configs.service'; | ||
|
||
@Injectable() | ||
export class ThemeService { | ||
private logger: Logger; | ||
constructor( | ||
private readonly configsService: ConfigsService, | ||
|
||
) { | ||
this.logger = new Logger(ThemeService.name) | ||
} | ||
|
||
|
||
CORE_VERSION = require('../../../package.json').version | ||
|
||
private async turnOnThemeLibs(name: string) { | ||
// 查找配置文件是否存在 | ||
const themeConfigFile = await fs | ||
.readFile(`${THEME_DIR}/${name}/theme.yaml`, "utf8") | ||
.catch(() => { | ||
throw new BadRequestException(`主题 ${name} 配置文件不存在`); | ||
}) | ||
|
||
const themeConfig = YAML.parse(themeConfigFile); // 解析配置文件 | ||
|
||
if (themeConfig.name !== name) { // 严格按照主题名称来配置,考虑大小写 | ||
throw new BadRequestException(`主题 ${name} 配置文件名称不匹配`); | ||
} | ||
|
||
// 检查主题是否适合当前后端的版本 | ||
if ( | ||
this.CORE_VERSION > themeConfig.support_min_version && | ||
this.CORE_VERSION < themeConfig.support_max_version | ||
) { | ||
// 提醒建议使用的版本 | ||
this.CORE_VERSION !== themeConfig.recommend_version && this.logger.warn(`主题 ${name} 建议使用版本 ${themeConfig.recommend_version}`) | ||
} else { | ||
throw new BadRequestException(`主题 ${name} 不支持当前版本`); | ||
} | ||
|
||
return { | ||
themeConfig, | ||
} | ||
} | ||
|
||
/** | ||
* turnOnTheme 启动主题 | ||
*/ | ||
async turnOnTheme(name: string) { | ||
if (fs.existsSync(path.join(THEME_DIR, name))) { // 检查主题是否存在 | ||
|
||
const { themeConfig } = await this.turnOnThemeLibs(name); // 获取主题配置 | ||
const theme: ThemeDto = { // 创建主题对象 | ||
name, | ||
enable: true, | ||
configs: themeConfig.configs ? themeConfig.configs : {}, | ||
viewExt: themeConfig.viewExt || 'art-template', | ||
} | ||
await this.configsService.patch('theme', theme); // 更新主题配置 | ||
return `主题 ${name} 启动成功`; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { beforeEach, expect, describe, it } from "vitest"; | ||
import { ThemeController } from '~/modules/theme/theme.controller'; | ||
|
||
describe('ThemeController', () => { | ||
let controller: ThemeController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [ThemeController], | ||
}).compile(); | ||
|
||
controller = module.get<ThemeController>(ThemeController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { beforeEach, expect, describe, it } from "vitest"; | ||
import { ThemeService } from '~/modules/theme/theme.service'; | ||
|
||
describe('ThemeService', () => { | ||
let service: ThemeService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ThemeService], | ||
}).compile(); | ||
|
||
service = module.get<ThemeService>(ThemeService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |