Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: chdir back to the starting directory before exit #167

13 changes: 11 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { readFileSync } from 'fs';
import path from 'path';
import { toJS, resolve, versionCheck } from './util';
import { Document } from './document';
import { parse } from './parser';

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

// remember the directory where execution of the program started
const originDir = String(process.cwd());

/**
*
* @param {string | string[]} files One or more relative/absolute paths to
Expand Down Expand Up @@ -87,9 +91,9 @@ export default async function bundle(
}

if (options.baseDir && typeof options.baseDir === 'string') {
process.chdir(options.baseDir);
process.chdir(path.resolve(originDir, options.baseDir));
} else if (options.baseDir && Array.isArray(options.baseDir)) {
process.chdir(String(options.baseDir[0])); // guard against passing an array
process.chdir(path.resolve(originDir, String(options.baseDir[0]))); // guard against passing an array
}

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

// return to the starting directory before finishing the execution
if (options.baseDir) {
process.chdir(originDir);
}

return new Document(resolvedJsons, options.base);
}

Expand Down
Loading