Skip to content

Commit 76e5aa7

Browse files
authored
Merge pull request #377 from hasezoey/fix/improveResolveConfig
fix(resolve-config): improve code
2 parents 0b03a9c + 8976574 commit 76e5aa7

File tree

6 files changed

+47
-54
lines changed

6 files changed

+47
-54
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,19 @@ Environment variables have higher priority than contents of package.json.
303303
}
304304
```
305305

306+
By default it uses the nearest (upwards) `package.json` to `process.cwd()`.
307+
To change this:
308+
309+
```ts
310+
import { findPackageJson } from "mongodb-memory-server-core/lib/util/resolve-config";
311+
312+
findPackageJson('/custom/path');
313+
314+
// OR
315+
316+
process.chdir('/custom/path'); // not recommended
317+
```
318+
306319
### Simple test with MongoClient
307320

308321
Take a look at this [test file](https://github.com/nodkz/mongodb-memory-server/blob/master/packages/mongodb-memory-server-core/src/__tests__/singleDB-test.ts).

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export default class MongoBinary {
148148
arch: resolveConfig('ARCH') || os.arch(),
149149
version: resolveConfig('VERSION') || LATEST_VERSION,
150150
systemBinary: resolveConfig('SYSTEM_BINARY'),
151-
checkMD5: envToBool(resolveConfig('MD5_CHECK') ?? ''),
151+
checkMD5: envToBool(resolveConfig('MD5_CHECK')),
152152
};
153153

154154
/** Provided Options combined with the Default Options */

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
}

yarn.lock

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3434,7 +3434,7 @@ debug@^3.1.0:
34343434
dependencies:
34353435
ms "^2.1.1"
34363436

3437-
debuglog@*, debuglog@^1.0.1:
3437+
debuglog@^1.0.1:
34383438
version "1.0.1"
34393439
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
34403440
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
@@ -5044,7 +5044,7 @@ import-local@^3.0.2:
50445044
pkg-dir "^4.2.0"
50455045
resolve-cwd "^3.0.0"
50465046

5047-
imurmurhash@*, imurmurhash@^0.1.4:
5047+
imurmurhash@^0.1.4:
50485048
version "0.1.4"
50495049
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
50505050
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
@@ -6429,11 +6429,6 @@ lockfile@^1.0.4:
64296429
dependencies:
64306430
signal-exit "^3.0.2"
64316431

6432-
lodash._baseindexof@*:
6433-
version "3.1.0"
6434-
resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
6435-
integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=
6436-
64376432
lodash._baseuniq@~4.6.0:
64386433
version "4.6.0"
64396434
resolved "https://registry.yarnpkg.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8"
@@ -6442,33 +6437,11 @@ lodash._baseuniq@~4.6.0:
64426437
lodash._createset "~4.0.0"
64436438
lodash._root "~3.0.0"
64446439

6445-
lodash._bindcallback@*:
6446-
version "3.0.1"
6447-
resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
6448-
integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=
6449-
6450-
lodash._cacheindexof@*:
6451-
version "3.0.2"
6452-
resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
6453-
integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=
6454-
6455-
lodash._createcache@*:
6456-
version "3.1.2"
6457-
resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
6458-
integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
6459-
dependencies:
6460-
lodash._getnative "^3.0.0"
6461-
64626440
lodash._createset@~4.0.0:
64636441
version "4.0.3"
64646442
resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
64656443
integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY=
64666444

6467-
lodash._getnative@*, lodash._getnative@^3.0.0:
6468-
version "3.9.1"
6469-
resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
6470-
integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=
6471-
64726445
lodash._reinterpolate@^3.0.0:
64736446
version "3.0.0"
64746447
resolved "https://registry.yarnpkg.com/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz#0ccf2d89166af03b3663c796538b75ac6e114d9d"
@@ -6519,11 +6492,6 @@ [email protected]:
65196492
resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe"
65206493
integrity sha1-vMbEmkKihA7Zl/Mj6tpezRguC/4=
65216494

6522-
lodash.restparam@*:
6523-
version "3.6.1"
6524-
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
6525-
integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=
6526-
65276495
lodash.set@^4.3.2:
65286496
version "4.3.2"
65296497
resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"

0 commit comments

Comments
 (0)