UNPKG

4.93 kBJavaScriptView Raw
1
2'use strict';
3
4module.exports = function (env, config, args) {
5 config = config || {};
6
7 env = env || process.env.NODE_ENV || 'development';
8
9 var hostname = require('os').hostname();
10 var path = require('path');
11 var yargs = require('yargs');
12 var extend = require('extend');
13 var fs = require('fs');
14 var shunterRoot = path.dirname(__dirname);
15
16 args = args || yargs
17 .options('c', {
18 alias: 'max-child-processes',
19 default: 10,
20 type: 'number'
21 })
22 .options('d', {
23 alias: 'source-directory',
24 default: process.cwd(),
25 type: 'string'
26 })
27 .options('g', {
28 alias: 'origin-override',
29 type: 'boolean'
30 })
31 .options('l', {
32 alias: 'logging',
33 default: 'info',
34 type: 'string'
35 })
36 .options('m', {
37 alias: 'max-post-size',
38 default: 204800,
39 type: 'number'
40 })
41 .options('o', {
42 alias: 'route-override',
43 type: 'string'
44 })
45 .options('p', {
46 alias: 'port',
47 default: 5400,
48 type: 'number'
49 })
50 .options('r', {
51 alias: 'route-config',
52 default: 'default'
53 })
54 .options('s', {
55 alias: 'syslog',
56 type: 'boolean'
57 })
58 .options('w', {
59 alias: 'preserve-whitespace',
60 type: 'boolean'
61 })
62 .options('compile-on-demand', {
63 type: 'boolean'
64 })
65 .options('rewrite-protocol', {
66 type: 'string'
67 })
68 .options('rewrite-redirect', {
69 type: 'boolean'
70 })
71 .describe({
72 c: 'Shunter will create one worker process per cpu available up to this maximum',
73 d: 'Specify the directory for the main app if you are not running it from its own directory',
74 g: 'Requires --route-override. Sets changeOrigin to true for the route set up via --route-override',
75 l: 'Set logging level',
76 m: 'Maximum size for request body in bytes',
77 o: 'Specify host and port to override or replace route config file',
78 p: 'Port number',
79 r: 'Specify the name of the default route from your route config file',
80 s: 'Enable logging to syslog',
81 w: 'Preserves whitespace in HTML output',
82 'compile-on-demand': 'Compile templates on demand instead of at application start up, only recommended in development mode',
83 'rewrite-protocol': 'Rewrite the location protocol on 301, 302, 307 & 308 redirects to http or https',
84 'rewrite-redirect': 'Rewrite the location host/port on 301, 302, 307 & 308 redirects based on requested host/port'
85 })
86 .alias('h', 'help')
87 .help()
88 .alias('v', 'version')
89 .version(function () {
90 return require('../package').version;
91 })
92 .check(function (argv, args) {
93 var exclude = ['_', '$0'];
94
95 Object.keys(argv).forEach(function (key) {
96 if (exclude.indexOf(key) === -1 && !args.hasOwnProperty(key)) {
97 throw new Error('Unknown argument error: `' + key + '` is not a valid argument');
98 }
99 });
100 Object.keys(args).forEach(function (key) {
101 if (Array.isArray(argv[key])) {
102 throw new TypeError('Invalid argument error: `' + key + '` must only be specified once');
103 }
104 });
105 return true;
106 })
107 .argv;
108
109 var appRoot = args['source-directory'] || process.cwd();
110
111 var defaultConfig = {
112 argv: args,
113 env: {
114 host: function () {
115 return hostname;
116 },
117 isDevelopment: function () {
118 return this.name === 'development';
119 },
120 isProduction: function () {
121 return this.name === 'production';
122 },
123 name: env
124 },
125 jsonViewParameter: null,
126 log: null,
127 middleware: [],
128 modules: [],
129 path: {
130 clientTests: path.join(appRoot, 'tests', 'client'),
131 dust: path.join(appRoot, 'dust'),
132 public: path.join(appRoot, 'public'),
133 publicResources: path.join(appRoot, 'public', 'resources'),
134 resources: path.join(appRoot, 'resources'),
135 root: appRoot,
136 shunterResources: path.join(shunterRoot, 'resources'),
137 shunterRoot: shunterRoot,
138 templates: path.join(appRoot, 'view'),
139 tests: path.join(appRoot, 'tests'),
140 themes: path.join(shunterRoot, 'themes')
141 },
142 statsd: {
143 host: 'localhost',
144 mock: env === 'development',
145 prefix: 'shunter.'
146 },
147 structure: {
148 dust: 'dust',
149 ejs: 'ejs',
150 filters: 'filters',
151 filtersInput: 'input',
152 filtersOutput: 'output',
153 fonts: 'fonts',
154 images: 'img',
155 logging: 'logging',
156 loggingFilters: 'filters',
157 loggingTransports: 'transports',
158 mincer: 'mincer',
159 resources: 'resources',
160 scripts: 'js',
161 styles: 'css',
162 templateExt: '.dust',
163 templates: 'view',
164 tests: 'tests'
165 },
166 timer: function () {
167 var start = Date.now();
168 return function (msg) {
169 var diff = Date.now() - start;
170 config.log.debug(msg + ' - ' + diff + 'ms');
171 return diff;
172 };
173 },
174 web: {
175 public: '/public',
176 publicResources: '/public/resources',
177 resources: '/resources',
178 tests: '/tests'
179 }
180 };
181
182 config = extend(true, {}, defaultConfig, config);
183 var localConfig = path.join(appRoot, 'config', 'local.json');
184 if (fs.existsSync(localConfig)) {
185 extend(true, config, require(localConfig));
186 }
187
188 if (!config.log) {
189 config.log = require('./logging')(config).getLogger();
190 }
191
192 return config;
193};