From fb746966affd3cac17b2374e33ce22970d9bfbb0 Mon Sep 17 00:00:00 2001 From: Mark van Renswoude Date: Wed, 2 May 2018 20:54:27 +0200 Subject: [PATCH] Changed config from example to defaults file, merged with config.js --- README.md | 2 +- config.example.js => config.defaults.js | 15 +++++++++++++++ index.js | 18 ++++++++++++++++-- lib/api/admin.js | 1 - lib/api/token.js | 1 - lib/api/upload.js | 1 - lib/repository/code.js | 1 - lib/repository/upload.js | 1 - package-lock.json | 5 +++++ package.json | 1 + 10 files changed, 38 insertions(+), 8 deletions(-) rename config.example.js => config.defaults.js (79%) diff --git a/README.md b/README.md index 382bd77..c512837 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ### Installation -Copy ```config.example.js``` to ```config.js``` and review the configuration options. If this is an update, compare your ```config.js``` with ```config.example.js``` to see if anything has been added. +Copy ```config.defaults.js``` to ```config.js``` and change as required. Any settings left out will use the value from ```config.defaults.js```. To build the production version: ```bash diff --git a/config.example.js b/config.defaults.js similarity index 79% rename from config.example.js rename to config.defaults.js index 2db07a5..08e4962 100644 --- a/config.example.js +++ b/config.defaults.js @@ -2,6 +2,17 @@ const path = require('path'); const ExpirationUnits = require('./lib/expirationunits'); +/* + + Copy this file and rename it to 'config.js' to customize the configuration. + This will ensure your changes are not overwritten during updates. + + Any settings not configured in your own 'config.js' will use the default + defined in 'config.defaults.js'. + +*/ + + module.exports = { port: 3000, @@ -26,7 +37,11 @@ module.exports = { length: 8, maxExpiration: null + /* + + To set a maximum expiration of 7 days: + maxExpiration: { units: ExpirationUnits.Days, value: 7 diff --git a/index.js b/index.js index 47e220b..d8846ce 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,23 @@ 'use strict' +const fs = require('fs'); +const merge = require('deepmerge'); + + +let configDefaults = require('./config.defaults'); +if (fs.existsSync('./config.js')) +{ + let configChanges = require('./config'); + global.config = merge(configDefaults, configChanges); +} +else + global.config = configDefaults; + + + -const config = require('./config'); const Repository = require('./lib/repository'); const NotificationWorker = require('./lib/workers/notification'); -const fs = require('fs'); const express = require('express'); const bodyParser = require('body-parser'); const tus = require('tus-node-server'); @@ -18,6 +31,7 @@ const webpackDevMiddleware = require('webpack-dev-middleware'); const webpackHotMiddleware = require('webpack-hot-middleware'); const webpackConfigFactory = require('./webpack.config.js'); + (async () => { try diff --git a/lib/api/admin.js b/lib/api/admin.js index c10df86..d25c5f3 100644 --- a/lib/api/admin.js +++ b/lib/api/admin.js @@ -1,4 +1,3 @@ -const config = require('../../config'); const express = require('express'); const asyncHandler = require('express-async-handler'); const jwt = require('jsonwebtoken'); diff --git a/lib/api/token.js b/lib/api/token.js index 1d1aecd..93270f9 100644 --- a/lib/api/token.js +++ b/lib/api/token.js @@ -1,4 +1,3 @@ -const config = require('../../config'); const express = require('express'); const asyncHandler = require('express-async-handler'); const jwt = require('jsonwebtoken'); diff --git a/lib/api/upload.js b/lib/api/upload.js index 1993006..d2467d7 100644 --- a/lib/api/upload.js +++ b/lib/api/upload.js @@ -1,4 +1,3 @@ -const config = require('../../config'); const express = require('express'); const asyncHandler = require('express-async-handler'); const jwt = require('jsonwebtoken'); diff --git a/lib/repository/code.js b/lib/repository/code.js index 0f47321..a85c830 100644 --- a/lib/repository/code.js +++ b/lib/repository/code.js @@ -2,7 +2,6 @@ const map = require('lodash/map'); const retry = require('async-retry'); const generate = require('nanoid/generate'); const markdown = require('markdown').markdown; -const config = require('../../config'); const ExpirationUnits = require('../expirationunits'); diff --git a/lib/repository/upload.js b/lib/repository/upload.js index 889cafe..679e404 100644 --- a/lib/repository/upload.js +++ b/lib/repository/upload.js @@ -2,7 +2,6 @@ const map = require('lodash/map'); const filter = require('lodash/filter'); const resolvePath = require('resolve-path'); const fs = require('mz/fs'); -const config = require('../../config'); class Upload diff --git a/package-lock.json b/package-lock.json index c70434e..3557edf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3132,6 +3132,11 @@ "resolved": "https://registry.npmjs.org/deep-extend/-/deep-extend-0.4.2.tgz", "integrity": "sha1-SLaZwn4zS/ifEIkr5DL25MfTSn8=" }, + "deepmerge": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-2.1.0.tgz", + "integrity": "sha512-Q89Z26KAfA3lpPGhbF6XMfYAm3jIV3avViy6KOJ2JLzFbeWHOvPQUu5aSJIWXap3gDZC2y1eF5HXEPI2wGqgvw==" + }, "define-properties": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.1.2.tgz", diff --git a/package.json b/package.json index ebc768d..c613973 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "body-parser": "^1.18.2", "cookie-parser": "^1.4.3", "debug": "^3.1.0", + "deepmerge": "^2.1.0", "diskusage": "^0.2.4", "ejs": "^2.5.9", "email-templates": "^3.6.0",