mirror of
https://github.com/cool-team-official/cool-admin-midway.git
synced 2025-12-10 16:12:50 +00:00
优化
This commit is contained in:
parent
3299b02362
commit
a66e7e176f
@ -1,4 +1,9 @@
|
||||
import { CoolController, BaseController, CoolTag, TagTypes } from '@cool-midway/core';
|
||||
import {
|
||||
CoolController,
|
||||
BaseController,
|
||||
CoolTag,
|
||||
TagTypes,
|
||||
} from '@cool-midway/core';
|
||||
import { Body, Get, Inject, Post } from '@midwayjs/core';
|
||||
import { BaseCodingService } from '../../service/coding';
|
||||
|
||||
@ -16,10 +21,14 @@ export class AdminCodingController extends BaseController {
|
||||
}
|
||||
|
||||
@Post('/createCode', { summary: '创建代码' })
|
||||
async createCode(@Body() codes: {
|
||||
path: string;
|
||||
content: string;
|
||||
}[]) {
|
||||
return this.baseCodingService.createCode(codes);
|
||||
async createCode(
|
||||
@Body('codes')
|
||||
codes: {
|
||||
path: string;
|
||||
content: string;
|
||||
}[]
|
||||
) {
|
||||
this.baseCodingService.createCode(codes);
|
||||
return this.ok();
|
||||
}
|
||||
}
|
||||
|
||||
@ -28,14 +28,14 @@ export class BaseCodingService extends BaseService {
|
||||
// 递归扫描目录函数
|
||||
const scanDir = (dir: string) => {
|
||||
const files = fs.readdirSync(dir);
|
||||
|
||||
|
||||
for (const file of files) {
|
||||
// 跳过 .DS_Store 文件
|
||||
if (file === '.DS_Store') continue;
|
||||
|
||||
const fullPath = path.join(dir, file);
|
||||
const stat = fs.statSync(fullPath);
|
||||
|
||||
|
||||
if (stat.isDirectory()) {
|
||||
scanDir(fullPath);
|
||||
} else {
|
||||
@ -62,23 +62,25 @@ export class BaseCodingService extends BaseService {
|
||||
* 创建代码
|
||||
* @param codes 代码
|
||||
*/
|
||||
async createCode(codes: {
|
||||
path: string;
|
||||
content: string;
|
||||
}[]) {
|
||||
async createCode(
|
||||
codes: {
|
||||
path: string;
|
||||
content: string;
|
||||
}[]
|
||||
) {
|
||||
if (this.app.getEnv() !== 'local') {
|
||||
throw new Error('只能在开发环境下创建代码');
|
||||
}
|
||||
|
||||
const moduleDir = await this.app.getBaseDir();
|
||||
const moduleDir = this.app.getAppDir();
|
||||
|
||||
for (const code of codes) {
|
||||
// 格式化代码内容
|
||||
const formattedContent = await this.formatContent(code.content);
|
||||
|
||||
|
||||
// 获取完整的文件路径
|
||||
const filePath = path.join(moduleDir, code.path);
|
||||
|
||||
|
||||
// 确保目录存在
|
||||
const dirPath = path.dirname(filePath);
|
||||
if (!fs.existsSync(dirPath)) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user