forked from anthonywebb/sprinkler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.js
83 lines (74 loc) · 1.94 KB
/
path.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyrigth (C) Pascal Martin, 2014.
//
// NAME
//
// path - a central point where to decide the location of files.
//
// SYNOPSYS
//
// This module provides a way to organize the configuration files.
//
// The module searchs in the following locations:
// 1- local directory.
// 2- /var/lib/sprinkler
//
// If the file does not exists, but /var/lib/sprinkler exists, then
// /var/lib/sprinkler is used. If /var/lib/sprinkler does not exist,
// the local directory is used.
//
// A separate function is used for each file needed by the application.
// This allows changing the search strategy for each file in the future.
//
//
// DESCRIPTION
//
// var path = require('./path');
//
// path.configure(options);
//
// Take the command line options into consideration.
//
// path.userConfig();
//
// Return a full path name for the user configuration file.
//
// path.hardwareConfig();
//
// Return a full path name for the hardware configuration file.
//
// path.events();
//
// Return a full path name for the event database file.
//
var fs = require('graceful-fs');
var debugLog = function (text) {}
function verboseLog (text) {
console.log ('[DEBUG] Path: '+text);
}
function searchFile (name) {
debugLog ('searching for '+name+' ..');
if (fs.existsSync('./'+name)) {
debugLog ('found local file ./'+name);
return './'+name;
}
if (fs.existsSync('/var/lib/sprinkler')) {
debugLog ('using file /var/lib/sprinkler/'+name);
return '/var/lib/sprinkler/'+name;
}
debugLog ('defaulting to local file ./'+name);
return './'+name;
}
exports.configure = function (options) {
if (options && options.debug) {
debugLog = verboseLog;
}
}
exports.userConfig = function () {
return searchFile ('config.json');
}
exports.hardwareConfig = function () {
return searchFile ('hardware.json');
}
exports.events = function () {
return searchFile ('database');
}