diff --git a/lib/gui/app/models/local-settings.js b/lib/gui/app/models/local-settings.js index c914637e..87ee956f 100644 --- a/lib/gui/app/models/local-settings.js +++ b/lib/gui/app/models/local-settings.js @@ -17,6 +17,10 @@ 'use strict' const Bluebird = require('bluebird') +const _ = require('lodash') +const fs = require('fs') +const path = require('path') +const os = require('os') /** * @summary Local storage settings key @@ -25,6 +29,43 @@ const Bluebird = require('bluebird') */ const LOCAL_STORAGE_SETTINGS_KEY = 'etcher-settings' +/** + * @summary Local settings filename + * @constant + * @type {String} + */ +const RCFILE = '.etcher.json' + +/** + * @summary Read a local .etcherrc file + * @function + * @public + * + * @param {String} filename - file path + * @fulfil {Object} - settings + * @returns {Promise} + * + * @example + * readConfigFile('.etcherrc').then((settings) => { + * console.log(settings) + * }) + */ +const readConfigFile = (filename) => { + return new Bluebird((resolve, reject) => { + fs.readFile(filename, (error, buffer) => { + if (error) { + if (error.code === 'ENOENT') { + resolve({}) + } else { + reject(error) + } + } else { + resolve(JSON.parse(buffer.toString())) + } + }) + }) +} + /** * @summary Read all local settings * @function @@ -39,9 +80,22 @@ const LOCAL_STORAGE_SETTINGS_KEY = 'etcher-settings' * }); */ exports.readAll = () => { + const homeConfigPath = process.platform === 'win32' + ? path.join(os.userInfo().homedir, RCFILE) + : path.join(os.userInfo().homedir, '.config', 'etcher', 'config.json') + const workdirConfigPath = path.join(process.cwd(), RCFILE) + const settings = {} return Bluebird.try(() => { - return JSON.parse(window.localStorage.getItem(LOCAL_STORAGE_SETTINGS_KEY)) || {} - }) + _.merge(settings, JSON.parse(window.localStorage.getItem(LOCAL_STORAGE_SETTINGS_KEY))) + }).return(readConfigFile(homeConfigPath)) + .then((homeConfig) => { + _.merge(settings, homeConfig) + }) + .return(readConfigFile(workdirConfigPath)) + .then((workdirConfig) => { + _.merge(settings, workdirConfig) + }) + .return(settings) } /**