This repository has been archived by the owner on May 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.js
140 lines (123 loc) · 4.49 KB
/
settings.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
'use strict';
var //---------------
// Imports
//---------------
path = require('path'),
q = require('rw-mercenary/promises').q,
lang = require('rw-mercenary/lang'),
partial = lang.partial,
destructured = lang.destructured,
has = lang.has,
each = lang.each,
extend = lang.extend,
items = lang.items,
get = lang.get,
isUndefined = lang.is.undef,
//----------------------
// Implementation
//----------------------
// A map from settings module file paths to formidable instances.
cache = {},
// The default formidable settings module file path.
defaultPath = process.env.FORMIDABLE_SETTINGS_MODULE,
// Reset the require() cache for formidable and for any paths resolved with the path library.
reset = function() {
var cached = cache[defaultPath] && cache[defaultPath].path;
if (cached) {
cached.js.clear();
cached.template.clear();
}
each([
'api',
'context',
'log',
'formidable',
'option',
'path',
'template',
'urls'
], function(name) {
delete require.cache[path.join(__dirname, name + '.js')];
});
},
// Configure the default settings module file path.
configure = function(settingsPath) {
reset();
defaultPath = path.resolve(settingsPath);
return {
load: partial(load, defaultPath)
};
},
// Load the formidable instance identified by the settings module file path.
// Optionally accepts a settings object as the second parameter, which
// will be used instead of an on-disk module to instantiate the formidable
// instance. For the latter case, the settings path serves as a key for the
// formidable instance in subsequent calls to load().
load = function(settingsPath, settings) {
var normalPath = (
(settingsPath || defaultPath) &&
path.resolve(settingsPath || defaultPath));
if (isUndefined(normalPath)) {
throw new Error(
'Either the FORMIDABLE_SETTINGS_MODULE environment variable ' +
'must be set to the settings module file path, or a path ' +
'must be passed to formidable/settings.load()');
}
if (has(cache, normalPath) && settings) {
throw new Error(
'A formidable instance for the settings module file path "' +
normalPath +
'" already exists, so a new instance for the given settings ' +
'cannot be created.');
}
return (
cache[normalPath] ||
instantiate(
normalPath,
settings || require(normalPath)));
},
// Instantiate and cache a formidable instance from the given options.
instantiate = function(id, options) {
var option = partial(get, options),
log = require('./lib/log')(option),
path = require('./lib/path')(option),
context = require('./lib/context')(option),
api = require('./lib/api')(option),
middleware = require('./lib/middleware')(option),
urls = require('./lib/urls')(option, path),
template = require('./lib/templating/' + (option('templating') || 'swig'))(option, path, urls),
plugins = option('plugins') || [],
build = (
extend(
require('./lib/build')(option, log, path, context, middleware, urls, template),
{
log: log,
path: path,
context: context,
api: api,
middleware: middleware,
urls: urls,
template: template,
option: option
}));
// Debug?
if (option('debug')) {
q.longStackSupport = true;
}
// Cache the build API so that it's immediately available for import
// with require('formidable').
cache[id] = build;
// Load any plugins.
each(items(plugins), destructured(function(name, options) {
path.js.require(name)(build, partial(get, options));
}));
return build;
};
//------------------
// Public API
//------------------
module.exports = {
reset: reset,
configure: configure,
load: load
};