feat: import snippets
This commit is contained in:
@@ -9,15 +9,21 @@ import {
|
||||
Put,
|
||||
Query,
|
||||
} from '@nestjs/common'
|
||||
import { SnippetModel, SnippetType } from './snippet.model'
|
||||
import { SnippetService } from './snippet.service'
|
||||
|
||||
import { Auth } from '~/common/decorator/auth.decorator'
|
||||
import { HTTPDecorators } from '~/common/decorator/http.decorator'
|
||||
import { ApiName } from '~/common/decorator/openapi.decorator'
|
||||
import { IsMaster } from '~/common/decorator/role.decorator'
|
||||
import { DATA_DIR } from '~/constants/path.constant'
|
||||
import { MongoIdDto } from '~/shared/dto/id.dto'
|
||||
import { PagerDto } from '~/shared/dto/pager.dto'
|
||||
import { transformDataToPaginate } from '~/transformers/paginate.transformer'
|
||||
import { installPKG } from '~/utils'
|
||||
|
||||
import { SnippetMoreDto } from './snippet.dto'
|
||||
import { SnippetModel, SnippetType } from './snippet.model'
|
||||
import { SnippetService } from './snippet.service'
|
||||
|
||||
@ApiName
|
||||
@Controller('snippets')
|
||||
export class SnippetController {
|
||||
@@ -41,6 +47,31 @@ export class SnippetController {
|
||||
)
|
||||
}
|
||||
|
||||
@Post('/more')
|
||||
@Auth()
|
||||
async createMore(@Body() body: SnippetMoreDto) {
|
||||
const { snippets, packages = [] } = body
|
||||
const tasks = snippets.map((snippet) => this.create(snippet))
|
||||
|
||||
const resultList = await Promise.all(tasks)
|
||||
|
||||
try {
|
||||
if (packages.length) {
|
||||
const tasks2 = packages.map((pkg) => {
|
||||
return installPKG(pkg, DATA_DIR)
|
||||
})
|
||||
await Promise.all(tasks2)
|
||||
}
|
||||
} catch (err) {
|
||||
await Promise.all(
|
||||
resultList.map((doc) => {
|
||||
return doc.remove()
|
||||
}),
|
||||
)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
@Post('/')
|
||||
@Auth()
|
||||
async create(@Body() body: SnippetModel) {
|
||||
|
||||
14
src/modules/snippet/snippet.dto.ts
Normal file
14
src/modules/snippet/snippet.dto.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { Type } from 'class-transformer'
|
||||
import { IsOptional, IsString, ValidateNested } from 'class-validator'
|
||||
|
||||
import { SnippetModel } from './snippet.model'
|
||||
|
||||
export class SnippetMoreDto {
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => SnippetModel)
|
||||
snippets: SnippetModel[]
|
||||
|
||||
@IsString({ each: true })
|
||||
@IsOptional()
|
||||
packages?: string[]
|
||||
}
|
||||
@@ -66,6 +66,7 @@ export const installPKG = async (name: string, cwd: string) => {
|
||||
const res = await nothrow($`${managerName} --version`)
|
||||
if (res.exitCode === 0) {
|
||||
manager = managerName
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user