Skip to content

Commit

Permalink
feat: optimize call github (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoanle396 authored Aug 9, 2024
1 parent d9165f2 commit 7533ae2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 41 deletions.
6 changes: 3 additions & 3 deletions apps/server/src/crawl/handleCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ export async function handleCase(caseData: DataReturn) {
if (caseData.subCategory.length !== 0) {
try {
await Promise.all(
caseData.subCategory.map((subPath) => {
creatorSubCategory(`/${subPath}`)
caseData.subCategory.map(async (subPath) => {
await creatorSubCategory(`/${subPath}`)
}),
)
} catch (error) {
Expand Down Expand Up @@ -115,7 +115,7 @@ export async function handleCase(caseData: DataReturn) {

const category = await getCategory(projectPath)

creatorProject(`/${projectPath}`, category)
await creatorProject(`/${projectPath}`, category)
}),
)
} catch (error) {
Expand Down
5 changes: 1 addition & 4 deletions apps/server/src/creator/creatorSubCategory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ export async function creatorSubCategory(subPath: string) {
category.description = detail.description
category.parentId = cat.id

const created = await connection
.getRepository(Categories)
.save(category)
.catch((error) => console.log(error))
const created = await connection.getRepository(Categories).save(category)

const datas = await fsWrapper.readdir(`${subPath}`)
await Promise.all(
Expand Down
76 changes: 42 additions & 34 deletions apps/server/src/utils/fs/fsWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
import 'dotenv/config'
import { exec } from 'child_process'
import { logger } from '../logger'

const token = process.env.GITHUB_TOKEN
const owner = process.env.GITHUB_OWNER
const repo = process.env.GITHUB_REPO
const branch = process.env.GITHUB_BRANCH

function fsGithubRequest(path: string): string {
const request = `curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${token}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/${owner}/${repo}/contents${path}?ref=${branch}`

return request
async function fsGithubRequest(path: string): Promise<string> {
try {
const url = `https://api.github.com/repos/${owner}/${repo}/contents${path}?ref=${branch}`
const options = {
headers: {
Accept: 'application/vnd.github+json',
Authorization: `Bearer ${token}`,
'X-GitHub-Api-Version': '2022-11-28',
},
}
const data = await fetch(url, options)
const result = await data.json()
if (!data.ok) {
throw result?.message ?? 'Error when working with github'
}
return JSON.stringify(result)
} catch (error) {
logger.info('Error when call github' + error)
throw error
}
}

function handleDirRaw(output: string): Array<string> {
Expand All @@ -30,34 +42,30 @@ function handleFileRaw(output: string) {
}

export class fsWrapper {
// path is the path from /
static readdir(path: string): Promise<Array<string>> {
return new Promise((res, rej) => {
const request = fsGithubRequest(path)
exec(request, (error, stdout, stderr) => {
const dir = handleDirRaw(stdout)
res(dir)
})
})
static async readdir(path: string): Promise<Array<string>> {
try {
const data = await fsGithubRequest(path)
return handleDirRaw(data)
} catch (error) {
throw error
}
}

static readFile(path: string): Promise<string> {
return new Promise((res, rej) => {
const request = fsGithubRequest(path)
exec(request, (error, stdout, stderr) => {
res(handleFileRaw(stdout))
})
})
static async readFile(path: string): Promise<string> {
try {
const data = await fsGithubRequest(path)
return handleFileRaw(data)
} catch (error) {
throw error
}
}

static checkFileExist(path: string): Promise<boolean> {
return new Promise((res, rej) => {
const request = fsGithubRequest(path)
exec(request, (error, stdout, stderr) => {
const data = JSON.parse(stdout)
if (data.message == 'Not Found') res(false)
else res(true)
})
})
static async checkFileExist(path: string): Promise<boolean> {
try {
await fsGithubRequest(path)
return true
} catch (error) {
return false
}
}
}

0 comments on commit 7533ae2

Please sign in to comment.