Skip to content

Commit da76bb9

Browse files
committed
Add warning if db adapter package isn't found
1 parent 616b9af commit da76bb9

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/db.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
const flatfile = require('flat-file-db')
22
const promise = require('promise')
3+
const { repeatCharacter } = require('./utils')
34

45
let adapter
56

67
if (process.env.DB_ADAPTER) {
7-
adapter = require('micro-analytics-adapter-' + process.env.DB_ADAPTER)
8+
const adapterName = 'micro-analytics-adapter-' + process.env.DB_ADAPTER
9+
try {
10+
adapter = require(adapterName)
11+
} catch (err) {
12+
if (err.code === 'MODULE_NOT_FOUND') {
13+
// Console.error a warning message, but normally exit the process to avoid printing ugly npm ERR lines and stack trace.
14+
console.error(`\n${repeatCharacter(' ', 22)}⚠️ ERROR ⚠️\n${repeatCharacter('-', 55)}\nYou specified "${process.env.DB_ADAPTER}" as the DB_ADAPTER, but no package\ncalled "${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`)
15+
process.exit(0)
16+
}
17+
}
818
} else {
919
adapter = require('./flat-file-adapter')
1020
}

src/utils.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,8 @@ const pushView = async (key, view) => {
2424
}
2525
}
2626

27+
const repeatCharacter = (char, n) => `${Array(n + 1).join(char)}`
28+
2729
module.exports = exports
2830
exports.pushView = pushView
31+
exports.repeatCharacter = repeatCharacter

0 commit comments

Comments
 (0)