|
@@ -1,3 +1,4 @@
|
|
|
+const fs = require('fs')
|
|
|
const express = require('express')
|
|
|
|
|
|
const Grant = require('grant').express()
|
|
@@ -48,6 +49,8 @@ module.exports.errors = { ProviderApiError, ProviderAuthError }
|
|
|
* @param {object} options
|
|
|
*/
|
|
|
module.exports.app = (options = {}) => {
|
|
|
+ validateConfig(options)
|
|
|
+
|
|
|
options = merge({}, defaultOptions, options)
|
|
|
const providers = providerManager.getDefaultProviders(options)
|
|
|
providerManager.addProviderOptions(options, grantConfig)
|
|
@@ -300,3 +303,38 @@ const maskLogger = (companionOptions) => {
|
|
|
|
|
|
logger.setMaskables(secrets)
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ * validates that the mandatory companion options are set.
|
|
|
+ * If it is invalid, it will console an error of unset options and exits the process.
|
|
|
+ * If it is valid, nothing happens.
|
|
|
+ *
|
|
|
+ * @param {object} companionOptions
|
|
|
+ */
|
|
|
+const validateConfig = (companionOptions) => {
|
|
|
+ const mandatoryOptions = ['secret', 'filePath', 'server.host']
|
|
|
+
|
|
|
+ const unspecified = []
|
|
|
+
|
|
|
+ mandatoryOptions.forEach((i) => {
|
|
|
+ const value = i.split('.').reduce((prev, curr) => prev ? prev[curr] : undefined, companionOptions)
|
|
|
+
|
|
|
+ if (!value) unspecified.push(`"${i}"`)
|
|
|
+ })
|
|
|
+
|
|
|
+
|
|
|
+ if (unspecified.length) {
|
|
|
+ const messagePrefix = 'Please specify the following options to use companion:'
|
|
|
+ throw new Error(`${messagePrefix}\n${unspecified.join(',\n')}`)
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ fs.accessSync(`${companionOptions.filePath}`, fs.R_OK | fs.W_OK)
|
|
|
+ } catch (err) {
|
|
|
+ throw new Error(
|
|
|
+ `No access to "${companionOptions.filePath}". Please ensure the directory exists and with read/write permissions.`
|
|
|
+ )
|
|
|
+ }
|
|
|
+}
|