blob: 3cd5244dabf925f662562ad1106d89e91dbc9cc3 [file] [log] [blame]
Timoney, Daniel (dt5972)324ee362017-02-15 10:37:53 -05001/**
2 * Copyright 2014 IBM Corp.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 **/
16
17var path = require("path");
18var fs = require("fs");
19
20var userHome = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
21
22var configDir = path.join(userHome,".nodered");
23var configFile = path.join(configDir,"config.json");
24
25var config;
26
27function load() {
28 if (config == null) {
29 try {
30 config = JSON.parse(fs.readFileSync(configFile));
31 } catch(err) {
32 config = {};
33 }
34 }
35}
36
37function save() {
38 try {
39 fs.mkdirSync(configDir);
40 } catch(err) {
41 if (err.code != "EEXIST") {
42 throw err;
43 }
44 }
45 fs.writeFileSync(configFile,JSON.stringify(config,null,4));
46}
47module.exports = {
48 unload: function() {
49 config = null;
50 }
51};
52module.exports.__defineGetter__('target',function() { load(); return config.target|| "http://localhost:1880" });
53module.exports.__defineSetter__('target',function(v) { load(); config.target = v; save();});