Skip to content

Commit aef6057

Browse files
browser: remove fs-extra uses from systemUtilities.ts (#4294)
Reverts to only using `fs` methods instead of `fs-extra` since `fs` will not break if imported in to the browser. `fs` code still cannot run in the browser, but it will not break if something imports it. Signed-off-by: nkomonen <[email protected]>
1 parent 62eb901 commit aef6057

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

src/shared/systemUtilities.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import * as fs from 'fs-extra'
6+
import { promises as fsPromises } from 'fs'
7+
import fs from 'fs'
78
import * as vscode from 'vscode'
89
import * as os from 'os'
910
import * as path from 'path'
@@ -28,7 +29,7 @@ export function createPermissionsErrorHandler(
2829
}
2930

3031
const userInfo = os.userInfo({ encoding: 'utf-8' })
31-
const stats = await fs.stat(uri.fsPath).catch(async err2 => {
32+
const stats = await fsPromises.stat(uri.fsPath).catch(async err2 => {
3233
if (!isNoPermissionsError(err2) && !(isFileNotFoundError(err2) && perms[1] === 'w')) {
3334
throw err
3435
}
@@ -70,7 +71,7 @@ export class SystemUtilities {
7071
const errorHandler = createPermissionsErrorHandler(uri, 'r**')
7172

7273
if (isCloud9()) {
73-
return decoder.decode(await fs.readFile(uri.fsPath).catch(errorHandler))
74+
return decoder.decode(await fsPromises.readFile(uri.fsPath).catch(errorHandler))
7475
}
7576

7677
return decoder.decode(await vscode.workspace.fs.readFile(uri).then(undefined, errorHandler))
@@ -86,7 +87,7 @@ export class SystemUtilities {
8687
const content = typeof data === 'string' ? new TextEncoder().encode(data) : data
8788

8889
if (isCloud9()) {
89-
return fs.writeFile(uri.fsPath, content, opt).catch(errorHandler)
90+
return fsPromises.writeFile(uri.fsPath, content, opt).catch(errorHandler)
9091
}
9192

9293
return vscode.workspace.fs.writeFile(uri, content).then(undefined, errorHandler)
@@ -98,11 +99,11 @@ export class SystemUtilities {
9899
const errorHandler = createPermissionsErrorHandler(dirUri, '*wx')
99100

100101
if (isCloud9()) {
101-
const stat = await fs.stat(uri.fsPath)
102+
const stat = await fsPromises.stat(uri.fsPath)
102103
if (stat.isDirectory()) {
103-
return fs.remove(uri.fsPath).catch(errorHandler)
104+
return fsPromises.rmdir(uri.fsPath).catch(errorHandler)
104105
} else {
105-
return fs.unlink(uri.fsPath).catch(errorHandler)
106+
return fsPromises.unlink(uri.fsPath).catch(errorHandler)
106107
}
107108
}
108109

@@ -120,7 +121,7 @@ export class SystemUtilities {
120121
} else if (uri.scheme !== 'file' || !isFileNotFoundError(err) || process.platform === 'win32') {
121122
throw err
122123
} else {
123-
const stats = await fs.stat(dirUri.fsPath).catch(() => {
124+
const stats = await fsPromises.stat(dirUri.fsPath).catch(() => {
124125
throw err
125126
})
126127
if ((stats.mode & fs.constants.S_IXUSR) === 0) {
@@ -138,7 +139,10 @@ export class SystemUtilities {
138139
const uri = typeof file === 'string' ? vscode.Uri.file(file) : file
139140

140141
if (isCloud9()) {
141-
return new Promise<boolean>(resolve => fs.access(uri.fsPath, fs.constants.F_OK, err => resolve(!err)))
142+
return fsPromises.access(uri.fsPath, fs.constants.F_OK).then(
143+
() => true,
144+
() => false
145+
)
142146
}
143147

144148
return vscode.workspace.fs.stat(uri).then(
@@ -152,7 +156,10 @@ export class SystemUtilities {
152156
const errorHandler = createPermissionsErrorHandler(vscode.Uri.joinPath(uri, '..'), '*wx')
153157

154158
if (isCloud9()) {
155-
return fs.ensureDir(uri.fsPath).catch(errorHandler)
159+
return fsPromises
160+
.mkdir(uri.fsPath, { recursive: true })
161+
.then(() => {})
162+
.catch(errorHandler)
156163
}
157164

158165
return vscode.workspace.fs.createDirectory(uri).then(undefined, errorHandler)
@@ -170,13 +177,13 @@ export class SystemUtilities {
170177
*
171178
* This throws {@link PermissionsError} when permissions are insufficient.
172179
*/
173-
public static async checkPerms(file: string | vscode.Uri, perms: PermissionsTriplet) {
180+
public static async checkPerms(file: string | vscode.Uri, perms: PermissionsTriplet): Promise<void> {
174181
const uri = typeof file === 'string' ? vscode.Uri.file(file) : file
175182
const errorHandler = createPermissionsErrorHandler(uri, perms)
176183
const flags = Array.from(perms) as (keyof typeof this.modeMap)[]
177184
const mode = flags.reduce((m, f) => m | this.modeMap[f], fs.constants.F_OK)
178185

179-
return fs.access(uri.fsPath, mode).catch(errorHandler)
186+
return fsPromises.access(uri.fsPath, mode).catch(errorHandler)
180187
}
181188

182189
// TODO: implement this by checking the file mode
@@ -242,7 +249,7 @@ export class SystemUtilities {
242249
'code', // $PATH
243250
]
244251
for (const vsc of vscs) {
245-
if (!vsc || (vsc !== 'code' && !fs.existsSync(vsc))) {
252+
if (!vsc || (vsc !== 'code' && !(await this.fileExists(vsc)))) {
246253
continue
247254
}
248255
if (await SystemUtilities.tryRun(vsc, ['--version'])) {
@@ -296,7 +303,7 @@ export class SystemUtilities {
296303
'C:/Program Files/Git/usr/bin/ssh.exe',
297304
]
298305
for (const p of paths) {
299-
if (!p || ('ssh' !== p && !fs.existsSync(p))) {
306+
if (!p || ('ssh' !== p && !(await this.fileExists(p)))) {
300307
continue
301308
}
302309
if (await SystemUtilities.tryRun(p, ['-G', 'x'], 'noresult' /* "ssh -G" prints quasi-sensitive info. */)) {
@@ -318,7 +325,7 @@ export class SystemUtilities {
318325

319326
const paths = [git.gitPath, 'git']
320327
for (const p of paths) {
321-
if (!p || ('git' !== p && !fs.existsSync(p))) {
328+
if (!p || ('git' !== p && !(await this.fileExists(p)))) {
322329
continue
323330
}
324331
if (await SystemUtilities.tryRun(p, ['--version'])) {
@@ -338,7 +345,7 @@ export class SystemUtilities {
338345

339346
const paths = ['bash', 'C:/Program Files/Git/usr/bin/bash.exe', 'C:/Program Files (x86)/Git/usr/bin/bash.exe']
340347
for (const p of paths) {
341-
if (!p || ('bash' !== p && !fs.existsSync(p))) {
348+
if (!p || ('bash' !== p && !(await this.fileExists(p)))) {
342349
continue
343350
}
344351
if (await SystemUtilities.tryRun(p, ['--version'])) {

0 commit comments

Comments
 (0)