11import path from 'path'
22import { fs } from './util/fs'
33
4- export async function readFile ( projectRoot : string , options : { file : string , encoding ?: string } = { file : '' , encoding : 'utf8' } ) {
4+ export async function readFile ( projectRoot : string , options : { file : string , encoding ?: BufferEncoding } = { file : '' , encoding : 'utf8' } ) {
55 const filePath = path . resolve ( projectRoot , options . file )
6- const readFn = ( path . extname ( filePath ) === '.json' && options . encoding !== null ) ? fs . readJsonAsync : fs . readFileAsync
76
87 // https://github.com/cypress-io/cypress/issues/1558
98 // If no encoding is specified, then Cypress has historically defaulted
109 // to `utf8`, because of it's focus on text files. This is in contrast to
1110 // NodeJs, which defaults to binary. We allow users to pass in `null`
1211 // to restore the default node behavior.
1312 try {
14- // @ts -ignore as we are mixing types with fs
15- const contents = await readFn ( filePath , options . encoding === undefined ? 'utf8' : options . encoding )
13+ let contents
14+
15+ if ( path . extname ( filePath ) === '.json' && options . encoding !== null ) {
16+ contents = await fs . readJsonAsync ( filePath , options . encoding === undefined ? 'utf8' : options . encoding )
17+ } else {
18+ contents = await fs . readFileAsync ( filePath , {
19+ encoding : options . encoding === undefined ? 'utf8' : options . encoding ,
20+ } )
21+ }
1622
1723 return {
1824 contents,
@@ -25,7 +31,7 @@ export async function readFile (projectRoot: string, options: { file: string, en
2531 }
2632}
2733
28- export async function readFiles ( projectRoot : string , options : { files : { path : string , encoding ?: string } [ ] } = { files : [ ] } ) {
34+ export async function readFiles ( projectRoot : string , options : { files : { path : string , encoding ?: BufferEncoding } [ ] } = { files : [ ] } ) {
2935 const files = await Promise . all ( options . files . map ( async ( file ) => {
3036 const { contents, filePath } = await readFile ( projectRoot , {
3137 file : file . path ,
@@ -42,7 +48,7 @@ export async function readFiles (projectRoot: string, options: { files: { path:
4248 return files
4349}
4450
45- export async function writeFile ( projectRoot : string , options : { fileName : string , contents : string , encoding ?: string , flag ?: string } = { fileName : '' , contents : '' , encoding : 'utf8' , flag : 'w' } ) {
51+ export async function writeFile ( projectRoot : string , options : { fileName : string , contents : string , encoding ?: BufferEncoding , flag ?: string } = { fileName : '' , contents : '' , encoding : 'utf8' , flag : 'w' } ) {
4652 const filePath = path . resolve ( projectRoot , options . fileName )
4753 const writeOptions = {
4854 encoding : options . encoding === undefined ? 'utf8' : options . encoding ,
0 commit comments