Skip to content

Commit 22d765d

Browse files
committed
fix(resolve-config): improve code
- envToBool: default to empty string (improve coverage) - combine functions "getPackageJson" and "reInitializePackageJson" - rename function "reInitializePackageJson" to "findPackageJson" (backwards compatible) - findPackageJson: add tsdoc - setDefaultValue: add tsdoc - findPackageJson: log what package.json (path) is used - rename variable "packageJson" to "packageJsonConfig" - packageJsonConfig: default to empty object - other files: apply "envToBool" defaulting - resolve-config-test: apply function name change
1 parent a6da2ed commit 22d765d

File tree

4 files changed

+32
-20
lines changed

4 files changed

+32
-20
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export default class MongoBinary {
125125
arch: resolveConfig('ARCH') || os.arch(),
126126
version: resolveConfig('VERSION') || LATEST_VERSION,
127127
systemBinary: resolveConfig('SYSTEM_BINARY'),
128-
checkMD5: envToBool(resolveConfig('MD5_CHECK') ?? ''),
128+
checkMD5: envToBool(resolveConfig('MD5_CHECK')),
129129
};
130130

131131
const options = { ...defaultOptions, ...opts };

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export default class MongoBinaryDownload {
5252
this.arch = arch ?? os.arch();
5353
this.version = version ?? LATEST_VERSION;
5454
this.downloadDir = path.resolve(downloadDir || 'mongodb-download');
55-
this.checkMD5 = checkMD5 ?? envToBool(resolveConfig('MD5_CHECK') ?? '');
55+
this.checkMD5 = checkMD5 ?? envToBool(resolveConfig('MD5_CHECK'));
5656
this.dlProgress = {
5757
current: 0,
5858
length: 0,

packages/mongodb-memory-server-core/src/util/__tests__/resolve-config-test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from 'fs';
22
import * as tmp from 'tmp';
33
import { promisify } from 'util';
4-
import resolveConfig, { reInitializePackageJson } from '../resolve-config';
4+
import resolveConfig, { findPackageJson } from '../resolve-config';
55

66
tmp.setGracefulCleanup();
77
const mkdirAsync = promisify(fs.mkdir);
@@ -61,21 +61,21 @@ describe('resolveConfig', () => {
6161

6262
test('in project', () => {
6363
process.chdir(`${tmpObj.name}/project`);
64-
reInitializePackageJson();
64+
findPackageJson();
6565
const got = resolveConfig('VERSION');
6666
expect(got).toBe('3.0.0');
6767
});
6868

6969
test('in subproject', () => {
7070
process.chdir(`${tmpObj.name}/project/subproject`);
71-
reInitializePackageJson();
71+
findPackageJson();
7272
const got = resolveConfig('VERSION');
7373
expect(got).toBe('4.0.0');
7474
});
7575

7676
test('with explicit directory in reInitializePackageJson', () => {
7777
process.chdir(`${tmpObj.name}/project`);
78-
reInitializePackageJson(`${tmpObj.name}/project/subproject`);
78+
findPackageJson(`${tmpObj.name}/project/subproject`);
7979
const got = resolveConfig('VERSION');
8080
expect(got).toBe('4.0.0');
8181
});
Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
11
import camelCase from 'camelcase';
2-
import finder, { Package } from 'find-package-json';
2+
import finder from 'find-package-json';
33
import debug from 'debug';
44

5+
const log = debug('MongoMS:ResolveConfig');
6+
57
const ENV_CONFIG_PREFIX = 'MONGOMS_';
68
const defaultValues = new Map<string, string>();
79

8-
function getPackageJson(directory: string): Package | undefined {
9-
const pjIterator = finder(directory);
10-
return pjIterator.next().value;
11-
}
12-
10+
/**
11+
* Set an Default value for an specific key
12+
* Mostly only used internally (for the "global-x.x" packages)
13+
* @param key The Key the default value should be assigned to
14+
* @param value The Value what the default should be
15+
*/
1316
export function setDefaultValue(key: string, value: string): void {
1417
defaultValues.set(key, value);
1518
}
1619

17-
let packageJson: Package | undefined;
18-
export function reInitializePackageJson(directory?: string): void {
19-
packageJson = getPackageJson(directory || process.cwd());
20+
let packageJsonConfig: {
21+
[key: string]: string;
22+
} = {};
23+
/**
24+
* Find the nearest package.json for the provided directory
25+
* @param directory Set an custom directory to search the config in (default: process.cwd())
26+
*/
27+
export function findPackageJson(directory?: string): void {
28+
const finderIterator = finder(directory || process.cwd()).next();
29+
log(`Using package.json at "${finderIterator.filename}"`);
30+
packageJsonConfig = finderIterator.value?.config?.mongodbMemoryServer ?? {};
2031
}
21-
reInitializePackageJson();
32+
export const reInitializePackageJson = findPackageJson; // TODO: remove this line on next minor version
33+
findPackageJson();
2234

2335
/**
2436
* Resolve "variableName" with a prefix of "ENV_CONFIG_PREFIX"
@@ -27,7 +39,7 @@ reInitializePackageJson();
2739
export default function resolveConfig(variableName: string): string | undefined {
2840
return (
2941
process.env[`${ENV_CONFIG_PREFIX}${variableName}`] ??
30-
packageJson?.config?.mongodbMemoryServer?.[camelCase(variableName)] ??
42+
packageJsonConfig?.[camelCase(variableName)] ??
3143
defaultValues.get(variableName)
3244
);
3345
}
@@ -36,11 +48,11 @@ export default function resolveConfig(variableName: string): string | undefined
3648
* Convert "1, on, yes, true" to true (otherwise false)
3749
* @param env The String / Environment Variable to check
3850
*/
39-
export function envToBool(env: string): boolean {
51+
export function envToBool(env: string = ''): boolean {
4052
return ['1', 'on', 'yes', 'true'].indexOf(env.toLowerCase()) !== -1;
4153
}
4254

43-
// enable debug "MONGOMS_DEBUG" is true
44-
if (envToBool(resolveConfig('DEBUG') ?? '')) {
55+
// enable debug if "MONGOMS_DEBUG" is true
56+
if (envToBool(resolveConfig('DEBUG'))) {
4557
debug.enable('MongoMS:*');
4658
}

0 commit comments

Comments
 (0)