Skip to content

Commit 047ebcb

Browse files
authored
fix: chdir back to the starting directory before exit (#167)
1 parent c9ae036 commit 047ebcb

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/index.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { readFileSync } from 'fs';
2+
import path from 'path';
23
import { toJS, resolve, versionCheck } from './util';
34
import { Document } from './document';
45
import { parse } from './parser';
56

67
import type { AsyncAPIObject } from './spec-types';
78

9+
// remember the directory where execution of the program started
10+
const originDir = String(process.cwd());
11+
812
/**
913
*
1014
* @param {string | string[]} files One or more relative/absolute paths to
@@ -87,9 +91,9 @@ export default async function bundle(
8791
}
8892

8993
if (options.baseDir && typeof options.baseDir === 'string') {
90-
process.chdir(options.baseDir);
94+
process.chdir(path.resolve(originDir, options.baseDir));
9195
} 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
9397
}
9498

9599
const readFiles = files.map(file => readFileSync(file, 'utf-8')); // eslint-disable-line
@@ -114,6 +118,11 @@ export default async function bundle(
114118
options
115119
);
116120

121+
// return to the starting directory before finishing the execution
122+
if (options.baseDir) {
123+
process.chdir(originDir);
124+
}
125+
117126
return new Document(resolvedJsons, options.base);
118127
}
119128

0 commit comments

Comments
 (0)