1
1
import { readFileSync } from 'fs' ;
2
+ import path from 'path' ;
2
3
import { toJS , resolve , versionCheck } from './util' ;
3
4
import { Document } from './document' ;
4
5
import { parse } from './parser' ;
5
6
6
7
import type { AsyncAPIObject } from './spec-types' ;
7
8
9
+ // remember the directory where execution of the program started
10
+ const originDir = String ( process . cwd ( ) ) ;
11
+
8
12
/**
9
13
*
10
14
* @param {string | string[] } files One or more relative/absolute paths to
@@ -87,9 +91,9 @@ export default async function bundle(
87
91
}
88
92
89
93
if ( options . baseDir && typeof options . baseDir === 'string' ) {
90
- process . chdir ( options . baseDir ) ;
94
+ process . chdir ( path . resolve ( originDir , options . baseDir ) ) ;
91
95
} else if ( options . baseDir && Array . isArray ( options . baseDir ) ) {
92
- process . chdir ( String ( options . baseDir [ 0 ] ) ) ; // guard against passing an array
96
+ process . chdir ( path . resolve ( originDir , String ( options . baseDir [ 0 ] ) ) ) ; // guard against passing an array
93
97
}
94
98
95
99
const readFiles = files . map ( file => readFileSync ( file , 'utf-8' ) ) ; // eslint-disable-line
@@ -114,6 +118,11 @@ export default async function bundle(
114
118
options
115
119
) ;
116
120
121
+ // return to the starting directory before finishing the execution
122
+ if ( options . baseDir ) {
123
+ process . chdir ( originDir ) ;
124
+ }
125
+
117
126
return new Document ( resolvedJsons , options . base ) ;
118
127
}
119
128
0 commit comments