Skip to content

Commit 90987c8

Browse files
committed
perf:refactor code about create file and fulfill question
1 parent ec47f03 commit 90987c8

File tree

9 files changed

+205
-62
lines changed

9 files changed

+205
-62
lines changed

common/utils/question-handler/createQuestion.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import fs from 'node:fs'
22
import path from 'node:path'
33
import { fulfillQuestion } from '#common/utils/question-handler/fulfillQuestion.js'
4-
import { template } from '#resources/template/template.js'
54
import { getQuestionFileExtension } from '#common/utils/question-handler/questionLanguage.js'
65

76
/**
@@ -18,7 +17,9 @@ export function createQuestion(question, questionDir) {
1817
`question${getQuestionFileExtension(question.lang)}`,
1918
),
2019
)
21-
if (fs.existsSync(filePath)) { resolve(false) }
20+
if (fs.existsSync(filePath)) {
21+
resolve(false)
22+
}
2223
else {
2324
createQuestionFile(questionDir, filePath, question)
2425
.then(path => resolve(path))
@@ -29,12 +30,12 @@ export function createQuestion(question, questionDir) {
2930
export function createQuestionFile(questionDir, questionFilePath, question) {
3031
return new Promise((resolve, reject) => {
3132
try {
33+
// 创建题目的目录
3234
fs.mkdir(questionDir, { recursive: true }, () => {
33-
fs.writeFile(questionFilePath, template, null, () => {
34-
fulfillQuestion(questionFilePath, question).then(() =>
35-
resolve(questionFilePath),
36-
)
37-
})
35+
// 写入文件和模板
36+
fulfillQuestion(questionFilePath, question).then(() =>
37+
resolve(questionFilePath),
38+
)
3839
})
3940
}
4041
catch (e) {
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
import fs from 'node:fs'
22
import { removeDomTags } from '../functions/removeDomTags.js'
33
import { getTestCase } from './getTestCase.js'
4-
import { getQuestionUrl } from './getQuestionUrl.js'
54
import { createMarkdown } from './createMarkdown.js'
65
import { getQuestionChineseName } from '#common/utils/question-handler/getQuestionChineseName.js'
6+
import { getConsoleText } from '#common/utils/question-handler/getConsoleText.js'
7+
import { template } from '#resources/template/template.js'
8+
import { setBlockComment } from '#common/utils/question-handler/questionLanguage.js'
79

810
/**
911
* @typedef {object} Question
1012
* @property {string} title
13+
* @property {string} title
1114
* @property {number} date
1215
* @property {string} detail
16+
* @property {string} lang
17+
* @property {string} code
1318
*/
1419

1520
/**
@@ -22,42 +27,34 @@ import { getQuestionChineseName } from '#common/utils/question-handler/getQuesti
2227
* @param {Question} question
2328
*
2429
*/
25-
export function generateTemplateContent(data, question) {
26-
return data
27-
.replace(
28-
'@题目',
29-
`${getQuestionChineseName(question)} ${question.date ? `[${question.date}]` : ''}`,
30-
)
31-
.replace(
32-
'@描述',
33-
removeDomTags(question.detail)
34-
.replace('@url', question.url)
35-
.replace(/\n+/g, '\n')
36-
.replaceAll('\n', '\n * '),
37-
)
38-
.replace('// @Function', question.code)
39-
.replace('// @TestCase', getTestCase(question))
40-
.replace('@url', getQuestionUrl(question.slug))
30+
export function generateTemplateContent(question) {
31+
const title = `${getQuestionChineseName(question)} ${question.date ? `[${question.date}]` : ''}\n`
32+
const describe = removeDomTags(question.detail).replace(/\n+/g, '\n')
33+
const code = question.code
34+
return template
35+
.replace('@Title', setBlockComment(question.lang, title + describe))
36+
.replace('@Describe', '')
37+
.replace('@Function', code)
38+
.replace('@TestCase', getTestCase(question))
39+
.replace('@Console', getConsoleText(question))
4140
}
4241
/**
43-
* 填充模板文件
42+
* 模板文件内容替换并生成文件
4443
* @param questionPath
4544
* @param question
4645
*/
4746
export function fulfillQuestion(questionPath, question) {
4847
return new Promise((resolve) => {
48+
// 创建描述文件 md
49+
createMarkdown(question.detail, questionPath)
50+
// 创建填充内容并创建
4951
// 开始填充内容
50-
fs.readFile(questionPath, 'utf8', (err, data) => {
52+
const newData = generateTemplateContent(question)
53+
// 创建文件
54+
fs.writeFile(questionPath, newData, (err) => {
5155
if (err)
5256
throw err
53-
// 修改文件内容
54-
const newData = generateTemplateContent(data, question)
55-
createMarkdown(question.detail, questionPath)
56-
fs.writeFile(questionPath, newData, (err) => {
57-
if (err)
58-
throw err
59-
resolve()
60-
})
57+
resolve()
6158
})
6259
})
6360
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { DefaultLang } from '#common/constants/question.const.js'
2+
import { getQuestionUrl } from '#common/utils/question-handler/getQuestionUrl.js'
3+
import { setLineComment } from '#common/utils/question-handler/questionLanguage.js'
4+
5+
export function getConsoleText(question) {
6+
const url = getQuestionUrl(question.slug)
7+
if (question.lang === DefaultLang)
8+
return `console.log('点击跳转到题目提交: ${url}');`
9+
else
10+
return setLineComment(question.lang, `题目地址:${url}`)
11+
}

common/utils/question-handler/getHot100QuestionList.js common/utils/question-handler/getHot100QuestionListCode.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async function getPromiseList() {
2020
)
2121
}
2222

23-
export async function getHot100QuestionListJSCode() {
23+
export async function getHot100QuestionListCode() {
2424
const promiseList = await getPromiseList()
2525
return await Promise.all(promiseList)
2626
}

common/utils/question-handler/getTestCase.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { removeDomTags } from '../functions/removeDomTags.js'
22
import { getDataStructure } from './parseStructure.js'
33
import { DefaultLang } from '#common/constants/question.const.js'
4+
import { setBlockComment } from '#common/utils/question-handler/questionLanguage.js'
45

56
/**
67
* test case 需要从两个地方拿到内容
@@ -48,14 +49,18 @@ export function getTestCase(question) {
4849
}
4950
else {
5051
// 其他语言无法支持测试 只能提供测试数据
51-
console.log(cases, expires)
52-
let showText = `\/* 暂无法支持除JS外的语言测试,提取的一些入参和返回值供自行测试,每一个case中的第一行为入参,第二行为返回值\n`
53-
for (let i = 0; i < Math.max(cases.length, expires.length); i++) {
52+
// 生成注释语句
53+
let showText = `暂无法支持除JS外的语言测试,提取的一些入参和返回值供自行测试,每一个case中的第一行为入参,第二行为返回值\n`
54+
for (
55+
let i = 0;
56+
i < Math.max(cases?.length ?? 0, expires?.length ?? 0);
57+
i++
58+
) {
5459
showText += `case ${i + 1}:\n`
5560
showText += `${cases?.[i]}\n` ?? '[参数获取错误]\n'
5661
showText += `${expires?.[i]}\n` ?? '[返回值获取错误]\n'
5762
}
58-
showText += `\n*\/`
59-
return showText
63+
showText += `\n`
64+
return setBlockComment(question.lang, showText)
6065
}
6166
}

0 commit comments

Comments
 (0)