Skip to content

Commit c8dbbb9

Browse files
kjarmickinodkz
authored andcommitted
feat: configure installation from package.json
* feat(install): configure installation from package.json * improvements after review
1 parent 84865f2 commit c8dbbb9

File tree

11 files changed

+128
-53
lines changed

11 files changed

+128
-53
lines changed

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ const mongod = new MongoMemoryServer({
110110
});
111111
```
112112
113-
Also you can use the environment variables for configure installation process
113+
You can also use the environment variables or package.json's `config` section to configure installation process.
114+
Environment variables have higher priority than contents of package.json.
114115
115116
```txt
116117
MONGOMS_DOWNLOAD_DIR=/path/to/mongodb/binaries
@@ -125,6 +126,22 @@ MONGOMS_MD5_CHECK=1 # if you want to make MD5 check of downloaded binary.
125126
# Passed constructor parameter `binary.checkMD5` has higher priority.
126127
```
127128

129+
```json
130+
"config": {
131+
"mongodbMemoryServer": {
132+
"downloadDir": "/path/to/mongodb/binaries",
133+
"platform": "linux",
134+
"arch": "x64",
135+
"version": "3",
136+
"debug": "1",
137+
"downloadMirror": "url",
138+
"disablePostinstall": "1",
139+
"systemBinary": "/usr/local/bin/mongod",
140+
"md5Check": "1"
141+
}
142+
}
143+
```
144+
128145
### Replica Set start:
129146

130147
```js

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"@types/decompress": "^4.2.3",
99
"@types/dedent": "^0.7.0",
1010
"@types/find-cache-dir": "^2.0.0",
11+
"@types/find-package-json": "^1.1.0",
1112
"@types/get-port": "^4.0.1",
1213
"@types/getos": "^3.0.0",
1314
"@types/jest": "^24.0.11",
@@ -34,10 +35,12 @@
3435
"typescript": "^3.3.3333"
3536
},
3637
"dependencies": {
38+
"camelcase": "^5.3.1",
3739
"debug": "^4.1.1",
3840
"decompress": "^4.2.0",
3941
"dedent": "^0.7.0",
4042
"find-cache-dir": "^2.0.0",
43+
"find-package-json": "^1.2.0",
4144
"get-port": "^4.2.0",
4245
"getos": "^3.1.1",
4346
"https-proxy-agent": "^2.2.1",

packages/mongodb-memory-server-core/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@
2424
},
2525
"homepage": "https://github.com/nodkz/mongodb-memory-server",
2626
"dependencies": {
27+
"camelcase": "^5.3.1",
2728
"debug": "^4.1.1",
2829
"decompress": "^4.2.0",
2930
"dedent": "^0.7.0",
3031
"find-cache-dir": "^2.0.0",
32+
"find-package-json": "^1.2.0",
3133
"get-port": "^4.2.0",
3234
"getos": "^3.1.1",
3335
"https-proxy-agent": "^2.2.1",

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import dedent from 'dedent';
99
import { promisify } from 'util';
1010
import MongoBinaryDownload from './MongoBinaryDownload';
1111
import { DebugFn } from '../types';
12+
import resolveConfig from './resolve-config';
1213

1314
// TODO: return back `latest` version when it will be fixed in MongoDB distro (for now use 4.0.3 😂)
1415
// More details in https://github.com/nodkz/mongodb-memory-server/issues/131
@@ -103,6 +104,7 @@ export default class MongoBinary {
103104

104105
static async getPath(opts: MongoBinaryOpts = {}): Promise<string> {
105106
const legacyDLDir = path.resolve(os.homedir(), '.cache/mongodb-binaries');
107+
const envDebug = resolveConfig('DEBUG');
106108

107109
// if we're in postinstall script, npm will set the cwd too deep
108110
let nodeModulesDLDir = process.cwd();
@@ -112,7 +114,7 @@ export default class MongoBinary {
112114

113115
const defaultOptions = {
114116
downloadDir:
115-
process.env.MONGOMS_DOWNLOAD_DIR ||
117+
resolveConfig('DOWNLOAD_DIR') ||
116118
(fs.existsSync(legacyDLDir)
117119
? legacyDLDir
118120
: path.resolve(
@@ -122,14 +124,12 @@ export default class MongoBinary {
122124
}) || '',
123125
'mongodb-binaries'
124126
)),
125-
platform: process.env.MONGOMS_PLATFORM || os.platform(),
126-
arch: process.env.MONGOMS_ARCH || os.arch(),
127-
version: process.env.MONGOMS_VERSION || LATEST_VERSION,
128-
systemBinary: process.env.MONGOMS_SYSTEM_BINARY,
127+
platform: resolveConfig('PLATFORM') || os.platform(),
128+
arch: resolveConfig('ARCH') || os.arch(),
129+
version: resolveConfig('VERSION') || LATEST_VERSION,
130+
systemBinary: resolveConfig('SYSTEM_BINARY'),
129131
debug:
130-
typeof process.env.MONGOMS_DEBUG === 'string'
131-
? ['1', 'on', 'yes', 'true'].indexOf(process.env.MONGOMS_DEBUG.toLowerCase()) !== -1
132-
: false,
132+
typeof envDebug === 'string' ? ['1', 'on', 'yes', 'true'].indexOf(envDebug) !== -1 : false,
133133
};
134134

135135
if (opts.debug) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import getos from 'getos';
22
import { execSync } from 'child_process';
3+
import resolveConfig from './resolve-config';
34

45
export interface MongoBinaryDownloadUrlOpts {
56
version: string;
@@ -23,7 +24,7 @@ export default class MongoBinaryDownloadUrl {
2324

2425
async getDownloadUrl(): Promise<string> {
2526
const archive = await this.getArchiveName();
26-
return `${process.env.MONGOMS_DOWNLOAD_MIRROR || 'https://fastdl.mongodb.org'}/${
27+
return `${resolveConfig('DOWNLOAD_MIRROR') || 'https://fastdl.mongodb.org'}/${
2728
this.platform
2829
}/${archive}`;
2930
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import camelCase from 'camelcase';
2+
import finder, { Package } from 'find-package-json';
3+
4+
const ENV_CONFIG_PREFIX = 'MONGOMS_';
5+
const defaultValues = new Map<string, string>();
6+
7+
function getPackageJson(): Package | undefined {
8+
const pjIterator = finder(__dirname);
9+
let next = pjIterator.next();
10+
let packageJson = next.value;
11+
while (!next.done) {
12+
packageJson = next.value;
13+
next = pjIterator.next();
14+
}
15+
return packageJson;
16+
}
17+
const packageJson = getPackageJson();
18+
19+
export function setDefaultValue(key: string, value: string): void {
20+
defaultValues.set(key, value);
21+
}
22+
23+
export default function resolveConfig(variableName: string): string | undefined {
24+
return (
25+
process.env[`${ENV_CONFIG_PREFIX}${variableName}`] ||
26+
(packageJson &&
27+
packageJson.config &&
28+
packageJson.config.mongodbMemoryServer &&
29+
packageJson.config.mongodbMemoryServer[camelCase(variableName)]) ||
30+
defaultValues.get(variableName)
31+
);
32+
}

packages/mongodb-memory-server-global-3.4/postinstall.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
if (!process.env.MONGOMS_DOWNLOAD_DIR) {
2-
process.env.MONGOMS_DOWNLOAD_DIR = require('path').resolve(
3-
require('os').homedir(),
4-
'.cache',
5-
'mongodb-binaries'
6-
);
7-
}
8-
if (!process.env.MONGOMS_VERSION) {
9-
process.env.MONGOMS_VERSION = '3.4.20'; // don't use `latest` it's nightly build
10-
}
11-
121
function isModuleExists(name) {
132
try {
143
return !!require.resolve(name);
@@ -17,6 +6,20 @@ function isModuleExists(name) {
176
}
187
}
198

9+
if (!isModuleExists('../mongodb-memory-server-core/lib/util/resolve-config')) {
10+
console.log('Could not resolve postinstall configuration');
11+
return;
12+
}
13+
const setDefaultValue = require('../mongodb-memory-server-core/lib/util/resolve-config')
14+
.setDefaultValue;
15+
16+
setDefaultValue(
17+
'DOWNLOAD_DIR',
18+
require('path').resolve(require('os').homedir(), '.cache', 'mongodb-binaries')
19+
);
20+
21+
setDefaultValue('VERSION', '3.4.20'); // don't use `latest` it's nightly build
22+
2023
const script = '../mongodb-memory-server/postinstall.js';
2124
if (isModuleExists(script)) {
2225
require(script);

packages/mongodb-memory-server-global-3.6/postinstall.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
if (!process.env.MONGOMS_DOWNLOAD_DIR) {
2-
process.env.MONGOMS_DOWNLOAD_DIR = require('path').resolve(
3-
require('os').homedir(),
4-
'.cache',
5-
'mongodb-binaries'
6-
);
7-
}
8-
if (!process.env.MONGOMS_VERSION) {
9-
process.env.MONGOMS_VERSION = '3.6.12'; // don't use `latest` it's nightly build
10-
}
11-
121
function isModuleExists(name) {
132
try {
143
return !!require.resolve(name);
@@ -17,6 +6,20 @@ function isModuleExists(name) {
176
}
187
}
198

9+
if (!isModuleExists('../mongodb-memory-server-core/lib/util/resolve-config')) {
10+
console.log('Could not resolve postinstall configuration');
11+
return;
12+
}
13+
const setDefaultValue = require('../mongodb-memory-server-core/lib/util/resolve-config')
14+
.setDefaultValue;
15+
16+
setDefaultValue(
17+
'DOWNLOAD_DIR',
18+
require('path').resolve(require('os').homedir(), '.cache', 'mongodb-binaries')
19+
);
20+
21+
setDefaultValue('VERSION', '3.6.12'); // don't use `latest` it's nightly build
22+
2023
const script = '../mongodb-memory-server/postinstall.js';
2124
if (isModuleExists(script)) {
2225
require(script);

packages/mongodb-memory-server-global-4.0/postinstall.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
if (!process.env.MONGOMS_DOWNLOAD_DIR) {
2-
process.env.MONGOMS_DOWNLOAD_DIR = require('path').resolve(
3-
require('os').homedir(),
4-
'.cache',
5-
'mongodb-binaries'
6-
);
7-
}
8-
if (!process.env.MONGOMS_VERSION) {
9-
process.env.MONGOMS_VERSION = '4.0.8'; // don't use `latest` it's nightly build
10-
}
11-
121
function isModuleExists(name) {
132
try {
143
return !!require.resolve(name);
@@ -17,6 +6,20 @@ function isModuleExists(name) {
176
}
187
}
198

9+
if (!isModuleExists('../mongodb-memory-server-core/lib/util/resolve-config')) {
10+
console.log('Could not resolve postinstall configuration');
11+
return;
12+
}
13+
const setDefaultValue = require('../mongodb-memory-server-core/lib/util/resolve-config')
14+
.setDefaultValue;
15+
16+
setDefaultValue(
17+
'DOWNLOAD_DIR',
18+
require('path').resolve(require('os').homedir(), '.cache', 'mongodb-binaries')
19+
);
20+
21+
setDefaultValue('VERSION', '4.0.8'); // don't use `latest` it's nightly build
22+
2023
const script = '../mongodb-memory-server/postinstall.js';
2124
if (isModuleExists(script)) {
2225
require(script);

packages/mongodb-memory-server-global/postinstall.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
if (!process.env.MONGOMS_DOWNLOAD_DIR) {
2-
process.env.MONGOMS_DOWNLOAD_DIR = require('path').resolve(
3-
require('os').homedir(),
4-
'.cache',
5-
'mongodb-binaries'
6-
);
7-
}
8-
91
function isModuleExists(name) {
102
try {
113
return !!require.resolve(name);
@@ -14,6 +6,18 @@ function isModuleExists(name) {
146
}
157
}
168

9+
if (!isModuleExists('../mongodb-memory-server-core/lib/util/resolve-config')) {
10+
console.log('Could not resolve postinstall configuration');
11+
return;
12+
}
13+
const setDefaultValue = require('../mongodb-memory-server-core/lib/util/resolve-config')
14+
.setDefaultValue;
15+
16+
setDefaultValue(
17+
'DOWNLOAD_DIR',
18+
require('path').resolve(require('os').homedir(), '.cache', 'mongodb-binaries')
19+
);
20+
1721
const script = '../mongodb-memory-server/postinstall.js';
1822
if (isModuleExists(script)) {
1923
require(script);

0 commit comments

Comments
 (0)