Skip to content

Commit 7559415

Browse files
committed
perf: getFileLastModifyTimeByGit
1 parent f1a3266 commit 7559415

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

packages/shared/src/fs.ts

+26-19
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,33 @@ export async function getFileLastModifyTime(url: string) {
3939
export function getFileLastModifyTimeByGit(url: string): Promise<Date | undefined> {
4040
return new Promise((resolve) => {
4141
const cwd = path.dirname(url)
42-
if (!fs.existsSync(cwd))
43-
return resolve(undefined)
44-
const fileName = path.basename(url)
45-
46-
// 使用异步回调
47-
const child = spawn('git', ['log', '-1', '--pretty="%ai"', fileName], {
48-
cwd,
49-
})
50-
let output = ''
51-
child.stdout.on('data', d => (output += String(d)))
52-
child.on('close', async () => {
53-
let date: Date | undefined
54-
if (output.trim()) {
55-
date = new Date(output)
56-
}
57-
resolve(date)
58-
})
59-
child.on('error', async () => {
42+
43+
// 有额外的耗时,try-catch 处理
44+
// if (!fs.existsSync(cwd))
45+
// return resolve(undefined)
46+
try {
47+
const fileName = path.basename(url)
48+
49+
// 使用异步回调
50+
const child = spawn('git', ['log', '-1', '--pretty="%ai"', fileName], {
51+
cwd,
52+
})
53+
let output = ''
54+
child.stdout.on('data', d => (output += String(d)))
55+
child.on('close', async () => {
56+
let date: Date | undefined
57+
if (output.trim()) {
58+
date = new Date(output)
59+
}
60+
resolve(date)
61+
})
62+
child.on('error', async () => {
63+
resolve(undefined)
64+
})
65+
}
66+
catch {
6067
resolve(undefined)
61-
})
68+
}
6269
})
6370
}
6471

0 commit comments

Comments
 (0)