blob: 11fb3162578b47b25155a331b302d5162ee43233 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF
* or more contributor license agreements. See th
* distributed with this work for additional infor
* regarding copyright ownership. The ASF license
* to you under the Apache License, Version 2.0 (t
* "License"); you may not use this file except in
* with the License. You may obtain a copy of the
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to
* software distributed under the License is distr
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
* KIND, either express or implied. See the Licen
* specific language governing permissions and lim
* under the License.
*/
var fs = require('fs');
var path = require('path');
// Iterates over a directory
function forEachFile(root, cbFile, cbDone) {
var count = 0;
function scan(name) {
++count;
fs.stat(name, function (err, stats) {
if (err) cbFile(err);
if (stats.isDirectory()) {
fs.readdir(name, function (err, files) {
if (err) cbFile(err);
files.forEach(function (file) {
scan(path.join(name, file));
});
done();
});
} else if (stats.isFile()) {
cbFile(null, name, stats, done);
} else {
done();
}
});
}
function done() {
--count;
if (count === 0 && cbDone) cbDone();
}
scan(root);
}
module.exports = function processWhiteSpace(processor, callback) {
var rexp_minified = new RegExp("\\.min\\.js$");
var rexp_src = new RegExp('\\.js$');
forEachFile('src', function(err, file, stats, cbDone) {
//if (err) throw err;
if (rexp_minified.test(file) || !rexp_src.test(file)) {
cbDone();
} else {
var origsrc = src = fs.readFileSync(file, 'utf8');
// tabs -> four spaces
if (src.indexOf('\t') >= 0) {
src = src.split('\t').join(' ');
}
// eliminate trailing white space
src = src.replace(/ +\n/g, '\n');
if (origsrc !== src) {
// write it out yo
processor(file, src);
}
cbDone();
}
}, callback);
}