2
2
// The path is expected to either be absolute or relative to the current working directory.
3
3
4
4
import { readFile , stat } from "node:fs/promises" ;
5
- import { resolve } from "node:path" ;
6
5
import { parse } from "yaml" ;
6
+ import { resolve } from "node:path" ;
7
7
8
- const CONFIG_VERSION = 1 ;
9
- const DEFAULT_YAML_PATHS = [ "jtr.yml" , "jtr.yaml" ] ;
8
+ export const CONFIG_VERSION = 1 ;
9
+ export const DEFAULT_YAML_PATHS = [ "jtr.yml" , "jtr.yaml" ] ;
10
10
11
11
const rkebab = / - ( [ a - z ] ) / g;
12
12
@@ -22,7 +22,28 @@ export default async function readYAML( path ) {
22
22
return { } ;
23
23
}
24
24
25
- const contents = await readFile ( resolve ( process . cwd ( ) , path ) , "utf8" ) ;
25
+ if ( ! path . endsWith ( ".yml" ) && ! path . endsWith ( ".yaml" ) ) {
26
+ throw new Error ( "Invalid configuration file. Expected a YAML file." ) ;
27
+ }
28
+
29
+ let contents ;
30
+
31
+ // Check if path is absolute
32
+ if ( path . startsWith ( "/" ) || path . startsWith ( "\\" ) || path . includes ( ":" ) ) {
33
+ if ( ! await stat ( path ) . catch ( ( ) => false ) ) {
34
+ throw new Error ( `Configuration file not found: ${ path } ` ) ;
35
+ }
36
+
37
+ contents = await readFile ( path , "utf8" ) ;
38
+ } else {
39
+ path = resolve ( process . cwd ( ) , path ) ;
40
+
41
+ if ( ! await stat ( path ) . catch ( ( ) => false ) ) {
42
+ throw new Error ( `Configuration file not found: ${ path } ` ) ;
43
+ }
44
+
45
+ contents = await readFile ( path , "utf8" ) ;
46
+ }
26
47
let config = await parse ( contents ) ;
27
48
28
49
if ( config . version !== CONFIG_VERSION ) {
0 commit comments