-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcreateQuestion.js
44 lines (43 loc) · 1.3 KB
/
createQuestion.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import fs from 'node:fs'
import path from 'node:path'
import { fulfillQuestion } from '#common/utils/question-handler/fulfillQuestion.js'
import { template } from '#resources/template/template.js'
import { getQuestionFileExtension } from '#common/utils/question-handler/questionLanguage.js'
/**
* 创建问题
* @param question 问题对象
* @param questionDir 问题要创建的目录 截止到名字
* @returns {Promise<unknown>}
*/
export function createQuestion(question, questionDir) {
return new Promise((resolve) => {
const filePath = path.normalize(
path.join(
questionDir,
`question${getQuestionFileExtension(question.lang)}`,
),
)
if (fs.existsSync(filePath)) { resolve(false) }
else {
createQuestionFile(questionDir, filePath, question)
.then(path => resolve(path))
.catch(e => resolve(false))
}
})
}
export function createQuestionFile(questionDir, questionFilePath, question) {
return new Promise((resolve, reject) => {
try {
fs.mkdir(questionDir, { recursive: true }, () => {
fs.writeFile(questionFilePath, template, null, () => {
fulfillQuestion(questionFilePath, question).then(() =>
resolve(questionFilePath),
)
})
})
}
catch (e) {
reject(e)
}
})
}