From 03123fb783701c44bc8258ff38c5f24189abda35 Mon Sep 17 00:00:00 2001 From: Daniel Welch Date: Fri, 11 May 2018 23:04:15 -0400 Subject: [PATCH 1/3] configure datapath with env variable --- index.js | 8 +++++++- lib/util/data.js | 14 ++++++++++++++ lib/util/logger.js | 3 ++- lib/util/settings.js | 3 ++- lib/zigbee.js | 3 ++- 5 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 lib/util/data.js diff --git a/index.js b/index.js index 6accf7ba..aed34b90 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,10 @@ +const fs = require('fs'); const Controller = require('./lib/controller'); +const data = require('./lib/util/data'); + +if (process.env.ZIGBEE2MQTT_DATA) { + fs.copyFileSync('./data/configuration.yaml', data.joinPath('configuration.yaml')) +} const controller = new Controller(); controller.start(); @@ -7,4 +13,4 @@ process.on('SIGINT', handleQuit); function handleQuit() { controller.stop(() => process.exit()); -} \ No newline at end of file +} diff --git a/lib/util/data.js b/lib/util/data.js new file mode 100644 index 00000000..89c87d6f --- /dev/null +++ b/lib/util/data.js @@ -0,0 +1,14 @@ +const path = require('path'); + +function dataPath() { + if (process.env.ZIGBEE2MQTT_DATA) { + return process.env.ZIGBEE2MQTT_DATA; + } else { + return `${__dirname}/../../data`; + } +} + +module.exports = { + path: dataPath(), + joinPath: (file) => path.join(dataPath(), file) +} diff --git a/lib/util/logger.js b/lib/util/logger.js index 4cb094f0..c1935fa0 100644 --- a/lib/util/logger.js +++ b/lib/util/logger.js @@ -1,9 +1,10 @@ const winston = require('winston'); +const data = require('./data'); const logger = new (winston.Logger)({ transports: [ new (winston.transports.File)({ - filename: 'data/log.txt', + filename: data.joinPath('log.txt'), json: false, level: winston.level.info, maxFiles: 3, diff --git a/lib/util/settings.js b/lib/util/settings.js index 2934a425..01392b88 100644 --- a/lib/util/settings.js +++ b/lib/util/settings.js @@ -1,6 +1,7 @@ const YAWN = require('yawn-yaml/cjs'); const fs = require('fs'); -const file = `${__dirname}/../../data/configuration.yaml`; +const data = require('./data'); +let file = data.joinPath('configuration.yaml'); let yawn = read(); function write() { diff --git a/lib/zigbee.js b/lib/zigbee.js index 530bc7ce..011308a7 100644 --- a/lib/zigbee.js +++ b/lib/zigbee.js @@ -2,10 +2,11 @@ const ZShepherd = require('zigbee-shepherd'); const logger = require('./util/logger'); const settings = require('./util/settings'); const deviceMapping = require('./devices'); +const data = require('./util/data'); const shepherdSettings = { net: {panId: 0x1a62}, - dbPath: `${__dirname}/../data/database.db` + dbPath: data.joinPath('database.db') }; class Zigbee { From d0306aedea490c85639bfecd59c8513c0e0ec49e Mon Sep 17 00:00:00 2001 From: Daniel Welch Date: Fri, 11 May 2018 23:16:37 -0400 Subject: [PATCH 2/3] missing semicolon --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index aed34b90..29ae02b7 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,7 @@ const Controller = require('./lib/controller'); const data = require('./lib/util/data'); if (process.env.ZIGBEE2MQTT_DATA) { - fs.copyFileSync('./data/configuration.yaml', data.joinPath('configuration.yaml')) + fs.copyFileSync('./data/configuration.yaml', data.joinPath('configuration.yaml')); } const controller = new Controller(); From 44002ed1c3d9611aa71a4c47b7dd92275954cdf6 Mon Sep 17 00:00:00 2001 From: Koenkk Date: Sat, 12 May 2018 10:58:06 +0200 Subject: [PATCH 3/3] Update configure data path code --- docker/run.sh | 12 ++++++++++-- index.js | 8 +------- lib/util/data.js | 17 +++++++++-------- lib/util/settings.js | 3 ++- 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/docker/run.sh b/docker/run.sh index 659a3e69..1bedb05f 100644 --- a/docker/run.sh +++ b/docker/run.sh @@ -1,8 +1,16 @@ #!/bin/sh -if [ ! -f /app/data/configuration.yaml ]; then +if [ ! -z "$ZIGBEE2MQTT_DATA" ]; then + DATA="$ZIGBEE2MQTT_DATA" +else + DATA="/app/data" +fi + +echo "Using '$DATA' as data directory" + +if [ ! -f "$DATA/configuration.yaml" ]; then echo "Creating configuration file..." - cp /app/configuration.yaml /app/data/configuration.yaml + cp /app/configuration.yaml "$DATA/configuration.yaml" fi npm start diff --git a/index.js b/index.js index 29ae02b7..6accf7ba 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,4 @@ -const fs = require('fs'); const Controller = require('./lib/controller'); -const data = require('./lib/util/data'); - -if (process.env.ZIGBEE2MQTT_DATA) { - fs.copyFileSync('./data/configuration.yaml', data.joinPath('configuration.yaml')); -} const controller = new Controller(); controller.start(); @@ -13,4 +7,4 @@ process.on('SIGINT', handleQuit); function handleQuit() { controller.stop(() => process.exit()); -} +} \ No newline at end of file diff --git a/lib/util/data.js b/lib/util/data.js index 89c87d6f..b50cadf4 100644 --- a/lib/util/data.js +++ b/lib/util/data.js @@ -1,14 +1,15 @@ const path = require('path'); -function dataPath() { - if (process.env.ZIGBEE2MQTT_DATA) { - return process.env.ZIGBEE2MQTT_DATA; - } else { - return `${__dirname}/../../data`; - } +let dataPath = null; + +if (process.env.ZIGBEE2MQTT_DATA) { + dataPath = process.env.ZIGBEE2MQTT_DATA; +} +else { + dataPath = path.join(__dirname, '..', '..', 'data'); + dataPath = path.normalize(dataPath); } module.exports = { - path: dataPath(), - joinPath: (file) => path.join(dataPath(), file) + joinPath: (file) => path.join(dataPath, file) } diff --git a/lib/util/settings.js b/lib/util/settings.js index 01392b88..d7e1fe30 100644 --- a/lib/util/settings.js +++ b/lib/util/settings.js @@ -1,7 +1,8 @@ const YAWN = require('yawn-yaml/cjs'); const fs = require('fs'); const data = require('./data'); -let file = data.joinPath('configuration.yaml'); +const file = data.joinPath('configuration.yaml'); + let yawn = read(); function write() {