-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathindex.js
227 lines (183 loc) · 7.25 KB
/
index.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
var path = require('path');
var _ = require('@sailshq/lodash');
// using private sails methods. Could be broken in next sails release!
var loadHelpers = require('sails/lib/hooks/helpers/private/load-helpers');
var iterateHelpers = require('sails/lib/hooks/helpers/private/iterate-helpers');
module.exports = function(sails) {
var logMode;
function log(data) {
if (logMode) {
if (logMode === true) {
sails.log(data);
} else if (sails.log[logMode]) {
sails.log[logMode](data);
}
}
}
function reloadConfigByFilepath(configPath, isConfig) {
try {
// Remove the routes config file from the require cache.
delete require.cache[require.resolve(configPath)];
if (isConfig !== false) {
Object.assign(sails.config, require(configPath));
}
log('sails-hook-autoreload: Reloaded `' + configPath + '`.');
} catch (e) {
log('sails-hook-autoreload: Could not reload `' + configPath + '`.');
}
}
function removeHelperByFilepath(fileName) {
iterateHelpers(sails.helpers, undefined, undefined, function(callable, key) {
var helperId = callable.toJSON()._loadedFrom;
var _helperIdParts = helperId.split('/');
_helperIdParts.pop();
var helperFile = path.resolve(sails.config.paths.helpers, helperId + '.js');
var store = _helperIdParts.length ? _.get(sails.helpers, _helperIdParts.join('.')) : sails.helpers;
if (fileName === helperFile) {
delete store[key];
}
})
}
function reloadRemovedHelpers(cb) {
loadHelpers(sails, cb);
}
function processChanges(changes) {
_.uniq(changes, changedItemUnique_cb).forEach(processChangedItem);
changes.length = 0;
}
function processChangedItem(changedItem) {
var changedPath = path.resolve(sails.config.appPath, changedItem[1]);
var action = changedItem[0];
if (changedPath.indexOf(sails.config.paths.config) === 0) {
if (action === 'change') {
reloadConfigByFilepath(changedPath)
}
} else if (changedPath.indexOf(sails.config.paths.helpers) === 0) {
if (action === 'change' || action === 'unlink') {
removeHelperByFilepath(changedPath);
}
} else if (action === 'change' || action === 'unlink') {
reloadConfigByFilepath(changedPath, false);
}
}
function changedItemUnique_cb(item) {
return item.join()
}
return {
/**
* Default configuration
*
* We do this in a function since the configuration key for
* the hook is itself configurable, so we can't just return
* an object.
*/
defaults: {
__configKey__: {
//use polling to watch file changes
//slower but sometimes needed for VM environments
usePolling: false,
// Set dirs to watch
dirs: [
sails.config.paths.controllers,
sails.config.paths.models,
sails.config.paths.services,
sails.config.paths.helpers,
sails.config.paths.hooks,
sails.config.paths.views,
sails.config.paths.adapters,
sails.config.paths.config,
],
overrideMigrateSetting: true,
// Ignored paths, passed to anymatch
// String to be directly matched, string with glob patterns,
// regular expression test, function
// or an array of any number and mix of these types
ignored: [],
log: 'verbose'
}
},
configure: function() {
logMode = sails.config[this.configKey].log;
sails.config[this.configKey].active =
// If an explicit value for the "active" config option is set, use it
(typeof sails.config[this.configKey].active !== 'undefined') ?
// Otherwise turn off in production environment, on for all others
sails.config[this.configKey].active :
(sails.config.environment != 'production');
},
/**
* Initialize the hook
* @param {Function} cb Callback for when we're done initializing
*/
initialize: function(cb) {
var self = this;
var routesConfigPath = path.resolve(sails.config.paths.config, 'routes.js');
// If the hook has been deactivated, or controllers is deactivated just return
if (!sails.config[this.configKey].active) {
log("sails-hook-autoreload: Autoreload hook deactivated.");
return cb();
}
// Initialize the file watcher to watch controller and model dirs
var chokidar = require('chokidar');
// Watch both the controllers and models directories
var watcher = chokidar.watch(sails.config[this.configKey].dirs, {
// Ignore the initial "add" events which are generated when Chokidar
// starts watching files
ignoreInitial: true,
usePolling: sails.config[this.configKey].usePolling,
ignored: sails.config[this.configKey].ignored
});
log("sails-hook-autoreload: Autoreload watching: " + JSON.stringify(sails.config[this.configKey].dirs, 0, 2));
// Whenever something changes in those dirs, reload the ORM, controllers and blueprints.
// Debounce the event handler so that it only fires after receiving all of the change
// events.
var changes = [];
watcher.on('all', function(action, watchedPath) {
changes.push([action, watchedPath]);
});
watcher.on('all', _.debounce(function(action, watchedPath, stats) {
log("sails-hook-autoreload: Detected API changes: " + JSON.stringify(changes, 0, 2));
processChanges(changes);
// Don't use the configured migration strategy if `overrideMigrateSetting` is true.
if(sails.config.models) {
sails.config.models.migrate = sails.config[self.configKey].overrideMigrateSetting ? 'alter' : sails.config.models.migrate;
}
// Reload controller middleware
sails.reloadActions(function() {
// Reload helpers
reloadRemovedHelpers(function() {
function reloadEveryElseThanOrm () {
// Reload services
if (sails.hooks.services) {
sails.hooks.services.loadModules(function() {});
}
// Unset all of the current routes from the `explicitRoutes` hash.
// This hash may include some routes added by hooks, so can't just wipe
// it entirely, but in case some route URLs changed we don't want
// the old ones hanging around.
sails.router.explicitRoutes = _.omit(sails.router.explicitRoutes, function(action, address) {
return !!sails.config.routes[address];
});
// Reload the config/routes.js file.
reloadConfigByFilepath(routesConfigPath);
// Flush the router.
sails.router.explicitRoutes = _.extend({}, sails.router.explicitRoutes, sails.config.routes);
sails.router.flush(sails.config.routes);
}
if(sails.hooks.orm) {
sails.hooks.orm.reload(function() {
// Wait for the ORM to reload
reloadEveryElseThanOrm();
});
}
else {
reloadEveryElseThanOrm();
}
});
});
}, 100));
// We're done initializing.
return cb();
},
};
};