refactor: extract dependency module
This commit is contained in:
@@ -21,6 +21,7 @@ import { CommentModule } from './modules/comment/comment.module'
|
||||
import { ConfigsModule } from './modules/configs/configs.module'
|
||||
import { DebugModule } from './modules/debug/debug.module'
|
||||
import { DemoModule } from './modules/demo/demo.module'
|
||||
import { DependencyModule } from './modules/dependency/dependency.module'
|
||||
import { FeedModule } from './modules/feed/feed.module'
|
||||
import { FileModule } from './modules/file/file.module'
|
||||
import { HealthModule } from './modules/health/health.module'
|
||||
@@ -64,6 +65,7 @@ import { RedisModule } from './processors/redis/redis.module'
|
||||
CommentModule,
|
||||
ConfigsModule,
|
||||
isInDemoMode && DemoModule,
|
||||
DependencyModule,
|
||||
FeedModule,
|
||||
FileModule,
|
||||
HealthModule,
|
||||
|
||||
65
src/modules/dependency/dependency.controller.ts
Normal file
65
src/modules/dependency/dependency.controller.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { readFile } from 'fs/promises'
|
||||
import { Observable } from 'rxjs'
|
||||
|
||||
import { BadRequestException, Get, Query, Sse } from '@nestjs/common'
|
||||
|
||||
import { ApiController } from '~/common/decorator/api-controller.decorator'
|
||||
import { Auth } from '~/common/decorator/auth.decorator'
|
||||
import { RedisKeys } from '~/constants/cache.constant'
|
||||
import { DATA_DIR } from '~/constants/path.constant'
|
||||
import { CacheService } from '~/processors/redis/cache.service'
|
||||
import { getRedisKey, installPKG } from '~/utils'
|
||||
|
||||
@ApiController('dependencies')
|
||||
@Auth()
|
||||
export class DependencyController {
|
||||
constructor(private readonly redisService: CacheService) {}
|
||||
|
||||
@Get('/graph')
|
||||
async getDependencyGraph() {
|
||||
return (
|
||||
JSON.safeParse(
|
||||
await readFile(path.join(DATA_DIR, 'package.json'), 'utf8'),
|
||||
)?.dependencies || {}
|
||||
)
|
||||
}
|
||||
|
||||
@Sse('/install_deps')
|
||||
async installDepsPty(@Query() query: any): Promise<Observable<string>> {
|
||||
const { id } = query
|
||||
|
||||
if (!id) {
|
||||
throw new BadRequestException('id is required')
|
||||
}
|
||||
|
||||
const packageNames = await this.redisService
|
||||
.getClient()
|
||||
.hget(getRedisKey(RedisKeys.DependencyQueue), id)
|
||||
|
||||
if (!packageNames) {
|
||||
throw new BadRequestException('can not get this task')
|
||||
}
|
||||
// const packageNames = 'axios vue'
|
||||
|
||||
const pty = await installPKG(packageNames, DATA_DIR)
|
||||
const observable = new Observable<string>((subscriber) => {
|
||||
pty.onData((data) => {
|
||||
subscriber.next(data)
|
||||
})
|
||||
|
||||
pty.onExit(async ({ exitCode }) => {
|
||||
if (exitCode != 0) {
|
||||
subscriber.next(`Error: Exit code: ${exitCode}`)
|
||||
}
|
||||
|
||||
subscriber.next('任务完成,可关闭此窗口。')
|
||||
subscriber.complete()
|
||||
await this.redisService
|
||||
.getClient()
|
||||
.hdel(getRedisKey(RedisKeys.DependencyQueue), id)
|
||||
})
|
||||
})
|
||||
|
||||
return observable
|
||||
}
|
||||
}
|
||||
9
src/modules/dependency/dependency.module.ts
Normal file
9
src/modules/dependency/dependency.module.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common'
|
||||
|
||||
import { DependencyController } from './dependency.controller'
|
||||
|
||||
@Module({
|
||||
controllers: [DependencyController],
|
||||
providers: [],
|
||||
})
|
||||
export class DependencyModule {}
|
||||
@@ -92,8 +92,4 @@ export class ServerlessController {
|
||||
reply.send(result)
|
||||
}
|
||||
}
|
||||
|
||||
@Get()
|
||||
@Auth()
|
||||
async getDependencyGraph() {}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import { nanoid } from 'nanoid'
|
||||
import { Observable } from 'rxjs'
|
||||
|
||||
import {
|
||||
BadRequestException,
|
||||
Body,
|
||||
Delete,
|
||||
ForbiddenException,
|
||||
@@ -11,7 +9,6 @@ import {
|
||||
Post,
|
||||
Put,
|
||||
Query,
|
||||
Sse,
|
||||
} from '@nestjs/common'
|
||||
|
||||
import { ApiController } from '~/common/decorator/api-controller.decorator'
|
||||
@@ -21,12 +18,11 @@ import { HTTPDecorators } from '~/common/decorator/http.decorator'
|
||||
import { ApiName } from '~/common/decorator/openapi.decorator'
|
||||
import { IsMaster } from '~/common/decorator/role.decorator'
|
||||
import { RedisKeys } from '~/constants/cache.constant'
|
||||
import { DATA_DIR } from '~/constants/path.constant'
|
||||
import { CacheService } from '~/processors/redis/cache.service'
|
||||
import { MongoIdDto } from '~/shared/dto/id.dto'
|
||||
import { PagerDto } from '~/shared/dto/pager.dto'
|
||||
import { transformDataToPaginate } from '~/transformers/paginate.transformer'
|
||||
import { getRedisKey, installPKG } from '~/utils'
|
||||
import { getRedisKey } from '~/utils'
|
||||
|
||||
import { SnippetMoreDto } from './snippet.dto'
|
||||
import { SnippetModel, SnippetType } from './snippet.model'
|
||||
@@ -86,45 +82,6 @@ export class SnippetController {
|
||||
return 'OK'
|
||||
}
|
||||
|
||||
@Sse('/install_deps')
|
||||
async installDepsPty(@Query() query: any): Promise<Observable<string>> {
|
||||
const { id } = query
|
||||
|
||||
if (!id) {
|
||||
throw new BadRequestException('id is required')
|
||||
}
|
||||
|
||||
const packageNames = await this.redisService
|
||||
.getClient()
|
||||
.hget(getRedisKey(RedisKeys.DependencyQueue), id)
|
||||
|
||||
if (!packageNames) {
|
||||
throw new BadRequestException('can not get this task')
|
||||
}
|
||||
// const packageNames = 'axios vue'
|
||||
|
||||
const pty = await installPKG(packageNames, DATA_DIR)
|
||||
const observable = new Observable<string>((subscriber) => {
|
||||
pty.onData((data) => {
|
||||
subscriber.next(data)
|
||||
})
|
||||
|
||||
pty.onExit(async ({ exitCode }) => {
|
||||
if (exitCode != 0) {
|
||||
subscriber.next(`Error: Exit code: ${exitCode}`)
|
||||
}
|
||||
|
||||
subscriber.next('任务完成,可关闭此窗口。')
|
||||
subscriber.complete()
|
||||
await this.redisService
|
||||
.getClient()
|
||||
.hdel(getRedisKey(RedisKeys.DependencyQueue), id)
|
||||
})
|
||||
})
|
||||
|
||||
return observable
|
||||
}
|
||||
|
||||
@Post('/')
|
||||
@Auth()
|
||||
@BanInDemo
|
||||
|
||||
Reference in New Issue
Block a user