Skip to content
This repository was archived by the owner on Jul 30, 2025. It is now read-only.

Commit 38c41ea

Browse files
committed
fix(plugins/plugin-bash-like): improve efficiency of fetching file data
fstat inefficiently does stat-then-readFile for cases that just want the file bits Fixes #6659
1 parent 29cba1d commit 38c41ea

File tree

1 file changed

+52
-47
lines changed
  • plugins/plugin-bash-like/fs/src/lib

1 file changed

+52
-47
lines changed
Lines changed: 52 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-19 IBM Corporation
2+
* Copyright 2018-21 IBM Corporation
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,57 +39,62 @@ export const fstat = ({
3939
argvNoOptions,
4040
parsedOptions
4141
}: Pick<Arguments<FStatOptions>, 'argvNoOptions' | 'parsedOptions'>) => {
42-
return new Promise<FStat>((resolve, reject) => {
43-
const filepath = argvNoOptions[1]
42+
const filepath = argvNoOptions[1]
43+
const { resolved: fullpath, viewer = 'open' } = findFileWithViewer(expandHomeDir(filepath))
4444

45-
const { resolved: fullpath, viewer = 'open' } = findFileWithViewer(expandHomeDir(filepath))
45+
const prettyFullPath = fullpath.replace(new RegExp(`^${process.env.HOME}`), '~')
4646

47-
const prettyFullPath = fullpath.replace(new RegExp(`^${process.env.HOME}`), '~')
47+
return new Promise<FStat>((resolve, reject) => {
48+
const handleError = (err: CodedError<string>) => {
49+
if (err.code === 'ENOENT') {
50+
if (parsedOptions['enoent-ok']) {
51+
// file does not exist; caller told us that's ok
52+
resolve({
53+
viewer,
54+
filepath,
55+
fullpath: prettyFullPath,
56+
isDirectory: false,
57+
data: ''
58+
})
59+
}
4860

49-
// note: stat not lstat, because we want to follow the link
50-
stat(fullpath, (err, stats) => {
51-
if (err) {
52-
if (err.code === 'ENOENT') {
53-
if (parsedOptions['enoent-ok']) {
54-
// file does not exist; caller told us that's ok
55-
resolve({
56-
viewer,
57-
filepath,
58-
fullpath: prettyFullPath,
59-
isDirectory: false,
60-
data: ''
61-
})
62-
}
61+
const error: CodedError = new Error(err.message)
62+
error.stack = err.stack
63+
error.code = 404
64+
reject(error)
65+
} else {
66+
reject(err)
67+
}
68+
}
6369

64-
const error: CodedError = new Error(err.message)
65-
error.stack = err.stack
66-
error.code = 404
67-
reject(error)
70+
if (parsedOptions['with-data']) {
71+
readFile(fullpath, (err, data) => {
72+
if (err) {
73+
return handleError(err)
6874
} else {
69-
reject(err)
75+
resolve({
76+
viewer,
77+
filepath,
78+
fullpath: prettyFullPath,
79+
data: data.toString(),
80+
isDirectory: false
81+
})
7082
}
71-
} else if (stats.isDirectory() || !parsedOptions['with-data']) {
72-
resolve({
73-
viewer,
74-
filepath,
75-
fullpath: prettyFullPath,
76-
isDirectory: stats.isDirectory()
77-
})
78-
} else {
79-
readFile(fullpath, (err, data) => {
80-
if (err) {
81-
reject(err)
82-
} else {
83-
resolve({
84-
viewer,
85-
filepath,
86-
fullpath: prettyFullPath,
87-
data: data.toString(),
88-
isDirectory: false
89-
})
90-
}
91-
})
92-
}
93-
})
83+
})
84+
} else {
85+
// note: stat not lstat, because we want to follow the link
86+
stat(fullpath, (err, stats) => {
87+
if (err) {
88+
return handleError(err)
89+
} else {
90+
resolve({
91+
viewer,
92+
filepath,
93+
fullpath: prettyFullPath,
94+
isDirectory: stats.isDirectory()
95+
})
96+
}
97+
})
98+
}
9499
})
95100
}

0 commit comments

Comments
 (0)