Skip to content

Commit

Permalink
better beautify scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Jul 15, 2015
1 parent 232618e commit e68108a
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .jsbeautifyrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
"js": {
"allowed_file_extensions": ["js", "json", "jshintrc", "jsbeautifyrc"],
"brace_style": "collapse",
Expand All @@ -9,7 +9,7 @@
"indent_char": "\t",
"indent_level": 0,
"indent_size": 1,
"indent_with_tabs": false,
"indent_with_tabs": true,
"jslint_happy": false,
"jslint_happy_align_switch_case": true,
"space_after_anon_function": false,
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@
"component-webpack-plugin": "~0.2.0",
"coveralls": "^2.11.2",
"css-loader": "~0.14.0",
"diff": "^1.4.0",
"eslint": "^0.24.0",
"eslint-plugin-nodeca": "^1.0.3",
"express": "~3.4.8",
"extract-text-webpack-plugin": "~0.8.0",
"file-loader": "~0.8.0",
"glob": "^5.0.14",
"i18n-webpack-plugin": "~0.2.0",
"istanbul": "^0.3.13",
"jade-loader": "~0.7.0",
Expand Down Expand Up @@ -71,11 +73,12 @@
"web_modules/"
],
"scripts": {
"pretest": "npm run lint",
"pretest": "npm run lint && npm run beautify-lint",
"test": "mocha",
"travis": "npm run cover -- --report lcovonly",
"lint": "eslint lib",
"jsbeautify": "js-beautify --indent-with-tabs --end-with-newline lib/**/*.js lib/*.js -r",
"beautify-lint": "node ./scripts/beautify-check",
"beautify": "node ./scripts/beautify-rewrite",
"precover": "npm run lint",
"cover": "istanbul cover -x *.runtime.js node_modules/mocha/bin/_mocha",
"publish-patch": "mocha && npm version patch && git push && git push --tags && npm publish"
Expand Down
28 changes: 28 additions & 0 deletions scripts/beautify-check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var forEachBeautifiedFile = require("./forEachBeautifiedFile");
var diff = require("diff");

function normalizeNewLines(str) {
return str.replace(/\r\n?/g, "\n");
}

var errors = 0;

forEachBeautifiedFile(function(item, callback) {
var content = normalizeNewLines(item.content);
var beautifiedContent = normalizeNewLines(item.beautifiedContent);
if(content !== beautifiedContent) {
console.log(diff.createPatch(item.file, content, beautifiedContent));
console.log();
errors++;
}
callback();
}, function(err) {
if(err) throw err;
if(errors) {
console.log(errors + " Errors.");
process.exit(1);
} else {
console.log("Fine.");
process.exit(0);
}
});
20 changes: 20 additions & 0 deletions scripts/beautify-rewrite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var forEachBeautifiedFile = require("./forEachBeautifiedFile");
var fs = require("fs");

function normalizeNewLines(str) {
return str.replace(/\r\n?/g, "\n");
}

forEachBeautifiedFile(function(item, callback) {
var content = normalizeNewLines(item.content);
var beautifiedContent = normalizeNewLines(item.beautifiedContent);
if(content !== beautifiedContent) {
console.log("- " + item.file);
fs.writeFile(item.path, beautifiedContent, "utf-8", callback);
} else {
callback();
}
}, function(err) {
if(err) throw err;
console.log("Done.");
});
3 changes: 3 additions & 0 deletions scripts/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports.beautify = {
files: "lib/**/*.js"
};
34 changes: 34 additions & 0 deletions scripts/forEachBeautifiedFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
var beautify = require("js-beautify").js_beautify;
var fs = require("fs");
var path = require("path");
var glob = require("glob");
var async = require("async");
var config = require("./config").beautify;

var options = JSON.parse(fs.readFileSync(path.resolve(__dirname, "..", ".jsbeautifyrc"), "utf-8")).js;

module.exports = function forEachBeautifiedFile(fn, callback) {

glob(config.files, {
cwd: path.resolve(__dirname, "..")
}, function(err, files) {
if(err) return callback(err);
async.eachLimit(files, 50, function(file, callback) {
var absPath = path.resolve(__dirname, "..", file);
fs.readFile(absPath, "utf-8", function(err, content) {
if(err) return callback(err);
var beautifiedContent = beautify(content, options);
fn({
file: file,
path: absPath,
content: content,
beautifiedContent: beautifiedContent
}, callback);
});
}, function(err) {
if(err) return callback(err);
callback();
});
})

};

0 comments on commit e68108a

Please sign in to comment.