-
Notifications
You must be signed in to change notification settings - Fork 9
/
square
executable file
·241 lines (194 loc) · 5.46 KB
/
square
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env node
"use strict";
/**
* Library modules
*/
var path = require('path')
, color = require('colors')
, findit = require('findit')
, _ = require('underscore')._
, program = require('commander');
/**
* Do we need to silence all the output because we spit the shit to std out?
*/
program.silence = ~process.argv.indexOf('--output') || ~process.argv.indexOf('-o');
/**
* Square library.
*/
var Square = require('../lib/square')
, npm = require('../package')
, square = new Square();
/**
* Argument filters for commander.
*
* @type {Object}
* @api private
*/
var filters = {
/**
* Split the supplied argument to create a list (array)
*
* @param {String} val the commandline flag value
* @returns {Array}
* @api private
*/
list: function list (val) {
return val.split(',');
}
/**
* Checks if the given option should be ignored.
*
* @param {Mixed} ignore
* @api private
*/
, ignore: function ignoring (ignore) {
return ~this.indexOf(ignore);
}
};
/**
* Setup the commandline interface.
*/
program
.version(npm.version)
.usage('[options]')
.option('-b, --bundle <dir>', 'the location of the package file')
.option('-e, --extension <extension>', 'filter on this file extension')
.option('-w, --watch', 'watch the bundled files for changes and rebuild')
.option('-o, --output', 'output the contents to stdout instead of a file')
// options that require argument filtering
.option('-i, --ignore [files]', 'ignore these files', filters.list)
.option('-f, --filename <file>', 'alternate filenames', filters.list)
.option('-p, --plugin <plugins>', 'which plugins should be included', filters.list);
/**
* Adding some basic commands
*/
program
.command('init')
.description('initialize an empty project')
.action(function action () {
require('./initialize')(program, square);
});
/**
* Display addition help information.
*/
program.on('--help', function help () {
console.log('');
console.log(' Examples:');
console.log('');
console.log([
' # Building for development with multiple plugins \n'.grey
, ' square --bundle '.cyan
, './'.white
, ' --plugin '.cyan
, 'debug,crush'.white
].join(''));
console.log('');
console.log([
' # Only build css files \n'.grey
, ' square --bundle '.cyan
, './other/directory'.white
, ' --plugin '.cyan
, 'crush'.white
, ' --extension '.cyan
, 'css'.white
].join(''));
process.exit();
});
/**
* Start watching files for changes, but only if we have actually done one
* succesfull build before.
*/
program.on('--watch', function watching () {
square.once('build', function builded () {
var watch = require('../lib/watch')
, extensions = [ program.extension || 'js' ]
, dir = program.bundle;
// start watching for file changes in the given directory
watch(program.bundle, extensions, function update (files) {
square.refresh(files);
square.build(program.extension);
});
});
});
// output the help if it's called without arguments
if (!process.argv.slice(2).length) process.argv.push('--help');
// output a fancy pancy logo, [square] for le win.
if (!program.silence)
[
''
, 'o-o o-o o o o-o o-o o-o '.cyan
, ' \\ | | | | |-| | |- '.cyan + (' version: ' + npm.version).white
, 'o-o o-O o--o o o-o o-o '.cyan
, ' | '.cyan
, ' o '.cyan
, ''
].forEach(function each (line) {
console.log(' ' + line);
});
// all the event listeners are attached can we can start parsing the arguments, as
// this a sync call, we need to be 100% sure that every listener is added.
program.parse(process.argv);
/**
* Set some defaults based on the flags.
*/
program.filename = Array.isArray(program.filename)
? program.filename
: ['square', 'bundle'];
program.ignore = Array.isArray(program.ignore)
? program.ignore
: [];
program.bundle = program.bundle || process.env.PWD;
program.plugin = Array.isArray(program.plugin)
? program.plugin
: [];
// how do we need to output data
square.stdout = !!program.output;
/**
* Trigger some events based on the params
*/
if (program.watch) program.emit('--watch');
/**
* Display a not found message.
*
* @api private
*/
program.notFound = function notFound () {
[
'We are unable to find the '.white
+ (program.filename.join('.json, ') + '.json').red
+ ' files.'.white
, ''
, 'Please double check if these files are located in the '.white
+ program.bundle.red
+ ' directory.'.white
, 'Thanks for flying Square.'.white
].forEach(function output (line) {
square.logger.error(line);
});
};
/**
* Make sure that the bundle location exists, so we don't get any ENOENT errors
*/
if (!path.existsSync(program.bundle)) {
return program.notFound();
}
/**
* Start search the given directory tree for square.json files.
*/
var found = findit.find(program.bundle)
, filenames = new RegExp('\\/('+ program.filename.join('|') +')\\.json', 'gi')
, files = [];
found.on('file', function foundFile (file, fstat) {
if (!filenames.test(file)) return;
if (program.ignore.some(filters.ignore.bind(file))) return;
files.push(file);
});
found.on('end', function end () {
if (!files.length) return program.notFound();
square.bundle(files[0]);
// process the plugins
program.plugin.forEach(function load (plugin) {
square.plugin(plugin);
});
square.build(program.extension);
});