forked from saebekassebil/teoria
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJakefile
207 lines (173 loc) · 5.08 KB
/
Jakefile
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
/*jshint node:true */
/*global desc:true task:true complete:true jake:true*/
'use strict';
var path = require('path'),
fs = require('fs'),
mingler = require('mingler'),
existsSync = 'existsSync' in fs ? fs.existsSync : path.existsSync,
colors;
try {
colors = require('colors');
} catch (e) {}
// Default settings
var settings = {
minify: false,
silent: false,
colors: true
};
// Log colors if the npm module colors is available
var logcolors = {
info: 'cyan',
warn: 'yellow',
error: 'red'
};
// Utility function which respects the 'silent' setting
function log(text, type, acolor, nolabel) {
type = (typeof console[type] === 'function') ? type : 'info';
if(!settings.silent) {
if(!nolabel) {
text = ' ['+type.toUpperCase()+'] ' + text;
} else {
text = ' ' + text;
}
if(settings.colors && colors) {
acolor = logcolors[acolor || type] || 'grey';
console[type](text[acolor]);
} else {
console[type](text);
}
}
}
// Constants
var kDistDir = 'dist/';
var kSrcDir = 'src/';
var kMainFile = 'core.js';
var kDistFilename = 'teoria.js';
var kDistMinFilename = 'teoria.min.js';
var kFileList = [
kSrcDir + 'core.js',
kSrcDir + 'note.js',
kSrcDir + 'interval.js',
kSrcDir + 'chord.js',
kSrcDir + 'scale.js',
'Jakefile'
];
// Default task - lint and build
desc('Default task both lints and build the entire project');
task({'default': ['lint', 'build']}, function(){});
// Concatenates the files
desc('Concatenates all files into dist/teoria[.min].js');
task('build', function() {
var params, filename;
params = Array.prototype.slice.call(arguments);
params.forEach(function(el) {
if(el in settings) {
settings[el] = !settings[el];
} else {
log('Ignoring invalid settings: ' + el, 'warn');
}
});
if (!existsSync(kDistDir)) {
log('Creating ' + kDistDir + ' directory');
fs.mkdirSync(kDistDir, 0x01FF); // With 0777 mode
}
// Change to /src dir
process.chdir(kSrcDir);
mingler.on('complete', function(concatenation) {
// Should the source be minified?
if(settings.minify) {
var ugly, ast, ratio, compressed;
try {
ugly = require('uglify-js');
} catch(e) {
return log('uglify-js module doesn\'t appear to be installed. Use:' +
'`npm install uglify-js`', 'error');
}
log('Minifying');
ast = ugly.parser.parse(concatenation);
ast = ugly.uglify.ast_mangle(ast);
ast = ugly.uglify.ast_squeeze(ast);
compressed = ugly.uglify.gen_code(ast);
ratio = 100 - (compressed.length/concatenation.length) * 100;
ratio = ratio.toString().substr(0, 4);
log('Saved ' + ratio + '% of the original size');
concatenation = compressed;
filename = kDistMinFilename;
} else {
filename = kDistFilename;
}
// Write to file and close!
log('Writing to output file \'' + filename + '\'');
fs.writeFileSync(kDistDir + filename, concatenation, 'utf8');
log('Concatenation completed - Goodbye!');
complete();
});
mingler.on('error', function(error) {
log(error, 'error');
process.exit(1);
});
mingler.on('warning', function(warning) {
log(warning, 'warn');
});
mingler.on('concatenate', function(feedback) {
log("Concatenating: " + feedback.filename, 'info', 'grey');
});
mingler.mingle(kMainFile, function() {
process.chdir('../');
});
}, {async: true});
desc('Builds the project and unit tests it');
task('test', ['build'], function() {
jake.exec('node test/teoria.js', function() {
complete();
}, {printStdout: true});
}, {async: true});
// Lints the files according to .jshintrc
desc('Lint all files according to coding standards');
task('lint', function() {
var jshint, config, errors, errorfilecount, content;
try {
jshint = require('jshint');
} catch(e) {
console.log(e);
log('jshint doesn\'t appear to be installed. Do a `npm install -g jshint`',
'error');
return false;
}
// Load configuration
config = fs.readFileSync('./.jshintrc', 'utf8');
config = JSON.parse(config);
// List all files in src/
errors = [], errorfilecount = 0;
kFileList.forEach(function(file) {
try {
content = fs.readFileSync(file, 'utf8');
} catch(e) {
log('Unable to open file ' + file, 'error');
}
content = content.replace(/^\uFEFF/, ''); // Remove Unicode BOM
if(!jshint.JSHINT(content, config)) {
errorfilecount++;
jshint.JSHINT.errors.forEach(function(error) {
if(error) {
errors.push({file: file, error: error});
}
});
}
});
log(kFileList.length.toString(10) + ' files linted, ' +
(kFileList.length - errorfilecount) +
' successfully, ' + errorfilecount + ' with errors!', 'info');
// Show errors
errors.forEach(function(error) {
log(error.file + ': line ' + error.error.line +
', col ' + error.error.character + ', ' + error.error.reason,
'error', 'grey', true);
});
// Show lint status
if(errors.length === 0) {
log('Lint passed', 'info');
} else {
log('Lint failed!', 'error');
}
});