Skip to content

Commit 73cd74d

Browse files
committed
refactor: Move check if adapter exists to parseArgs
1 parent 78718c8 commit 73cd74d

File tree

2 files changed

+12
-17
lines changed

2 files changed

+12
-17
lines changed

src/db.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,8 @@
11
const promise = require('promise')
22

33
function initDbAdapter(options) {
4-
let adapter
5-
const repeatCharacter = (char, n) => `${Array(n + 1).join(char)}`
64
const adapterName = options.adapter
7-
8-
try {
9-
adapter = require(`micro-analytics-adapter-${adapterName}`)
10-
} catch (err) {
11-
if (err.code === 'MODULE_NOT_FOUND') {
12-
// Console.error a warning message, but normally exit the process to avoid printing ugly npm ERR lines and stack trace.
13-
console.error(`\n${repeatCharacter(' ', 22)}⚠️ ERROR ⚠️\n${repeatCharacter('-', 55)}\nYou specified "${adapterName}" as the DB_ADAPTER, but no package\ncalled "micro-analytics-adapter-${adapterName}" was found.\n\nPlease make sure you spelled the name correctly and\nhave "npm install"ed the necessary adapter package!\n${repeatCharacter('-', 55)}\n`)
14-
process.exit(0)
15-
} else {
16-
throw err
17-
}
18-
}
5+
const adapter = require(`micro-analytics-adapter-${adapterName}`)
196

207

218
module.exports.get = adapter.get

src/parseArgs.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const args = require('args');
22

3+
const repeatCharacter = (char, n) => `${Array(n + 1).join(char)}`
4+
35
function adapterNameFromArgs(argv) {
46
for (let i = 0; i < argv.length; i++) {
57
const arg = argv[i]
@@ -23,9 +25,15 @@ module.exports = function parseArgs(argv) {
2325
if (adapter.options) {
2426
options = adapter.options
2527
}
26-
} catch (error) {
27-
if (error.code !== "MODULE_NOT_FOUND") {
28-
throw error;
28+
} catch (err) {
29+
if (err.code === 'MODULE_NOT_FOUND') {
30+
if (process.env.NODE_ENV !== 'test') {
31+
// Console.error a warning message, but normally exit the process to avoid printing ugly npm ERR lines and stack trace.
32+
console.error(`\n${repeatCharacter(' ', 22)}⚠️ ERROR ⚠️\n${repeatCharacter('-', 55)}\nYou specified "${adapterName}" as the DB_ADAPTER, but no package\ncalled "micro-analytics-adapter-${adapterName}" was found.\n\nPlease make sure you spelled the name correctly and\nhave "npm install"ed the necessary adapter package!\n${repeatCharacter('-', 55)}\n`)
33+
process.exit(0)
34+
}
35+
} else {
36+
throw err
2937
}
3038
}
3139

0 commit comments

Comments
 (0)