This repository has been archived by the owner on Apr 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
205 lines (162 loc) · 5.16 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
var async = require('async');
var debug = require('debug')('bake');
var getit = require('getit');
var path = require('path');
var rigger = require('rigger');
var fs = require('fs');
var out = require('out');
var formatter = require('formatter');
var Oven = require('./lib/oven.js');
var _ = require('underscore');
/**
# bake-js
Bake is a javascript __application__ build tool that is designed to make
building web apps as simple as possible.
It's core reason for existence is to help with managing the dependencies web
applications will require, and to make it simple for you to include libraries
without having to know where those libraries actually come from.
## NOTE: Project Inactive
Development on this project has all but stopped, as
[the primary author](https://github.com/DamonOehlman) is a
[big fan of browserify](https://twitter.com/DamonOehlman/status/466146817608912896).
## Documentation
Full documentation for bake is available at:
<http://bakejs.readthedocs.org/>
**/
function _help() {
var commands = [],
files = fs.readdirSync(path.resolve(__dirname, 'commands')),
actionHelp = formatter(' {{ 0|len:30 }}{{ 1 }}');
// now iterate through the commands and provide some output
out(' Available Actions:\n');
(files || []).forEach(function(file) {
commands.push({
name: path.basename(file, '.js'),
module: require('./commands/' + file)
});
});
commands.forEach(function(command) {
out(actionHelp(command.name, command.module.desc));
});
out(' ');
}
function _process(files, opts, callback) {
var oven, writer, recipe;
// remap args if required
if (typeof opts == 'function') {
callback = opts;
opts = {};
}
// ensure we have opts
opts = opts || {};
// default the build type to oldschool
opts.buildstyle = opts.buildstyle || 'glob';
opts.output = opts.output || 'dist';
// load the required writer
if (! opts.dummyRun) {
try {
writer = require('./lib/writers/' + opts.buildstyle);
}
catch (e) {
callback(new Error('Unable to load the writer for buildstyle: ' + opts.buildstyle));
return;
}
}
// create the oven
oven = new Oven(opts);
// if the files argument is not an array, make it one
if (! Array.isArray(files)) {
files = [files];
}
if (! opts.quiet) {
oven.on('warn', function(err) {
out(err.message);
});
}
// process each of the files specified
async.forEach(
files,
function(targetFile, itemCallback) {
if (! opts.quiet) {
out('!{grey}reading: !{grey,underline}{0}', targetFile);
}
rigger(path.resolve(targetFile), function(err, data) {
if (err) {
itemCallback(err);
}
else {
// tell the process what the base path is
oven.basePath = path.resolve(path.dirname(targetFile));
// add the recipe
recipe = oven.addRecipe(data, path.basename(targetFile));
// route events correctly
oven.on('done', function(recipes) {
if (writer) {
if (! opts.quiet) {
out('\n!{grey}=== SORTING RECIPES IN ORDER OF REQUIREMENT\n !{magenta}' + _.pluck(recipes, 'name').join('\n !{magenta}'));
out('\n!{grey}=== GENERATING OUTPUT FILES');
}
// get the writer to write the files
writer(recipe.name, recipes, opts, function(err) {
// remove the item recipe
delete oven.recipes[recipe.name];
// trigger the item callback
itemCallback(err);
});
}
else {
itemCallback();
}
});
oven.on('error', itemCallback);
if (! opts.quiet) out('\n!{grey}=== LOADING RECIPE REQUIREMENTS');
oven.process();
}
});
},
callback
);
return oven;
}
exports = module.exports = function(args, program) {
var action = typeof args == 'string' ? args : args[0];
if (! action) {
out(program ? program.helpInformation() : 'An action or file is required');
return;
}
// use the local bakery path if one not provided
program.bakery = program.bakery || path.resolve(__dirname, '../data/bakery');
// if the action maps to a path, then process that path
fs.stat(action, function(err, stats) {
if ((! err) && (stats.isFile() || stats.isDirectory())) {
var files = [action];
// if the target path is a directory, then get the files from that directory
if (stats.isDirectory()) {
files = (fs.readdirSync(action) || []).map(function(file) {
return path.join(action, file);
});
}
_process(files, program, function(err) {
if (err) {
out.error(err);
}
else {
out('\n!{grey}=== DONE !{check, green}');
}
}, stats.isDirectory());
}
else {
var handler;
try {
handler = require('./commands/' + action);
}
catch (e) {
}
if (typeof handler == 'function') {
handler.apply(null, args.slice(1).concat(program));
}
}
});
};
exports.help = _help;
exports.process = _process;