1 |
|
2 | var fs = require( 'fs' ),
|
3 | express = require( 'express' ),
|
4 | _ = require( 'underscore' ),
|
5 | path = require('path' );
|
6 |
|
7 |
|
8 |
|
9 | function Core( dir ){
|
10 |
|
11 | Core.modulesPath = dir;
|
12 |
|
13 | if( !Core.modules )
|
14 | Core.modules = {
|
15 | app : express(),
|
16 | error : error
|
17 | };
|
18 |
|
19 | return Core.modules;
|
20 | }
|
21 |
|
22 | Core.lock = function(){
|
23 |
|
24 | for( var key in Core.modules )
|
25 | if( Core.modules.hasOwnProperty( key ) )
|
26 | Object.freeze( Core.modules[key] );
|
27 |
|
28 | Object.freeze( Core.modules );
|
29 |
|
30 | };
|
31 |
|
32 | Core.load = function( options, callback ){
|
33 |
|
34 | Core.loadConfig( options );
|
35 |
|
36 | fs.readdirSync( Core.modulesPath ).forEach( function ( corePath ){
|
37 | if( corePath == "config" )
|
38 | return;
|
39 |
|
40 | var stats = fs.lstatSync( path.join( Core.modulesPath, corePath ) );
|
41 |
|
42 | if( stats.isDirectory() ){
|
43 | var mainFile = path.join( Core.modulesPath , corePath, corePath + ".main" );
|
44 |
|
45 | if( fs.existsSync( mainFile + ".js" ) )
|
46 | Core.modules[corePath] = require( mainFile );
|
47 |
|
48 | else
|
49 | Core.error( mainFile + ".js is missing", true );
|
50 | }
|
51 | });
|
52 |
|
53 | if( callback )
|
54 | callback();
|
55 | };
|
56 |
|
57 | Core.loadConfig = function( options ){
|
58 | var configPath = path.join( Core.modulesPath, "config" );
|
59 |
|
60 | if( fs.existsSync( configPath ) ) {
|
61 |
|
62 | Core.modules.config = require( path.join( configPath, "config.main" ) );
|
63 | _.extend( Core.modules.config.globals, options );
|
64 |
|
65 | } else
|
66 | error( "Config is missing", true );
|
67 |
|
68 | };
|
69 |
|
70 | var error = function( msg, fatal ){
|
71 | console.log( new Error( msg.toString().red ).stack.red );
|
72 |
|
73 | if( fatal )
|
74 | process.exit(1);
|
75 | };
|
76 |
|
77 | module.exports = Core; |
\ | No newline at end of file |