1
- import fs from 'fs' ;
1
+ /* eslint-disable node/prefer-promises/fs */
2
+ import { readFile , access } from 'fs' ;
2
3
import { resolve , dirname } from 'path' ;
3
4
4
5
import type { PreprocessorArgs } from '../types' ;
5
6
import { getLanguage } from './language' ;
6
7
import { isValidLocalPath } from './utils' ;
7
8
8
- const { access, readFile } = fs . promises ;
9
-
10
9
const resolveSrc = ( importerFile : string , srcPath : string ) =>
11
10
resolve ( dirname ( importerFile ) , srcPath ) ;
12
11
12
+ const getSrcContent = ( file : string ) : Promise < string > => {
13
+ return new Promise ( ( resolve , reject ) => {
14
+ readFile ( file , ( error : Error , data : unknown ) => {
15
+ // istanbul ignore if
16
+ if ( error ) reject ( error ) ;
17
+ else resolve ( data . toString ( ) ) ;
18
+ } ) ;
19
+ } ) ;
20
+ } ;
21
+
13
22
async function doesFileExist ( file : string ) {
14
- return access ( file , fs . constants . F_OK )
15
- . then ( ( ) => true )
16
- . catch ( ( ) => false ) ;
23
+ return new Promise ( ( resolve ) => access ( file , 0 , ( err ) => resolve ( ! err ) ) ) ;
17
24
}
18
25
19
26
export const getTagInfo = async ( {
@@ -38,10 +45,10 @@ export const getTagInfo = async ({
38
45
if ( isValidLocalPath ( path ) ) {
39
46
path = resolveSrc ( filename , path ) ;
40
47
if ( await doesFileExist ( path ) ) {
41
- content = ( await readFile ( path ) ) . toString ( ) ;
48
+ content = await getSrcContent ( path ) ;
42
49
dependencies . push ( path ) ;
43
50
} else {
44
- console . warn ( `[svelte-preprocess] The file "${ path } " was not found.` ) ;
51
+ console . warn ( `[svelte-preprocess] The file "${ path } " was not found.` ) ;
45
52
}
46
53
}
47
54
}
0 commit comments