Skip to content

Commit 61e2924

Browse files
committed
feat(commit): finish set question range tag in file and get code in file by range tag
1 parent 5c7f361 commit 61e2924

9 files changed

+101
-35
lines changed

common/utils/file/getFilePathById.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import fs from 'node:fs'
2+
3+
/**
4+
* 根据id获取文件的具体路径
5+
* @param id
6+
* @param baseDir
7+
* @returns {string|undefined|string[]}
8+
*/
9+
export const getFilePathById = (id, baseDir = process.cwd()) => {
10+
const dir = fs.readdirSync(baseDir)
11+
const files = dir.filter((o) => o.startsWith(id + '.'))
12+
if (files.length > 1) return files
13+
return files?.[0]
14+
}

common/utils/file/getRootPath.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import path from 'node:path'
22
import { __dirname } from '#common/utils/file/getDirname.js'
3+
import { currentEnv } from '#common/utils/etc/checkEnv.js'
4+
import { PackageName } from '#common/constants/question.const.js'
35

4-
export const rootPath = path.dirname(path.dirname(path.dirname(__dirname)))
6+
export const rootPath =
7+
currentEnv() === 'project'
8+
? path.dirname(path.dirname(path.dirname(__dirname)))
9+
: path.join(path.dirname(__dirname), PackageName)

common/utils/question-handler/code.js

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { getQuestionCodeList } from '#common/utils/question-getter/getQuestionCodeList.js'
2+
import {
3+
getLangByExtension,
4+
setLineComment
5+
} from '#common/utils/question-handler/questionLanguage.js'
6+
import { DefaultLang } from '#common/constants/question.const.js'
7+
import fs from 'node:fs'
8+
import path from 'node:path'
9+
import { parseFilePath } from '#common/utils/file/parseFilePath.js'
10+
11+
/**
12+
* 获取代码
13+
* @param slug
14+
* @param lang
15+
* @returns {Promise<*>}
16+
*/
17+
export async function code(slug, lang) {
18+
const list = await getQuestionCodeList(slug)
19+
return list.find((o) => o.langSlug === lang)?.code
20+
}
21+
/**
22+
* 获取支持的代码语言
23+
* @param slug
24+
* @returns {Promise<string[]>}
25+
*/
26+
export async function getSupportCode(slug) {
27+
const list = await getQuestionCodeList(slug)
28+
return list.map((code) => code?.langSlug)
29+
}
30+
31+
/**
32+
* 生成有范围块的代码
33+
* @param lang
34+
* @param code
35+
* @returns {*|string}
36+
*/
37+
export const getCodeRange = (lang, code) => {
38+
if (!code) {
39+
return setLineComment(
40+
lang,
41+
`!important: 此题目没有当前语言[${lang}]的代码模板!`
42+
)
43+
}
44+
return (
45+
setLineComment(lang, '@QUESTION_START') +
46+
code +
47+
'\n' +
48+
setLineComment(lang, '@QUESTION_END')
49+
)
50+
}
51+
/**
52+
* 获取文件中的代码部分
53+
*/
54+
export const getCodeInFile = (filePath) => {
55+
const lang = getLangByExtension(filePath)?.lang ?? DefaultLang
56+
const data = fs.readFileSync(filePath, 'utf-8')
57+
const startTag = setLineComment(lang, '@QUESTION_START')
58+
const endTag = setLineComment(lang, '@QUESTION_END')
59+
const rangeReg = new RegExp(startTag + '.*' + endTag, 'ms')
60+
const rangeTagReg = new RegExp(`(${startTag}|${endTag})+`, 'mg')
61+
const match = data.match(rangeReg)
62+
if (!match) return null
63+
return match[0]?.replace(rangeTagReg, '')
64+
}

common/utils/question-handler/createQuestion.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from 'node:fs'
22
import path from 'node:path'
33
import { fulfillQuestion } from '#common/utils/question-handler/fulfillQuestion.js'
44
import { getQuestionFileExtension } from '#common/utils/question-handler/questionLanguage.js'
5-
import { getSupportCode } from '#common/utils/question-handler/getCode.js'
5+
import { getSupportCode } from '#common/utils/question-handler/code.js'
66

77
/**
88
* 创建问题

common/utils/question-handler/fulfillQuestion.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
setBlockComment,
1010
setLineComment
1111
} from '#common/utils/question-handler/questionLanguage.js'
12+
import { getCodeRange } from '#common/utils/question-handler/code.js'
1213

1314
/**
1415
* @typedef {object} Question
@@ -37,14 +38,7 @@ export function generateTemplateContent(question) {
3738
return template
3839
.replace('@Title', setBlockComment(lang, title + describe))
3940
.replace('@Describe', '')
40-
.replace(
41-
'@Function',
42-
code ??
43-
setLineComment(
44-
lang,
45-
`!important: 此题目没有当前语言[${lang}]的代码模板!`
46-
)
47-
)
41+
.replace('@Function', getCodeRange(lang, code))
4842
.replace('@TestCase', getTestCase(question))
4943
.replace('@Console', getConsoleText(question))
5044
}

common/utils/question-handler/getCode.js

-21
This file was deleted.

common/utils/question-handler/getQuestionDetail.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getQuestionDetailJson } from '#resources/headers/questionDetailJson.js'
2-
import { getCode } from '#common/utils/question-handler/getCode.js'
2+
import { code } from '#common/utils/question-handler/code.js'
33
import { getQuestionLanguage } from '#common/utils/question-handler/questionLanguage.js'
44
import { graphql } from '#common/utils/http/graphql.js'
55

@@ -14,7 +14,7 @@ export async function getQuestionDetail(slug, extra = {}) {
1414
const questionDetail = await graphql(getQuestionDetailJson(slug))
1515
const detail = questionDetail.data.question
1616
const curLang = await getQuestionLanguage()
17-
const code = await getCode(slug, curLang)
17+
const code = await code(slug, curLang)
1818
return {
1919
id: detail?.questionId,
2020
slug,

common/utils/question-handler/questionLanguage.js

+12
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ export const LANGUAGES = [
210210
lineComment: '--'
211211
}
212212
]
213+
213214
/**
214215
* 设置编程主语言
215216
*/
@@ -241,6 +242,17 @@ export function getQuestionFileExtension(lang = DefaultLang) {
241242
return detail?.extension
242243
}
243244

245+
/**
246+
* 通过后缀获取语言配置
247+
* @param extensionLike
248+
* @returns {*}
249+
*/
250+
export function getLangByExtension(extensionLike) {
251+
const reg = /\.[0-9a-zA-Z_]+$/im
252+
const match = extensionLike.match(reg)
253+
let extension = match === null ? '.' + extensionLike : match[0]
254+
return LANGUAGES?.find((o) => o.extension === extension)
255+
}
244256
/**
245257
* 获取行注释
246258
* @param lang

scripts/fork.js

-2
This file was deleted.

0 commit comments

Comments
 (0)