Leaflet/build/build.js

185 lines
4.2 KiB
JavaScript
Raw Normal View History

2011-12-12 18:44:16 +08:00
var fs = require('fs'),
2012-12-18 03:32:00 +08:00
jshint = require('jshint'),
2012-12-18 01:15:19 +08:00
UglifyJS = require('uglify-js'),
2012-12-18 03:32:00 +08:00
deps = require('./deps.js').deps,
hintrc = require('./hintrc.js').config;
2012-12-18 01:15:19 +08:00
function lintFiles(files) {
var errorsFound = 0,
2012-12-18 03:32:00 +08:00
i, j, len, len2, src, errors, e;
2012-12-18 01:15:19 +08:00
for (i = 0, len = files.length; i < len; i++) {
jshint.JSHINT(fs.readFileSync(files[i], 'utf8'), hintrc, i ? {L: true} : null);
2012-12-18 01:15:19 +08:00
errors = jshint.JSHINT.errors;
for (j = 0, len2 = errors.length; j < len2; j++) {
e = errors[j];
2012-12-18 03:32:00 +08:00
console.log(files[i] + '\tline ' + e.line + '\tcol ' + e.character + '\t ' + e.reason);
2012-12-18 01:15:19 +08:00
}
errorsFound += len2;
}
return errorsFound;
}
function getFiles(compsBase32) {
2011-12-12 18:44:16 +08:00
var memo = {},
2012-12-18 03:32:00 +08:00
comps;
2011-12-12 18:44:16 +08:00
if (compsBase32) {
comps = parseInt(compsBase32, 32).toString(2).split('');
console.log('Managing dependencies...')
}
function addFiles(srcs) {
for (var j = 0, len = srcs.length; j < len; j++) {
memo[srcs[j]] = true;
}
}
for (var i in deps) {
if (comps) {
if (parseInt(comps.pop(), 2) === 1) {
console.log('\t* ' + i);
addFiles(deps[i].src);
} else {
console.log('\t ' + i);
}
} else {
addFiles(deps[i].src);
}
}
var files = [];
for (var src in memo) {
files.push('src/' + src);
}
return files;
2012-12-18 01:15:19 +08:00
}
2011-12-12 18:44:16 +08:00
2013-01-31 02:48:22 +08:00
exports.getFiles = getFiles;
2012-12-18 01:15:19 +08:00
exports.lint = function () {
2011-12-12 18:44:16 +08:00
2012-12-18 01:15:19 +08:00
var files = getFiles();
2011-12-12 18:44:16 +08:00
2012-12-18 01:15:19 +08:00
console.log('Checking for JS errors...');
2012-12-18 01:15:19 +08:00
var errorsFound = lintFiles(files);
2011-12-12 18:44:16 +08:00
2012-12-18 01:15:19 +08:00
if (errorsFound > 0) {
console.log(errorsFound + ' error(s) found.\n');
fail();
} else {
console.log('\tCheck passed');
2011-12-12 18:44:16 +08:00
}
2011-12-13 06:41:56 +08:00
};
2011-12-12 18:44:16 +08:00
2011-12-13 06:41:56 +08:00
2012-12-18 01:15:19 +08:00
function getSizeDelta(newContent, oldContent) {
if (!oldContent) {
return 'new';
}
var newLen = newContent.replace(/\r\n?/g, '\n').length,
oldLen = oldContent.replace(/\r\n?/g, '\n').length,
delta = newLen - oldLen;
return (delta >= 0 ? '+' : '') + delta;
}
function loadSilently(path) {
2011-12-13 06:41:56 +08:00
try {
2012-12-18 01:15:19 +08:00
return fs.readFileSync(path, 'utf8');
2011-12-13 06:41:56 +08:00
} catch (e) {
return null;
}
2012-12-18 01:15:19 +08:00
}
2011-12-13 06:41:56 +08:00
2012-12-18 01:15:19 +08:00
function combineFiles(files) {
var content = '';
for (var i = 0, len = files.length; i < len; i++) {
2012-12-18 03:32:00 +08:00
content += fs.readFileSync(files[i], 'utf8') + '\n\n';
2012-12-18 01:15:19 +08:00
}
return content;
}
exports.build = function (compsBase32, buildName) {
var files = getFiles(compsBase32);
console.log('Concatenating ' + files.length + ' files...');
2012-12-18 03:32:00 +08:00
var copy = fs.readFileSync('src/copyright.js', 'utf8'),
intro = '(function (window, document, undefined) {',
outro = '}(this, document));',
2012-12-18 01:15:19 +08:00
newSrc = copy + intro + combineFiles(files) + outro,
pathPart = 'dist/leaflet' + (buildName ? '-' + buildName : ''),
srcPath = pathPart + '-src.js',
oldSrc = loadSilently(srcPath),
srcDelta = getSizeDelta(newSrc, oldSrc);
console.log('\tUncompressed size: ' + newSrc.length + ' bytes (' + srcDelta + ')');
if (newSrc === oldSrc) {
console.log('\tNo changes');
} else {
fs.writeFileSync(srcPath, newSrc);
console.log('\tSaved to ' + srcPath);
}
console.log('Compressing...');
var path = pathPart + '.js',
oldCompressed = loadSilently(path),
newCompressed = copy + UglifyJS.minify(newSrc, {
2012-12-18 03:32:00 +08:00
warnings: true,
fromString: true
2012-12-18 01:15:19 +08:00
}).code,
delta = getSizeDelta(newCompressed, oldCompressed);
console.log('\tCompressed size: ' + newCompressed.length + ' bytes (' + delta + ')');
if (newCompressed === oldCompressed) {
console.log('\tNo changes');
} else {
fs.writeFileSync(path, newCompressed);
console.log('\tSaved to ' + path);
2011-12-13 06:41:56 +08:00
}
};
2013-01-30 22:06:19 +08:00
2013-01-31 02:41:13 +08:00
exports.test = function() {
var testacular = require('testacular'),
testConfig = {configFile : __dirname + '/../spec/testacular.conf.js'};
2013-02-04 19:42:27 +08:00
2013-01-30 22:06:19 +08:00
testConfig.browsers = ['PhantomJS'];
isArgv('--chrome') && testConfig.browsers.push('Chrome');
isArgv('--ff') && testConfig.browsers.push('Firefox');
2013-02-02 15:29:20 +08:00
2013-02-04 19:42:27 +08:00
// will work only with new testacular that supports code coverage (today it's in master)
if (isArgv('--cov')) { // temporary hack until testacular with coverage becomes stable
testacular = require('../node_modules/testacular/lib/index.js'); // use local testacular
testConfig.preprocessors = {
'**/src/**/*.js': 'coverage',
};
testConfig.coverageReporter = {
type : 'html',
dir : 'coverage/'
};
testConfig.reporters = ['coverage'];
}
2013-01-30 22:06:19 +08:00
testacular.server.start(testConfig);
function isArgv(optName){
return process.argv.indexOf(optName) !== -1;
}
}