2015-03-09 05:16:17 -05:00
|
|
|
var fs = require('fs'),
|
|
|
|
UglifyJS = require('uglify-js'),
|
|
|
|
zlib = require('zlib'),
|
2016-08-11 03:59:06 -05:00
|
|
|
browserify = require('browserify'),
|
2016-08-11 05:50:27 -05:00
|
|
|
browserifyCss = require('browserify-css'),
|
2016-08-11 05:58:04 -05:00
|
|
|
exorcist = require('exorcist'),
|
2015-03-09 05:16:17 -05:00
|
|
|
|
2016-07-31 02:57:20 -05:00
|
|
|
deps = require('./deps.js').deps,
|
2016-03-05 07:56:03 -06:00
|
|
|
adminDeps = require('./adminDeps.js').adminDeps;
|
2015-03-09 05:16:17 -05:00
|
|
|
|
2016-12-09 06:36:53 -06:00
|
|
|
// TODO: Don't hardcode leaflet-draw version here
|
|
|
|
var JSBundleFiles = [
|
|
|
|
'main.js',
|
|
|
|
'dist/leaflet-src.js',
|
|
|
|
'dist/errormessages.js',
|
|
|
|
'dist/plugins/draw-0.2.4/dist/leaflet.draw.js'
|
|
|
|
];
|
|
|
|
|
|
|
|
var CSSBundleFiles = [
|
|
|
|
'main.css',
|
|
|
|
'dist/leaflet.css',
|
|
|
|
'dist/selectionMarkers.css',
|
|
|
|
'dist/loleaflet.css',
|
|
|
|
'dist/toolbar.css',
|
|
|
|
'dist/partsPreviewControl.css',
|
|
|
|
'dist/scrollBar.css',
|
|
|
|
'dist/searchControl.css',
|
|
|
|
'dist/spreadsheet.css',
|
2017-01-09 02:45:24 -06:00
|
|
|
'dist/menubar.css',
|
2016-12-09 06:36:53 -06:00
|
|
|
'plugins/draw-0.2.4/dist/leaflet.draw.css',
|
|
|
|
];
|
|
|
|
|
|
|
|
var adminBundleFiles = [
|
|
|
|
'main-admin.js',
|
|
|
|
'dist/admin/admin-src.js',
|
|
|
|
'dist/admin/bootstrap/ie10-viewport-bug-workaround.css',
|
|
|
|
'admin.strings.js',
|
|
|
|
'dist/admin/bootstrap/holder.min.js',
|
|
|
|
'dist/admin/bootstrap/ie10-viewport-bug-workaround.js'
|
|
|
|
];
|
|
|
|
|
|
|
|
exports.getBundleFiles = function() {
|
|
|
|
return JSBundleFiles.concat(CSSBundleFiles);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.getAdminBundleFiles = function() {
|
|
|
|
return adminBundleFiles;
|
|
|
|
};
|
|
|
|
|
2015-03-09 05:16:17 -05:00
|
|
|
function getFiles(compsBase32) {
|
|
|
|
var memo = {},
|
|
|
|
comps;
|
|
|
|
|
|
|
|
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(' * ' + i);
|
|
|
|
addFiles(deps[i].src);
|
|
|
|
} else {
|
|
|
|
console.log(' ' + i);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
addFiles(deps[i].src);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('');
|
|
|
|
|
|
|
|
var files = [];
|
|
|
|
|
|
|
|
for (var src in memo) {
|
|
|
|
files.push('src/' + src);
|
|
|
|
}
|
|
|
|
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
2016-03-05 07:56:03 -06:00
|
|
|
function getAdminFiles() {
|
|
|
|
var files = [];
|
|
|
|
|
|
|
|
for (var i in adminDeps) {
|
|
|
|
for (var j = 0, len = adminDeps[i].src.length; j < len; j++) {
|
|
|
|
files.push('src/' + adminDeps[i].src[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
2015-03-09 05:16:17 -05:00
|
|
|
exports.getFiles = getFiles;
|
2016-12-09 06:36:53 -06:00
|
|
|
exports.getAdminFiles = getAdminFiles;
|
2015-03-09 05:16:17 -05:00
|
|
|
|
|
|
|
function getSizeDelta(newContent, oldContent, fixCRLF) {
|
|
|
|
if (!oldContent) {
|
|
|
|
return ' (new)';
|
|
|
|
}
|
|
|
|
if (newContent === oldContent) {
|
|
|
|
return ' (unchanged)';
|
|
|
|
}
|
|
|
|
if (fixCRLF) {
|
|
|
|
newContent = newContent.replace(/\r\n?/g, '\n');
|
|
|
|
oldContent = oldContent.replace(/\r\n?/g, '\n');
|
|
|
|
}
|
|
|
|
var delta = newContent.length - oldContent.length;
|
|
|
|
|
|
|
|
return delta === 0 ? '' : ' (' + (delta > 0 ? '+' : '') + delta + ' bytes)';
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadSilently(path) {
|
|
|
|
try {
|
|
|
|
return fs.readFileSync(path, 'utf8');
|
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function combineFiles(files) {
|
|
|
|
var content = '';
|
|
|
|
for (var i = 0, len = files.length; i < len; i++) {
|
|
|
|
content += fs.readFileSync(files[i], 'utf8') + '\n\n';
|
|
|
|
}
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
|
|
|
function bytesToKB(bytes) {
|
|
|
|
return (bytes / 1024).toFixed(2) + ' KB';
|
2016-08-11 03:16:11 -05:00
|
|
|
}
|
2015-03-09 05:16:17 -05:00
|
|
|
|
2016-12-09 06:36:53 -06:00
|
|
|
function bundle(files, destFilename, debug, minify, callback) {
|
2016-08-11 04:16:55 -05:00
|
|
|
var bundler = browserify(files, {debug: debug});
|
2016-08-16 00:37:55 -05:00
|
|
|
bundler = bundler.transform(browserifyCss);
|
|
|
|
if (minify) {
|
|
|
|
console.log('uglifying');
|
|
|
|
bundler.transform({
|
|
|
|
global: true
|
|
|
|
}, 'uglifyify');
|
|
|
|
}
|
2016-08-11 03:59:06 -05:00
|
|
|
var bundleFs = fs.createWriteStream('dist/' + destFilename);
|
2016-08-11 11:08:13 -05:00
|
|
|
var res = bundler.bundle();
|
|
|
|
if (debug) {
|
|
|
|
res = res.pipe(exorcist('dist/' + destFilename + '.map'));
|
|
|
|
}
|
|
|
|
res.pipe(bundleFs);
|
2016-08-11 03:59:06 -05:00
|
|
|
|
|
|
|
bundleFs.on('finish', function() {
|
|
|
|
console.log('Finish writing to dist/' + destFilename);
|
2016-12-09 06:36:53 -06:00
|
|
|
callback();
|
2016-08-11 03:59:06 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-12-09 06:36:53 -06:00
|
|
|
exports.bundle = function(debug, minify, callback) {
|
|
|
|
bundle(['main.js'], 'bundle.js', debug, minify, callback);
|
2016-08-11 03:59:06 -05:00
|
|
|
};
|
|
|
|
|
2016-12-09 06:36:53 -06:00
|
|
|
exports.bundleAdmin = function(debug, minify, callback) {
|
|
|
|
bundle(['main-admin.js'], 'admin-bundle.js', debug, minify, callback);
|
2016-08-11 03:59:06 -05:00
|
|
|
};
|
|
|
|
|
2015-03-09 05:16:17 -05:00
|
|
|
exports.build = function (callback, version, compsBase32, buildName) {
|
|
|
|
|
|
|
|
var files = getFiles(compsBase32);
|
|
|
|
|
|
|
|
console.log('Concatenating and compressing ' + files.length + ' files...');
|
|
|
|
|
|
|
|
var copy = fs.readFileSync('src/copyright.js', 'utf8').replace('{VERSION}', version),
|
|
|
|
intro = '(function (window, document, undefined) {',
|
|
|
|
outro = '}(window, document));',
|
|
|
|
newSrc = copy + intro + combineFiles(files) + outro,
|
|
|
|
|
|
|
|
pathPart = 'dist/leaflet' + (buildName ? '-' + buildName : ''),
|
|
|
|
srcPath = pathPart + '-src.js',
|
|
|
|
|
|
|
|
oldSrc = loadSilently(srcPath),
|
|
|
|
srcDelta = getSizeDelta(newSrc, oldSrc, true);
|
|
|
|
|
|
|
|
console.log('\tUncompressed: ' + bytesToKB(newSrc.length) + srcDelta);
|
|
|
|
|
|
|
|
if (newSrc !== oldSrc) {
|
|
|
|
fs.writeFileSync(srcPath, newSrc);
|
|
|
|
console.log('\tSaved to ' + srcPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
var path = pathPart + '.js',
|
|
|
|
oldCompressed = loadSilently(path),
|
|
|
|
newCompressed = copy + UglifyJS.minify(newSrc, {
|
|
|
|
warnings: true,
|
|
|
|
fromString: true
|
|
|
|
}).code,
|
|
|
|
delta = getSizeDelta(newCompressed, oldCompressed);
|
|
|
|
|
|
|
|
console.log('\tCompressed: ' + bytesToKB(newCompressed.length) + delta);
|
|
|
|
|
|
|
|
var newGzipped,
|
|
|
|
gzippedDelta = '';
|
|
|
|
|
|
|
|
function done() {
|
|
|
|
if (newCompressed !== oldCompressed) {
|
|
|
|
fs.writeFileSync(path, newCompressed);
|
|
|
|
console.log('\tSaved to ' + path);
|
|
|
|
}
|
|
|
|
console.log('\tGzipped: ' + bytesToKB(newGzipped.length) + gzippedDelta);
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
zlib.gzip(newCompressed, function (err, gzipped) {
|
|
|
|
if (err) { return; }
|
|
|
|
newGzipped = gzipped;
|
|
|
|
if (oldCompressed && (oldCompressed !== newCompressed)) {
|
|
|
|
zlib.gzip(oldCompressed, function (err, oldGzipped) {
|
|
|
|
if (err) { return; }
|
|
|
|
gzippedDelta = getSizeDelta(gzipped, oldGzipped);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-12-09 06:36:53 -06:00
|
|
|
exports.buildadmin = function(callback) {
|
|
|
|
// TODO: Also minify if admin complexity increases in future
|
|
|
|
var adminNewSrc = combineFiles(getAdminFiles()),
|
|
|
|
adminPath = 'dist/admin/admin-src.js',
|
|
|
|
adminOldSrc = loadSilently(adminPath),
|
|
|
|
adminSrcDelta = getSizeDelta(adminNewSrc, adminOldSrc, true);
|
|
|
|
|
|
|
|
if (adminSrcDelta !== ' (unchanged)') {
|
|
|
|
fs.writeFileSync(adminPath, adminNewSrc);
|
|
|
|
console.log('\tAdmin files saved to ' + adminPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
callback();
|
|
|
|
};
|
|
|
|
|
2015-03-09 05:16:17 -05:00
|
|
|
exports.test = function(complete, fail) {
|
|
|
|
var karma = require('karma'),
|
|
|
|
testConfig = {configFile : __dirname + '/../spec/karma.conf.js'};
|
|
|
|
|
|
|
|
testConfig.browsers = ['PhantomJS'];
|
|
|
|
|
|
|
|
function isArgv(optName) {
|
|
|
|
return process.argv.indexOf(optName) !== -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isArgv('--chrome')) {
|
|
|
|
testConfig.browsers.push('Chrome');
|
|
|
|
}
|
|
|
|
if (isArgv('--safari')) {
|
|
|
|
testConfig.browsers.push('Safari');
|
|
|
|
}
|
|
|
|
if (isArgv('--ff')) {
|
|
|
|
testConfig.browsers.push('Firefox');
|
|
|
|
}
|
|
|
|
if (isArgv('--ie')) {
|
|
|
|
testConfig.browsers.push('IE');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isArgv('--cov')) {
|
|
|
|
testConfig.preprocessors = {
|
|
|
|
'src/**/*.js': 'coverage'
|
|
|
|
};
|
|
|
|
testConfig.coverageReporter = {
|
|
|
|
type : 'html',
|
|
|
|
dir : 'coverage/'
|
|
|
|
};
|
|
|
|
testConfig.reporters = ['coverage'];
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('Running tests...');
|
|
|
|
|
|
|
|
karma.server.start(testConfig, function(exitCode) {
|
|
|
|
if (!exitCode) {
|
|
|
|
console.log('\tTests ran successfully.\n');
|
|
|
|
complete();
|
|
|
|
} else {
|
|
|
|
process.exit(exitCode);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|