Skip to content

Commit

Permalink
fix(resolveConfig): change to use the first found package.json with a…
Browse files Browse the repository at this point in the history
…n non-empty config field

fixes #439
  • Loading branch information
hasezoey committed Feb 21, 2021
1 parent 9d27ad6 commit 4d9de37
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions packages/mongodb-memory-server-core/src/util/resolveConfig.ts
@@ -1,10 +1,15 @@
import camelCase from 'camelcase';
import finder from 'find-package-json';
import finder, { FinderIterator, Package } from 'find-package-json';
import debug from 'debug';
import * as path from 'path';

const log = debug('MongoMS:ResolveConfig');

// Workaround, because that package dosnt implement the default iterator
interface CustomFinderIterator extends FinderIterator {
[Symbol.iterator](): Iterator<Package>;
}

export enum ResolveConfigVariables {
DOWNLOAD_DIR = 'DOWNLOAD_DIR',
PLATFORM = 'PLATFORM',
Expand Down Expand Up @@ -41,9 +46,17 @@ let packageJsonConfig: Record<string, string> = {};
* @param directory Set an custom directory to search the config in (default: process.cwd())
*/
export function findPackageJson(directory?: string): Record<string, string> {
const finderIterator = finder(directory || process.cwd()).next();
log(`Using package.json at "${finderIterator.filename}"`);
packageJsonConfig = finderIterator.value?.config?.mongodbMemoryServer ?? {};
for (const found of finder(directory || process.cwd()) as CustomFinderIterator) {
log(`findPackageJson: Found package.json"`);

if (Object.keys(found?.config?.mongodbMemoryServer ?? {}).length > 0) {
log(`findPackageJson: Found package with non-empty config field"`);

// the optional chaining is needed, because typescript wont accept an "isNullOrUndefined" in the if with "&& Object.keys"
packageJsonConfig = found?.config?.mongodbMemoryServer;
break;
}
}

// block for all file-path resolving
{
Expand Down

0 comments on commit 4d9de37

Please sign in to comment.