From aea974387d70736008396a759ff71b1d4310c865 Mon Sep 17 00:00:00 2001 From: Martijn de Boer Date: Fri, 5 Jan 2018 15:26:03 +0100 Subject: [PATCH] Config lookup --- pixdisp.js | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/pixdisp.js b/pixdisp.js index aa24a7b..7f7722e 100644 --- a/pixdisp.js +++ b/pixdisp.js @@ -1,17 +1,30 @@ 'use strict'; let fs = require( 'fs' ); -let config; -let api; +let os = require( 'os' ); + +let config, + api, + contents; if ( fs.existsSync( 'config.json' ) ) { - let contents = fs.readFileSync( 'config.json' ); - config = JSON.parse( contents ); + contents = fs.readFileSync( 'config.json' ); +} else if ( fs.existsSync( os.homedir() + '/.pixdisp/config.json' ) ) { + contents = fs.readFileSync( os.homedir() + '/.pixdisp/config.json' ); } else { - let contents = fs.readFileSync( 'config.example.json' ); - config = JSON.parse( contents ); + contents = fs.readFileSync( 'config.example.json' ); } +if ( typeof contents === 'undefined' || + contents === false || contents === undefined || + contents === '' ) { + + console.log( 'Invalid or no configuration' ); + process.exit(); +} + +config = JSON.parse( contents ); + let driver; let { DriverFactory } = require( './drivers/driverfactory' ); let driverFactory = new DriverFactory();