Skip to content

Commit 4d9de37

Browse files
committed
fix(resolveConfig): change to use the first found package.json with an non-empty config field
fixes #439
1 parent 9d27ad6 commit 4d9de37

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

packages/mongodb-memory-server-core/src/util/resolveConfig.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import camelCase from 'camelcase';
2-
import finder from 'find-package-json';
2+
import finder, { FinderIterator, Package } from 'find-package-json';
33
import debug from 'debug';
44
import * as path from 'path';
55

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

8+
// Workaround, because that package dosnt implement the default iterator
9+
interface CustomFinderIterator extends FinderIterator {
10+
[Symbol.iterator](): Iterator<Package>;
11+
}
12+
813
export enum ResolveConfigVariables {
914
DOWNLOAD_DIR = 'DOWNLOAD_DIR',
1015
PLATFORM = 'PLATFORM',
@@ -41,9 +46,17 @@ let packageJsonConfig: Record<string, string> = {};
4146
* @param directory Set an custom directory to search the config in (default: process.cwd())
4247
*/
4348
export function findPackageJson(directory?: string): Record<string, string> {
44-
const finderIterator = finder(directory || process.cwd()).next();
45-
log(`Using package.json at "${finderIterator.filename}"`);
46-
packageJsonConfig = finderIterator.value?.config?.mongodbMemoryServer ?? {};
49+
for (const found of finder(directory || process.cwd()) as CustomFinderIterator) {
50+
log(`findPackageJson: Found package.json"`);
51+
52+
if (Object.keys(found?.config?.mongodbMemoryServer ?? {}).length > 0) {
53+
log(`findPackageJson: Found package with non-empty config field"`);
54+
55+
// the optional chaining is needed, because typescript wont accept an "isNullOrUndefined" in the if with "&& Object.keys"
56+
packageJsonConfig = found?.config?.mongodbMemoryServer;
57+
break;
58+
}
59+
}
4760

4861
// block for all file-path resolving
4962
{

0 commit comments

Comments
 (0)