/
configvention.js
120 lines (91 loc) · 3.59 KB
/
configvention.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*!
* @license nodejs-configvention
* Copyright (c) 2012, 2016, 2021, 2023, Joel Purra <https://joelpurra.com/>
* Released under BSD, MIT and GPL, see LICENSES.
*
* A node.js configuration convention with a minimal readonly interface.
*/
/*jslint vars: true, white: true*/
import {
statSync,
} from "node:fs";
import process from "node:process";
import debug from "debug";
import nconf from "nconf";
var exported = (function() {
var priv = {},
pub = {};
priv.nconf = new nconf.Provider();
priv.debug = debug("configvention");
priv.debug.getAppPath = debug("configvention:getAppPath");
priv.debug.initialize = debug("configvention:initialize");
priv.debug.get = debug("configvention:get");
priv.isNonEmptyString = function(value) {
return typeof value === "string" && value.length > 0;
};
priv.hasFilename = function(mod) {
// https://nodejs.org/api/modules.html#modules_module_filename
//
// The fully resolved filename to the module.
return mod && mod.filename && priv.isNonEmptyString(mod.filename);
};
priv.getAppPath = function() {
if (!(
(process && process.mainModule && priv.hasFilename(process.mainModule))
|| (process && process.argv && priv.isNonEmptyString(process.argv[1]))
)) {
priv.debug.getAppPath("Could not find the entry nor current module. Not loading configuration from files.");
return null;
}
var appPath = null;
// Return the first available filename.
if (process.mainModule && priv.hasFilename(process.mainModule)) {
appPath = process.mainModule.filename;
} else if (process && process.argv && priv.isNonEmptyString(process.argv[1])) {
appPath = process.argv[1];
}
if (appPath) {
priv.debug.getAppPath("Using app path: " + appPath);
if (appPath.indexOf("node_modules/") !== -1) {
priv.debug.getAppPath("App path seems to be pointing to inside `node_modules/`: " + appPath);
}
} else {
priv.debug.getAppPath("Could not find the filename of the module. Not loading configuration from files.");
}
return appPath;
};
priv.initialize = function() {
var appPath = priv.getAppPath();
var nconfLoadingSteps = priv.nconf.argv().env();
if (appPath) {
var overridePath = appPath + ".config.json";
var defaultsPath = appPath + ".defaults.config.json";
try {
statSync(overridePath).isFile();
priv.debug.initialize("Configuration override file found: " + overridePath);
nconfLoadingSteps = nconfLoadingSteps.file({
file: overridePath,
});
} catch(e) {
priv.debug.initialize("Configuration override file not found: " + overridePath);
}
try {
statSync(defaultsPath).isFile();
priv.debug.initialize("Configuration defaults file found: " + defaultsPath);
nconfLoadingSteps = nconfLoadingSteps.file("defaults", {
file: defaultsPath,
});
} catch(e) {
priv.debug.initialize("Configuration defaults file not found: " + defaultsPath);
}
}
};
pub.get = function(name) {
var value = priv.nconf.get(name);
priv.debug.get("" + name + '=' + value);
return value;
};
priv.initialize();
return pub;
}());
export default exported;