better beautify scripts

This commit is contained in:
Tobias Koppers 2015-07-16 00:18:04 +02:00
parent 232618e489
commit e68108a432
6 changed files with 92 additions and 4 deletions

View File

@ -1,4 +1,4 @@
{
{
"js": {
"allowed_file_extensions": ["js", "json", "jshintrc", "jsbeautifyrc"],
"brace_style": "collapse",
@ -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,

View File

@ -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",
@ -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"

28
scripts/beautify-check.js Normal file
View File

@ -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);
}
});

View File

@ -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
scripts/config.js Normal file
View File

@ -0,0 +1,3 @@
exports.beautify = {
files: "lib/**/*.js"
};

View File

@ -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();
});
})
};