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' ;
174
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' ) ;
287}
298
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 ) ;
4111}
4212
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 ) ;
5415}
5516
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 } ) ;
6719}
6820
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 ) ;
8023}
8124
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 ) ;
8427}
8528
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 } ) ;
9431}
9532
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 ) ) ;
10135
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 ) ;
10837}
10938
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 ) ;
11342
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+ }
11453
11554export 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 ] ) ) ) ;
11756}
11857
119-
12058export 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+ ) ;
12362}
12463
125-
12664export 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+ ) ;
12968}
13069
131-
13270export 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+ ) ;
13574}
13675
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 ) ) ;
13779
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 ;
14685}
14786
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...` ) ;
15895}
15996
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+ }
170103}
171104
172105export 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 } "...
178110 Content:
179111 ${ content }
180112 ------
181113 ` ) ;
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 } "...
186118 Content:
187119 ${ content }
188120 ------
189121 ` ) ;
190- }
191122 }
192- } ) ;
123+ }
124+ } ) ;
193125}
194126
195127export async function getFileSize ( fileName : string ) {
@@ -202,6 +134,8 @@ export async function expectFileSizeToBeUnder(fileName: string, sizeInBytes: num
202134 const fileSize = await getFileSize ( fileName ) ;
203135
204136 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+ ) ;
206140 }
207141}
0 commit comments