1
- import * as fs from 'fs-extra' ;
2
- import { dirname } from 'path' ;
3
- import { stripIndents } from 'common-tags' ;
4
-
5
-
6
- export function readFile ( fileName : string ) {
7
- return new Promise < string > ( ( resolve , reject ) => {
8
- fs . readFile ( fileName , 'utf-8' , ( err : any , data : string ) => {
9
- if ( err ) {
10
- reject ( err ) ;
11
- } else {
12
- resolve ( data ) ;
13
- }
14
- } ) ;
15
- } ) ;
16
- }
1
+ import { promises as fs , constants } from 'fs' ;
2
+ import { dirname , join } from 'path' ;
3
+ import { stripIndents } from 'common-tags' ;
17
4
18
- export function writeFile ( fileName : string , content : string , options ?: any ) {
19
- return new Promise < void > ( ( resolve , reject ) => {
20
- fs . writeFile ( fileName , content , options , ( err : any ) => {
21
- if ( err ) {
22
- reject ( err ) ;
23
- } else {
24
- resolve ( ) ;
25
- }
26
- } ) ;
27
- } ) ;
5
+ export function readFile ( fileName : string ) : Promise < string > {
6
+ return fs . readFile ( fileName , 'utf-8' ) ;
28
7
}
29
8
30
-
31
- export function deleteFile ( path : string ) {
32
- return new Promise < void > ( ( resolve , reject ) => {
33
- fs . unlink ( path , ( err ) => {
34
- if ( err ) {
35
- reject ( err ) ;
36
- } else {
37
- resolve ( ) ;
38
- }
39
- } ) ;
40
- } ) ;
9
+ export function writeFile ( fileName : string , content : string , options ?: any ) : Promise < void > {
10
+ return fs . writeFile ( fileName , content , options ) ;
41
11
}
42
12
43
-
44
- export function rimraf ( path : string ) {
45
- return new Promise < void > ( ( resolve , reject ) => {
46
- fs . remove ( path , ( err ?: any ) => {
47
- if ( err ) {
48
- reject ( err ) ;
49
- } else {
50
- resolve ( ) ;
51
- }
52
- } ) ;
53
- } ) ;
13
+ export function deleteFile ( path : string ) : Promise < void > {
14
+ return fs . unlink ( path ) ;
54
15
}
55
16
56
-
57
- export function moveFile ( from : string , to : string ) {
58
- return new Promise < void > ( ( resolve , reject ) => {
59
- fs . rename ( from , to , ( err ) => {
60
- if ( err ) {
61
- reject ( err ) ;
62
- } else {
63
- resolve ( ) ;
64
- }
65
- } ) ;
66
- } ) ;
17
+ export function rimraf ( path : string ) : Promise < void > {
18
+ return fs . rmdir ( path , { recursive : true , maxRetries : 3 } ) ;
67
19
}
68
20
69
-
70
- export function symlinkFile ( from : string , to : string , type ?: string ) {
71
- return new Promise < void > ( ( resolve , reject ) => {
72
- fs . symlink ( from , to , type , ( err ) => {
73
- if ( err ) {
74
- reject ( err ) ;
75
- } else {
76
- resolve ( ) ;
77
- }
78
- } ) ;
79
- } ) ;
21
+ export function moveFile ( from : string , to : string ) : Promise < void > {
22
+ return fs . rename ( from , to ) ;
80
23
}
81
24
82
- export function createDir ( path : string ) {
83
- return _recursiveMkDir ( path ) ;
25
+ export function symlinkFile ( from : string , to : string , type ?: string ) : Promise < void > {
26
+ return fs . symlink ( from , to , type ) ;
84
27
}
85
28
86
-
87
- function _recursiveMkDir ( path : string ) : Promise < void > {
88
- if ( fs . existsSync ( path ) ) {
89
- return Promise . resolve ( ) ;
90
- } else {
91
- return _recursiveMkDir ( dirname ( path ) )
92
- . then ( ( ) => fs . mkdirSync ( path ) ) ;
93
- }
29
+ export function createDir ( path : string ) : Promise < void > {
30
+ return fs . mkdir ( path , { recursive : true } ) ;
94
31
}
95
32
96
- export function copyFile ( from : string , to : string ) {
97
- return _recursiveMkDir ( dirname ( to ) )
98
- . then ( ( ) => new Promise ( ( resolve , reject ) => {
99
- const rd = fs . createReadStream ( from ) ;
100
- rd . on ( 'error' , ( err : Error ) => reject ( err ) ) ;
33
+ export async function copyFile ( from : string , to : string ) : Promise < void > {
34
+ await createDir ( dirname ( to ) ) ;
101
35
102
- const wr = fs . createWriteStream ( to ) ;
103
- wr . on ( 'error' , ( err : Error ) => reject ( err ) ) ;
104
- wr . on ( 'close' , ( ) => resolve ( ) ) ;
105
-
106
- rd . pipe ( wr ) ;
107
- } ) ) ;
36
+ return fs . copyFile ( from , to , constants . COPYFILE_FICLONE ) ;
108
37
}
109
38
110
- export function moveDirectory ( from : string , to : string ) {
111
- return fs . move ( from , to , { overwrite : true } ) ;
112
- }
39
+ export async function moveDirectory ( from : string , to : string ) : Promise < void > {
40
+ await rimraf ( to ) ;
41
+ await createDir ( to ) ;
113
42
43
+ for ( const entry of await fs . readdir ( from ) ) {
44
+ const fromEntry = join ( from , entry ) ;
45
+ const toEntry = join ( to , entry ) ;
46
+ if ( ( await fs . stat ( fromEntry ) ) . isFile ( ) ) {
47
+ await copyFile ( fromEntry , toEntry ) ;
48
+ } else {
49
+ await moveDirectory ( fromEntry , toEntry ) ;
50
+ }
51
+ }
52
+ }
114
53
115
54
export function writeMultipleFiles ( fs : { [ path : string ] : string } ) {
116
- return Promise . all ( Object . keys ( fs ) . map ( fileName => writeFile ( fileName , fs [ fileName ] ) ) ) ;
55
+ return Promise . all ( Object . keys ( fs ) . map ( ( fileName ) => writeFile ( fileName , fs [ fileName ] ) ) ) ;
117
56
}
118
57
119
-
120
58
export function replaceInFile ( filePath : string , match : RegExp | string , replacement : string ) {
121
- return readFile ( filePath )
122
- . then ( ( content : string ) => writeFile ( filePath , content . replace ( match , replacement ) ) ) ;
59
+ return readFile ( filePath ) . then ( ( content : string ) =>
60
+ writeFile ( filePath , content . replace ( match , replacement ) ) ,
61
+ ) ;
123
62
}
124
63
125
-
126
64
export function appendToFile ( filePath : string , text : string , options ?: any ) {
127
- return readFile ( filePath )
128
- . then ( ( content : string ) => writeFile ( filePath , content . concat ( text ) , options ) ) ;
65
+ return readFile ( filePath ) . then ( ( content : string ) =>
66
+ writeFile ( filePath , content . concat ( text ) , options ) ,
67
+ ) ;
129
68
}
130
69
131
-
132
70
export function prependToFile ( filePath : string , text : string , options ?: any ) {
133
- return readFile ( filePath )
134
- . then ( ( content : string ) => writeFile ( filePath , text . concat ( content ) , options ) ) ;
71
+ return readFile ( filePath ) . then ( ( content : string ) =>
72
+ writeFile ( filePath , text . concat ( content ) , options ) ,
73
+ ) ;
135
74
}
136
75
76
+ export async function expectFileMatchToExist ( dir : string , regex : RegExp ) : Promise < string > {
77
+ const files = await fs . readdir ( dir ) ;
78
+ const fileName = files . find ( ( name ) => regex . test ( name ) ) ;
137
79
138
- export function expectFileMatchToExist ( dir : string , regex : RegExp ) {
139
- return new Promise ( ( resolve , reject ) => {
140
- const [ fileName ] = fs . readdirSync ( dir ) . filter ( name => name . match ( regex ) ) ;
141
- if ( ! fileName ) {
142
- reject ( new Error ( `File ${ regex } was expected to exist but not found...` ) ) ;
143
- }
144
- resolve ( fileName ) ;
145
- } ) ;
80
+ if ( ! fileName ) {
81
+ throw new Error ( `File ${ regex } was expected to exist but not found...` ) ;
82
+ }
83
+
84
+ return fileName ;
146
85
}
147
86
148
- export function expectFileNotToExist ( fileName : string ) {
149
- return new Promise ( ( resolve , reject ) => {
150
- fs . exists ( fileName , ( exist ) => {
151
- if ( exist ) {
152
- reject ( new Error ( `File ${ fileName } was expected not to exist but found...` ) ) ;
153
- } else {
154
- resolve ( ) ;
155
- }
156
- } ) ;
157
- } ) ;
87
+ export async function expectFileNotToExist ( fileName : string ) : Promise < void > {
88
+ try {
89
+ await fs . access ( fileName , constants . F_OK ) ;
90
+ } catch {
91
+ return ;
92
+ }
93
+
94
+ throw new Error ( `File ${ fileName } was expected not to exist but found...` ) ;
158
95
}
159
96
160
- export function expectFileToExist ( fileName : string ) {
161
- return new Promise ( ( resolve , reject ) => {
162
- fs . exists ( fileName , ( exist ) => {
163
- if ( exist ) {
164
- resolve ( ) ;
165
- } else {
166
- reject ( new Error ( `File ${ fileName } was expected to exist but not found...` ) ) ;
167
- }
168
- } ) ;
169
- } ) ;
97
+ export async function expectFileToExist ( fileName : string ) : Promise < void > {
98
+ try {
99
+ await fs . access ( fileName , constants . F_OK ) ;
100
+ } catch {
101
+ throw new Error ( `File ${ fileName } was expected to exist but not found...` ) ;
102
+ }
170
103
}
171
104
172
105
export function expectFileToMatch ( fileName : string , regEx : RegExp | string ) {
173
- return readFile ( fileName )
174
- . then ( content => {
175
- if ( typeof regEx == 'string' ) {
176
- if ( content . indexOf ( regEx ) == - 1 ) {
177
- throw new Error ( stripIndents `File "${ fileName } " did not contain "${ regEx } "...
106
+ return readFile ( fileName ) . then ( ( content ) => {
107
+ if ( typeof regEx == 'string' ) {
108
+ if ( content . indexOf ( regEx ) == - 1 ) {
109
+ throw new Error ( stripIndents `File "${ fileName } " did not contain "${ regEx } "...
178
110
Content:
179
111
${ content }
180
112
------
181
113
` ) ;
182
- }
183
- } else {
184
- if ( ! content . match ( regEx ) ) {
185
- throw new Error ( stripIndents `File "${ fileName } " did not contain "${ regEx } "...
114
+ }
115
+ } else {
116
+ if ( ! content . match ( regEx ) ) {
117
+ throw new Error ( stripIndents `File "${ fileName } " did not contain "${ regEx } "...
186
118
Content:
187
119
${ content }
188
120
------
189
121
` ) ;
190
- }
191
122
}
192
- } ) ;
123
+ }
124
+ } ) ;
193
125
}
194
126
195
127
export async function getFileSize ( fileName : string ) {
@@ -202,6 +134,8 @@ export async function expectFileSizeToBeUnder(fileName: string, sizeInBytes: num
202
134
const fileSize = await getFileSize ( fileName ) ;
203
135
204
136
if ( fileSize > sizeInBytes ) {
205
- throw new Error ( `File "${ fileName } " exceeded file size of "${ sizeInBytes } ". Size is ${ fileSize } .` ) ;
137
+ throw new Error (
138
+ `File "${ fileName } " exceeded file size of "${ sizeInBytes } ". Size is ${ fileSize } .` ,
139
+ ) ;
206
140
}
207
141
}
0 commit comments