UNPKG

5.35 kBJavaScriptView Raw
1var fs = require("fs");
2var path = require("path");
3var _ = require("underscore");
4
5var cluster = require("cluster");
6
7var helpers = require("./lib/helpers");
8
9
10if(cluster.isWorker && process.env.INFRASTRUCTURE_AUTOLOAD){
11 var now = Date.now();
12 require("./init.js")({
13 config: JSON.parse(process.env.INFRASTRUCTURE_CONFIG),
14 helpers: helpers
15 }, function(err, env){
16 if(err) throw err;
17 env.i.do("log.sys", "worker started", (Date.now() - now) +"ms, structures: "+Object.keys(env.config.structures).join(","));
18 if(env.config.options.repl && typeof env.config.options.repl === "string" && env.config.structures.hasOwnProperty(env.config.options.repl)){
19 var repl = require("repl");
20 var replServer = repl.start({ prompt: "infrastructure."+env.config.options.repl+" > " });
21 replServer.context.env = env;
22 replServer.context.config = env.config;
23 }
24 });
25}
26else{
27
28 var argv = require('minimist')(process.argv.slice(2), {boolean: true});
29 var cli_config = argv.config;
30 var cli_options = _.omit(argv, ["config"]);
31 module.exports = function findApp( config, cb ){
32 var now = Date.now();
33 if( cluster.isWorker ) return module.exports.init({
34 config: JSON.parse(process.env.INFRASTRUCTURE_CONFIG),
35 helpers: helpers
36 }, cb);
37 else{
38 cluster.setupMaster({exec: __filename});
39 }
40
41 if(cli_options.mode) config.mode = cli_options.mode;
42 if(cli_options.process_mode) config.mode = cli_options.process_mode;
43
44 // Keep original config untouched
45 config = JSON.parse(JSON.stringify(config));
46 //if( !config.mode ) config.mode = "development"; // development is default mode
47 if( !config.process_mode ) config.process_mode = "single"; // single is default process_mode
48 if( !config.rootDir ) config.rootDir = process.cwd(); // process.cwd() is default rootDir
49 loadApp( extendConfig( config ), function(err, env){
50 if(err) return cb(err);
51 env.i.do("log.sys", "application started", (Date.now() - now) +"ms, process_mode: "+env.config.process_mode +", application mode: "+env.config.mode);
52 if(cli_options.repl && cli_options.repl === true){
53 var repl = require("repl");
54 var replServer = repl.start({ prompt: "infrastructure > " });
55 replServer.context.env = env;
56 replServer.context.config = env.config;
57 }
58 cb(null, env);
59 });
60 };
61
62 var init = module.exports.init = require("./init");
63
64 var loadApp = module.exports.loadApp = function( config, cb ){
65 // Creating the env object
66 module.exports.init({ config: config, helpers: helpers }, cb);
67 };
68
69 var hasConfig = module.exports.hasConfig = function( rootDir ){
70 return fs.existsSync(path.join(rootDir, "config" ))
71 || fs.existsSync(path.join(rootDir, "config.js" ))
72 || fs.existsSync(path.join(rootDir, "config.json" ))
73 || fs.existsSync(path.join(rootDir, "config.yml" ));
74 };
75
76 var loadConfig = module.exports.loadConfig = function(configPath, app_config){
77 var config;
78 if(fs.statSync(configPath).isDirectory()){
79
80 var YAML = require('yamljs');
81 var bulk = require('bulk-require');
82
83 require.extensions['.yml'] = function(module, filename) {
84 var yaml_string = fs.readFileSync(filename, 'utf8').toString();
85 module.exports = YAML.parse(yaml_string);
86 };
87
88 config = bulk(configPath, ['**/*.js','**/*.json', '**/*.yml']);
89 }
90 else if(configPath.split(".").pop() === "yml"){
91 config = require('yamljs').parse(fs.readFileSync(configPath, 'utf8').toString());
92 }
93 else{
94 config = require(configPath);
95 }
96
97 if(app_config.mode) config.mode = app_config.mode;
98 config = JSON.parse(JSON.stringify(config)); // this will keep modules untouched
99
100 return config
101 }
102
103 var getConfigPath = module.exports.getConfigPath = function(rootDir){
104 return ( fs.existsSync(path.join(rootDir, "config" )) ? path.join(rootDir, "config" ) : false )
105 || ( fs.existsSync(path.join(rootDir, "config.js" )) ? path.join(rootDir, "config.js" ) : false )
106 || ( fs.existsSync(path.join(rootDir, "config.json" )) ? path.join(rootDir, "config.json" ) : false )
107 || ( fs.existsSync(path.join(rootDir, "config.yml" )) ? path.join(rootDir, "config.yml" ) : false )
108 }
109
110 var extendConfig = module.exports.extendConfig = function( config ){
111 if( !hasConfig(config.rootDir) ) return config; // Nothing to extend
112 var isMaster = cluster.isMaster;
113
114 // Workers will get their from process.env
115 if( config.process_mode === "cluster" && !isMaster ) return config;
116 var extension = module.exports.loadConfig(module.exports.getConfigPath(config.rootDir), config);
117
118 var mode_extension;
119 if(config.mode){
120 mode_extension = extension[config.mode];
121 delete extension[config.mode]
122 }
123
124 if(mode_extension){
125 helpers.deepExtend( extension, mode_extension );
126 }
127 helpers.deepExtend(config, extension);
128
129 if(cli_config) helpers.deepExtend(config, cli_config);
130
131 config.options = cli_options;
132
133 return config;
134 };
135
136}
137
138