22// The path is expected to either be absolute or relative to the current working directory.
33
44import { readFile , stat } from "node:fs/promises" ;
5- import { resolve } from "node:path" ;
65import { parse } from "yaml" ;
6+ import { resolve } from "node:path" ;
77
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" ] ;
1010
1111const rkebab = / - ( [ a - z ] ) / g;
1212
@@ -22,7 +22,28 @@ export default async function readYAML( path ) {
2222 return { } ;
2323 }
2424
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+ }
2647 let config = await parse ( contents ) ;
2748
2849 if ( config . version !== CONFIG_VERSION ) {
0 commit comments