Skip to content

Commit 3b1365c

Browse files
committed
feat(create): can log file path and func start line number after create
1 parent 1c66484 commit 3b1365c

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

common/utils/create-check/createUtil.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { setQuestion } from '#common/utils/store/controller/question.js'
66
import { getQuestionChineseName } from '#common/utils/question-handler/getQuestionChineseName.js'
77
import { getQuestionById } from '#common/utils/question-getter/getQuestionById.js'
88
import { getQuestionIdBySlug } from '#common/utils/question-handler/getQuestionIdBySlug.js'
9+
import { getLineNumberByContent } from '#common/utils/file/getLineNumberByContent.js'
910

1011
export function create(mode, question, baseDir) {
1112
console.log(`MODE: ${mode}`)
@@ -14,8 +15,9 @@ export function create(mode, question, baseDir) {
1415
const questionDir = path.join(baseDir, getQuestionFileName(question))
1516
createQuestion(question, questionDir).then(async (path) => {
1617
if (!path) path = await createQuestionCopy(question, questionDir)
18+
const line = (await getLineNumberByContent(path, '@QUESTION_START')) + 1
1719
console.log(
18-
`题目[${getQuestionChineseName(question)}]获取成功!\n题目文件地址为:${path}`
20+
`题目[${getQuestionChineseName(question)}]获取成功!\n题目文件地址为:file://${path}:${line}`
1921
)
2022
resolve(true)
2123
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import fs from 'node:fs'
2+
3+
/**
4+
* 通过给入的文件地址和内容 给出对应的行号
5+
* @param filePath
6+
* @param searchString
7+
* @returns {Promise<unknown>}
8+
*/
9+
export function getLineNumberByContent(filePath, searchString) {
10+
return new Promise((resolve, reject) => {
11+
let lineNumber = 0
12+
const readStream = fs.createReadStream(filePath, { encoding: 'utf-8' })
13+
14+
readStream.on('data', (chunk) => {
15+
const lines = chunk.split('\n')
16+
for (let line of lines) {
17+
lineNumber++
18+
if (line.includes(searchString)) {
19+
readStream.close()
20+
resolve(lineNumber)
21+
return
22+
}
23+
}
24+
})
25+
26+
readStream.on('end', () => {
27+
console.warn(`"${searchString}" not found in file: ${filePath}`)
28+
resolve(0)
29+
})
30+
31+
readStream.on('error', (error) => {
32+
console.warn(`"${searchString}" not found in file: ${filePath}`)
33+
resolve(0)
34+
})
35+
})
36+
}

0 commit comments

Comments
 (0)