55
66import * as extfs from 'vs/base/node/extfs' ;
77import { join , dirname } from 'vs/base/common/path' ;
8- import { nfcall , Queue } from 'vs/base/common/async' ;
8+ import { Queue } from 'vs/base/common/async' ;
99import * as fs from 'fs' ;
1010import * as os from 'os' ;
1111import * as platform from 'vs/base/common/platform' ;
@@ -15,53 +15,61 @@ import { promisify } from 'util';
1515import { CancellationToken } from 'vs/base/common/cancellation' ;
1616
1717export function readdir ( path : string ) : Promise < string [ ] > {
18- return nfcall ( extfs . readdir , path ) ;
18+ return promisify ( extfs . readdir ) ( path ) ;
1919}
2020
2121export function exists ( path : string ) : Promise < boolean > {
22- return new Promise ( c => fs . exists ( path , c ) ) ;
22+ return promisify ( fs . exists ) ( path ) ;
2323}
2424
25- export function chmod ( path : string , mode : number ) : Promise < boolean > {
26- return nfcall ( fs . chmod , path , mode ) ;
25+ export function chmod ( path : string , mode : number ) : Promise < void > {
26+ return promisify ( fs . chmod ) ( path , mode ) ;
2727}
2828
29- export function rimraf ( path : string ) : Promise < void > {
30- return lstat ( path ) . then ( stat => {
29+ export async function rimraf ( path : string ) : Promise < void > {
30+ try {
31+ const stat = await lstat ( path ) ;
32+
33+ // Folder delete (recursive) - NOT for symbolic links though!
3134 if ( stat . isDirectory ( ) && ! stat . isSymbolicLink ( ) ) {
32- return readdir ( path )
33- . then ( children => Promise . all ( children . map ( child => rimraf ( join ( path , child ) ) ) ) )
34- . then ( ( ) => rmdir ( path ) ) ;
35- } else {
35+ const children = await readdir ( path ) ;
36+
37+ await Promise . all ( children . map ( child => rimraf ( join ( path , child ) ) ) ) ;
38+
39+ await promisify ( fs . rmdir ) ( path ) ;
40+ }
41+
42+ // Single file delete
43+ else {
3644 return unlink ( path ) ;
3745 }
38- } , ( err : NodeJS . ErrnoException ) => {
39- if ( err . code = == 'ENOENT' ) {
40- return undefined ;
46+ } catch ( error ) {
47+ if ( error . code ! == 'ENOENT' ) {
48+ throw error ;
4149 }
42-
43- return Promise . reject ( err ) ;
44- } ) ;
50+ }
4551}
4652
4753export function realpath ( path : string ) : Promise < string > {
48- return nfcall ( extfs . realpath , path ) ;
54+ return promisify ( extfs . realpath ) ( path ) ;
4955}
5056
5157export function stat ( path : string ) : Promise < fs . Stats > {
52- return nfcall ( fs . stat , path ) ;
58+ return promisify ( fs . stat ) ( path ) ;
5359}
5460
5561export function statLink ( path : string ) : Promise < { stat : fs . Stats , isSymbolicLink : boolean } > {
56- return nfcall ( extfs . statLink , path ) ;
62+ return new Promise ( ( resolve , reject ) => {
63+ extfs . statLink ( path , ( error , result ) => error ? reject ( error ) : resolve ( result ! ) ) ;
64+ } ) ;
5765}
5866
5967export function lstat ( path : string ) : Promise < fs . Stats > {
60- return nfcall ( fs . lstat , path ) ;
68+ return promisify ( fs . lstat ) ( path ) ;
6169}
6270
6371export function rename ( oldPath : string , newPath : string ) : Promise < void > {
64- return nfcall ( fs . rename , oldPath , newPath ) ;
72+ return promisify ( fs . rename ) ( oldPath , newPath ) ;
6573}
6674
6775export function renameIgnoreError ( oldPath : string , newPath : string ) : Promise < void > {
@@ -70,30 +78,26 @@ export function renameIgnoreError(oldPath: string, newPath: string): Promise<voi
7078 } ) ;
7179}
7280
73- export function rmdir ( path : string ) : Promise < void > {
74- return nfcall ( fs . rmdir , path ) ;
75- }
76-
7781export function unlink ( path : string ) : Promise < void > {
78- return nfcall ( fs . unlink , path ) ;
82+ return promisify ( fs . unlink ) ( path ) ;
7983}
8084
8185export function symlink ( target : string , path : string , type ?: string ) : Promise < void > {
82- return nfcall < void > ( fs . symlink , target , path , type ) ;
86+ return promisify ( fs . symlink ) ( target , path , type ) ;
8387}
8488
8589export function readlink ( path : string ) : Promise < string > {
86- return nfcall < string > ( fs . readlink , path ) ;
90+ return promisify ( fs . readlink ) ( path ) ;
8791}
8892
8993export function truncate ( path : string , len : number ) : Promise < void > {
90- return nfcall ( fs . truncate , path , len ) ;
94+ return promisify ( fs . truncate ) ( path , len ) ;
9195}
9296
9397export function readFile ( path : string ) : Promise < Buffer > ;
9498export function readFile ( path : string , encoding : string ) : Promise < string > ;
9599export function readFile ( path : string , encoding ?: string ) : Promise < Buffer | string > {
96- return nfcall ( fs . readFile , path , encoding ) ;
100+ return promisify ( fs . readFile ) ( path , encoding ) ;
97101}
98102
99103// According to node.js docs (https://nodejs.org/docs/v6.5.0/api/fs.html#fs_fs_writefile_file_data_options_callback)
@@ -109,7 +113,11 @@ export function writeFile(path: string, data: string | Buffer | NodeJS.ReadableS
109113export function writeFile ( path : string , data : string | Buffer | NodeJS . ReadableStream | Uint8Array , options ?: extfs . IWriteFileOptions ) : Promise < void > {
110114 const queueKey = toQueueKey ( path ) ;
111115
112- return ensureWriteFileQueue ( queueKey ) . queue ( ( ) => nfcall ( extfs . writeFileAndFlush , path , data , options ) ) ;
116+ return ensureWriteFileQueue ( queueKey ) . queue ( ( ) => {
117+ return new Promise ( ( resolve , reject ) => {
118+ extfs . writeFileAndFlush ( path , data , options , error => error ? reject ( error ) : resolve ( ) ) ;
119+ } ) ;
120+ } ) ;
113121}
114122
115123function toQueueKey ( path : string ) : string {
@@ -137,43 +145,51 @@ function ensureWriteFileQueue(queueKey: string): Queue<void> {
137145 return writeFileQueue ;
138146}
139147
140- /**
141- * Read a dir and return only subfolders
142- */
143- export function readDirsInDir ( dirPath : string ) : Promise < string [ ] > {
144- return readdir ( dirPath ) . then ( children => {
145- return Promise . all ( children . map ( c => dirExists ( join ( dirPath , c ) ) ) ) . then ( exists => {
146- return children . filter ( ( _ , i ) => exists [ i ] ) ;
147- } ) ;
148- } ) ;
148+ export async function readDirsInDir ( dirPath : string ) : Promise < string [ ] > {
149+ const children = await readdir ( dirPath ) ;
150+ const directories : string [ ] = [ ] ;
151+
152+ for ( const child of children ) {
153+ if ( await dirExists ( join ( dirPath , child ) ) ) {
154+ directories . push ( child ) ;
155+ }
156+ }
157+
158+ return directories ;
149159}
150160
151- /**
152- * `path` exists and is a directory
153- */
154- export function dirExists ( path : string ) : Promise < boolean > {
155- return stat ( path ) . then ( stat => stat . isDirectory ( ) , ( ) => false ) ;
161+ export async function dirExists ( path : string ) : Promise < boolean > {
162+ try {
163+ const fileStat = await stat ( path ) ;
164+
165+ return fileStat . isDirectory ( ) ;
166+ } catch ( error ) {
167+ return false ;
168+ }
156169}
157170
158- /**
159- * `path` exists and is a file.
160- */
161- export function fileExists ( path : string ) : Promise < boolean > {
162- return stat ( path ) . then ( stat => stat . isFile ( ) , ( ) => false ) ;
171+ export async function fileExists ( path : string ) : Promise < boolean > {
172+ try {
173+ const fileStat = await stat ( path ) ;
174+
175+ return fileStat . isFile ( ) ;
176+ } catch ( error ) {
177+ return false ;
178+ }
163179}
164180
165- /**
166- * Deletes a path from disk.
167- */
168- let _tmpDir : string | null = null ;
181+ let tmpDir : string | null = null ;
169182function getTmpDir ( ) : string {
170- if ( ! _tmpDir ) {
171- _tmpDir = os . tmpdir ( ) ;
183+ if ( ! tmpDir ) {
184+ tmpDir = os . tmpdir ( ) ;
172185 }
173- return _tmpDir ;
186+ return tmpDir ;
174187}
188+
175189export function del ( path : string , tmp = getTmpDir ( ) ) : Promise < void > {
176- return nfcall ( extfs . del , path , tmp ) ;
190+ return new Promise ( ( resolve , reject ) => {
191+ extfs . del ( path , tmp , error => error ? reject ( error ) : resolve ( ) ) ;
192+ } ) ;
177193}
178194
179195export function whenDeleted ( path : string ) : Promise < void > {
0 commit comments