feat: import snippets

This commit is contained in:
Innei
2022-04-05 14:40:03 +08:00
parent 3398dc0fe1
commit c86df4a34e
3 changed files with 48 additions and 2 deletions

View File

@@ -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) {

View 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[]
}

View File

@@ -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
}
}
}