From ee374647416d2eb86e792163a384ce9d588b6e61 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Sat, 26 Sep 2015 22:48:34 +0100 Subject: [PATCH] Copy settings file to userDir on start-up if it makes sense It will only copy the file if: - a custom file hasn't been specified (-s file) - there isn't an existing settings.js file in userDir - the default settings file (in install dir) hasn't been modified (mtime < ctime) --- red.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/red.js b/red.js index d4bd2d44d..3bdff9ce9 100755 --- a/red.js +++ b/red.js @@ -21,7 +21,7 @@ var express = require("express"); var crypto = require("crypto"); var nopt = require("nopt"); var path = require("path"); -var fs = require("fs"); +var fs = require("fs-extra"); var RED = require("./red/red.js"); var log = require("./red/log"); @@ -76,13 +76,22 @@ if (parsedArgs.settings) { // NODE_RED_HOME contains user data - use its settings.js settingsFile = path.join(process.env.NODE_RED_HOME,"settings.js"); } else { - var userSettingsFile = path.join(process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE,".node-red","settings.js"); + var userDir = path.join(process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE,".node-red"); + var userSettingsFile = path.join(userDir,"settings.js"); if (fs.existsSync(userSettingsFile)) { // $HOME/.node-red/settings.js exists settingsFile = userSettingsFile; } else { - // Use default settings.js - settingsFile = __dirname+"/settings.js"; + var defaultSettings = path.join(__dirname,"settings.js"); + var settingsStat = fs.statSync(defaultSettings); + if (settingsStat.mtime.getTime() < settingsStat.ctime.getTime()) { + // Default settings file has not been modified - safe to copy + fs.copySync(defaultSettings,userSettingsFile); + settingsFile = userSettingsFile; + } else { + // Use default settings.js as it has been modified + settingsFile = defaultSettings; + } } } }