-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmite.js
116 lines (105 loc) · 3.07 KB
/
mite.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"use strict";
var async = require( 'async' )
var path = require( 'path' )
var fs = require( 'fs' )
module.exports = function( options ) {
var seneca = this;
options.key = options.key || '1233212##$#2131'
function loadModules( done ) {
processInjectedFolders( ['constants', 'controller', 'protocol', 'service'], done )
}
function processInjectedFolders( folders, done ) {
seneca.log.debug( 'Process folders:', JSON.stringify( folders ) )
async.waterfall( [
function( done ) {
var paths = []
async.eachSeries( folders, function( item, callback ) {
var folder = path.resolve( __dirname, item )
paths.push( folder )
callback()
}, function( err ) {
if( err ) {
return done( err )
}
return done( null, paths )
} )
},
function( paths, done ) {
var files = [];
async.eachSeries( paths, function( path, callback ) {
seneca.log.debug( 'Processing folder %s', path )
walkdir( path, function( err, fileList ) {
seneca.log.debug( 'walkdir files: %s', fileList )
if( err ) {
seneca.log.error( err )
return callback()
}
else {
for( var f = 0; f < fileList.length; f++ ) {
files.push( fileList[f] );
}
return callback()
}
} )
}, function( err ) {
if( err ) {
return done( err )
}
return done( null, files )
} )
},
function( files, done ) {
async.eachSeries( files, function( file, callback ) {
seneca.log.debug( 'Injecting file: %s', file )
seneca.use( file, options );
callback()
}, function( err ) {
if( err ) {
seneca.log.error('Error processing file: ', file, ' error: ', err)
return done( err )
}
return done();
} )
}],
function( err, result ) {
seneca.log.debug( 'Finished modules injection' )
done( err, result );
} )
}
function walkdir( dir, done ) {
var results = [];
fs.readdir( dir, function( err, list ) {
if( err ) {
return done( err );
}
var pending = list.length;
if( !pending ) {
return done( null, results );
}
list.forEach( function( file ) {
file = dir + '/' + file;
fs.stat( file, function( err, stat ) {
if( stat && stat.isDirectory() ) {
walkdir( file, function( err, res ) {
results = results.concat( res );
if( !--pending ) {
done( null, results );
}
} );
}
else {
results.push( file );
if( !--pending ) {
done( null, results );
}
}
} );
} );
} );
};
function init( msg, respond ){
loadModules( respond )
}
seneca.use( 'seneca-crypt', {password: 'e2klsdf56fd'} )
seneca.add( 'init:mite', init )
}