Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50675 views
1
/*
2
* resourceful.js: Top-level plugin exposing resourceful to flatiron app
3
*
4
* (C) 2011, Nodejitsu Inc.
5
* MIT LICENSE
6
*
7
*/
8
9
var path = require('path'),
10
fs = require('fs'),
11
flatiron = require('../../flatiron'),
12
common = flatiron.common,
13
resourceful;
14
15
try {
16
//
17
// Attempt to require resourceful.
18
//
19
resourceful = require('resourceful');
20
}
21
catch (ex) {
22
//
23
// Do nothing since this is a progressive enhancement
24
//
25
console.warn('flatiron.plugins.resourceful requires the `resourceful` module from npm');
26
console.warn('install using `npm install resourceful`.');
27
console.trace();
28
process.exit(1);
29
}
30
31
exports.name = 'resourceful';
32
33
exports.attach = function (options) {
34
var app = this;
35
36
options = options || {};
37
38
//
39
// Accept string `options`.
40
//
41
if (typeof options === 'string') {
42
options = { root: options };
43
}
44
45
//
46
// Create `app.resources` if it does not exist already.
47
//
48
app.resources = app.resources || {};
49
50
//
51
// Lazy-load the resources directory based on a few intelligent defaults:
52
//
53
// * `options.dir`: Explicit path to resources directory
54
// * `options.root`: Relative root to the resources directory ('/app/resources')
55
// * `app.root`: Relative root to the resources directory ('/app/resources')
56
//
57
if (options.dir || options.root || app.root) {
58
app._resourceDir = options.dir
59
|| path.join(options.root || app.root, 'app', 'resources');
60
61
common.tryReaddirSync(app._resourceDir).forEach(function (file) {
62
file = file.replace('.js', '');
63
64
app.resources.__defineGetter__(common.capitalize(file), function () {
65
delete app.resources[file];
66
return app.resources[file] = require(
67
path.resolve(app._resourceDir, file)
68
);
69
});
70
});
71
}
72
73
//
74
// Expose a couple of resourceful helpers
75
//
76
app.define = resourceful.define;
77
78
//
79
// TODO: Determine how best to integrate `restful` here.
80
//
81
};
82
83
exports.init = function (done) {
84
var app = this,
85
options;
86
87
//
88
// Attempt to merge defaults passed to `app.use(flatiron.plugins.resourceful)`
89
// with any additional configuration that may have been loaded.
90
//
91
options = common.mixin(
92
{},
93
app.options['resourceful'],
94
app.config.get('resourceful') || {}
95
);
96
97
app.config.set('resourceful', options);
98
99
//
100
// Remark: Should we accept the autoMigrate option?
101
//
102
if (options.engine) {
103
resourceful.use(options.engine, options);
104
}
105
106
done();
107
};
108