-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
163 lines (147 loc) · 3.8 KB
/
index.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
'use strict';
var Wiretree = require('wiretree'),
getBlockPaths = require('./lib/paths.js'),
getConfig = require('./lib/getConfig.js'),
builder = require('./lib/builder.js'),
path = require('path'),
fs = require('fs');
var serie = function (target, functions, callback) {
var fns = functions.slice();
callback = callback || function (err) {
if (err) { throw err;}
};
var next = function (err) {
if (err) { return callback.call( target, err );}
if (!fns.length) {
return callback.call( target );
}
fns.shift().call( target, next );
};
next();
};
// generate a counter tick that fires `callback` when called `limit` times
var newCounter = function (limit, callback) {
var cursor = 0,
errors = [];
return function (err) {
if (err) { errors.push( err );}
if (++cursor >= limit) {
if (errors.length) { return callback( errors );}
callback();
}
};
};
var Hardwire = function (options, resolve) {
options = options || {};
this.engine = options.engine || false;
if (!this.engine) {
throw new Error('Engine name required');
}
this.folder = path.resolve( options.folder || '.');
this.output = path.resolve( options.folder, options.output || 'build' );
this.buildFolders = ['config'].concat( options.buildFolders || []);
this.config = options.config || {};
this.environment = options.environment || process.env.NODE_ENV !== 'default' ? process.env.NODE_ENV : false;
this.envProcessing = options.envProcessing || false;
this.tree = new Wiretree( this.folder );
// build stuff
if (options.beforeBuild) {
this.beforeBuild = options.beforeBuild;
}
if (options.afterBuild) {
this.afterBuild = options.afterBuild;
}
// config stuff
if (options.beforeConfig) {
this.beforeConfig = options.beforeConfig;
}
if (options.afterConfig) {
this.afterConfig = options.afterConfig;
}
// loading stuff
if (options.beforeLoad) {
this.beforeLoad = options.beforeLoad;
}
if (options.load) {
this.load = options.load;
}
if (options.afterLoad) {
this.afterLoad = options.afterLoad;
}
// loading stuff
if (options.afterAll) {
this.afterAll = options.afterAll;
}
// load environment variables from .env file
if (fs.existsSync( this.folder + '/.env' )) {
require('dotenv').config({ path: this.folder + '/.env' }).load();
}
if (resolve) {
this.resolve();
}
};
// Build middleware
Hardwire.prototype.beforeBuild = function (next) {
next();
};
Hardwire.prototype.builder = builder;
Hardwire.prototype.afterBuild = function (next) {
next();
};
// Config middleware
Hardwire.prototype.beforeConfig = function (next) {
next();
};
Hardwire.prototype.getConfig = getConfig;
Hardwire.prototype.afterConfig = function (next) {
next();
};
// Load middleware
Hardwire.prototype.beforeLoad = function (next) {
next();
};
Hardwire.prototype.load = function (blockPath, next) {
next();
};
Hardwire.prototype.loadBlocks = function (next) {
var self = this,
count = newCounter( this.blockPaths.length, next );
this.blockPaths.forEach( function (blockPath) {
self.load( blockPath, count );
});
};
Hardwire.prototype.afterLoad = function (next) {
next();
};
Hardwire.prototype.resolve = function () {
var self = this;
this.blockPaths = [ this.folder + '/node_modules/' + this.engine + '/core' ]
.concat( getBlockPaths( this.folder, this.engine ))
.concat( this.folder + '/app');
serie( this, [
this.beforeBuild,
this.builder,
this.afterBuild,
this.beforeConfig,
this.getConfig,
this.afterConfig,
function (next) {
self.tree
.add( 'config', self.config )
.then( next );
},
this.beforeLoad,
this.loadBlocks,
this.afterLoad
], function (err) {
if (err) { return self.afterAll( err );}
self.tree.resolve( function (err) {
self.afterAll( err );
});
});
};
// After all
Hardwire.prototype.afterAll = function (err) {
if (err) { throw err;}
};
module.exports = Hardwire;