UNPKG

3.92 kBJavaScriptView Raw
1/*
2 * Copyright (c) 2011-2013, Yahoo! Inc. All rights reserved.
3 * Copyrights licensed under the New BSD License.
4 * See the accompanying LICENSE file for terms.
5 */
6
7/*jslint anon:true, sloppy:true, nomen:true*/
8
9
10// ----------------------------------------------------------------------------
11// Prerequisites
12// ----------------------------------------------------------------------------
13
14var libpath = require('path'),
15 YUIFactory = require('./yui-sandbox.js');
16
17// ----------------------------------------------------------------------------
18// Store
19// ----------------------------------------------------------------------------
20
21/**
22 * A factory object providing methods for creating and managing ResourceStore
23 * instances.
24 */
25var Store = {};
26
27
28/**
29 * Creates a new server-side resource store instance and returns it.
30 * @method createStore
31 * @param {object} options An object containing store options.
32 * @param {string} options.string Path to application directory.
33 * @param {object} options.context Runtime context to apply to all requests.
34 * @param {object} options.appConfig Overrides for the application.json file.
35 * @param {string "skip"|"initial"|"full"} options.preload Whether to preload
36 * the application. Defaults to "full". If you only care about appConfig
37 * and package.json you can use the 'skip' option which is faster.
38 * @param {object} options.Y The runtime Y instance used by the mojito server.
39 * This will be exposed to store addons as `store.runtimeYUI`.
40 * @return Y.mojito.ResourceStore
41 */
42Store.createStore = function(options) {
43
44 var store,
45 YUI,
46 appConfig,
47 Y;
48
49 if (!options) {
50 options = {};
51 }
52 if (!options.root) {
53 options.root = process.cwd();
54 }
55 if (!options.context) {
56 options.context = {};
57 }
58 if (!options.preload) {
59 options.preload = 'full';
60 }
61 if (!options.context.runtime) {
62 options.context.runtime = 'server';
63 }
64
65 // Create a sandboxed YUI instance. This is necessary to avoid shared
66 // metadata with the main Mojito execution context and the execution context
67 // of the ResourceStore instance.
68 YUI = YUIFactory.getYUI();
69
70 // Configure the prerequisites and load the resource store impl code.
71 Y = YUI({
72 useSync: true,
73 modules: {
74 'mojito': {
75 fullpath: libpath.join(__dirname, 'app/autoload/mojito.common.js')
76 },
77 'mojito-util': {
78 fullpath: libpath.join(__dirname, 'app/autoload/util.common.js')
79 },
80 'mojito-resource-store': {
81 fullpath: libpath.join(__dirname, 'app/autoload/store.server.js')
82 }
83 }
84 });
85 Y.use('mojito-resource-store');
86
87 store = new Y.mojito.ResourceStore({
88 root: options.root,
89 context: options.context,
90 appConfig: options.appConfig
91 });
92
93 appConfig = store.getStaticAppConfig();
94 Y.applyConfig((appConfig && appConfig.yui && appConfig.yui.config) || {});
95
96 store.runtimeYUI = options.Y;
97
98 if ('initial' === options.preload) {
99 store.preloadInitial();
100 } else if ('skip' !== options.preload) {
101 store.preload();
102 }
103
104 return store;
105};
106
107
108/**
109 * Returns the application config.
110 * @method getAppConfig
111 * @param {string} dir The directory containing the application config.
112 * @param {Object} context The runtime context.
113 * @return {Object} The application config.
114 */
115Store.getAppConfig = function(dir, context) {
116 var store;
117 store = this.createStore({
118 root: dir,
119 context: context,
120 preload: 'skip'
121 });
122 return store.getStaticAppConfig();
123};
124
125
126// ----------------------------------------------------------------------------
127// EXPORT(S)
128// ----------------------------------------------------------------------------
129
130module.exports = Store;
131