1
1
import _ from 'lodash' ;
2
- import * as fs from 'fs' ;
3
2
import * as util from 'util' ;
4
3
import * as os from 'os' ;
5
4
import * as path from 'path' ;
6
5
7
- import { canAccess , writeFile , moveFile , readFile , getRealPath } from '../../util/fs' ;
6
+ import * as fs from '../../util/fs' ;
8
7
import { logError } from '../../error-tracking' ;
9
8
import { OVERRIDE_BIN_PATH } from './terminal-env-overrides' ;
10
9
@@ -15,16 +14,15 @@ const POSIX_OVERRIDE_BIN_PATH = process.platform === 'win32'
15
14
16
15
const SHELL = ( process . env . SHELL || '' ) . split ( '/' ) . slice ( - 1 ) [ 0 ] ;
17
16
18
- const appendOrCreateFile = util . promisify ( fs . appendFile ) ;
19
17
const appendToFirstExisting = async ( paths : string [ ] , forceWrite : boolean , contents : string ) => {
20
18
for ( let path of paths ) {
21
19
// Follow the path through symlinks (relatively common for terminal config):
22
- const realPath = await getRealPath ( path ) ;
20
+ const realPath = await fs . getRealPath ( path ) ;
23
21
if ( ! realPath ) continue ; // File (or linked file) does not exist
24
22
25
- if ( await canAccess ( realPath ) ) {
23
+ if ( await fs . canAccess ( realPath ) ) {
26
24
// Found our first valid readable file - append our extra config
27
- return appendOrCreateFile ( realPath , contents ) ;
25
+ return fs . appendOrCreateFile ( realPath , contents ) ;
28
26
}
29
27
30
28
// ^ Small races here, if the file content/perms change between check and write, but
@@ -33,7 +31,7 @@ const appendToFirstExisting = async (paths: string[], forceWrite: boolean, conte
33
31
34
32
if ( forceWrite ) {
35
33
// If force write is set, write the last file anyway, even though it didn't exist before:
36
- return appendOrCreateFile ( paths . slice ( - 1 ) [ 0 ] , contents ) ;
34
+ return fs . appendOrCreateFile ( paths . slice ( - 1 ) [ 0 ] , contents ) ;
37
35
}
38
36
} ;
39
37
@@ -196,19 +194,19 @@ export const editShellStartupScripts = async () => {
196
194
[
197
195
path . join ( os . homedir ( ) , '.config' , 'fish' , 'config.fish' ) ,
198
196
] ,
199
- SHELL === 'fish' || await canAccess ( path . join ( os . homedir ( ) , '.config' , 'fish' ) ) ,
197
+ SHELL === 'fish' || await fs . canAccess ( path . join ( os . homedir ( ) , '.config' , 'fish' ) ) ,
200
198
FISH_SHELL_PATH_CONFIG
201
199
) . catch ( logError ) ;
202
200
} ;
203
201
204
202
const removeConfigSectionsFromFile = async ( path : string ) => {
205
203
let fileLines : string [ ] ;
206
204
207
- const targetPath = await getRealPath ( path ) ; // Follow symlinks, if present
205
+ const targetPath = await fs . getRealPath ( path ) ; // Follow symlinks, if present
208
206
if ( ! targetPath ) return ; // File doesn't exist, no need to clean it up
209
207
210
208
try {
211
- fileLines = ( await readFile ( targetPath , 'utf8' ) ) . split ( '\n' ) ;
209
+ fileLines = ( await fs . readFile ( targetPath , 'utf8' ) ) . split ( '\n' ) ;
212
210
} catch ( e ) {
213
211
// Silently skip any files we can't read
214
212
return ;
@@ -227,8 +225,8 @@ const removeConfigSectionsFromFile = async (path: string) => {
227
225
// Write & rename to ensure this is atomic, and avoid races here
228
226
// as much as we reasonably can.
229
227
const tempFile = targetPath + Date . now ( ) + '.temp' ;
230
- await writeFile ( tempFile , fileLines . join ( '\n' ) ) ;
231
- return moveFile ( tempFile , targetPath ) ;
228
+ await fs . writeFile ( tempFile , fileLines . join ( '\n' ) ) ;
229
+ return fs . moveFile ( tempFile , targetPath ) ;
232
230
} ;
233
231
234
232
// Cleanup: strip our extra config line from all config files
0 commit comments