Skip to content

Commit e68108a

Browse files
committed
better beautify scripts
1 parent 232618e commit e68108a

File tree

6 files changed

+92
-4
lines changed

6 files changed

+92
-4
lines changed

.jsbeautifyrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{
1+
{
22
"js": {
33
"allowed_file_extensions": ["js", "json", "jshintrc", "jsbeautifyrc"],
44
"brace_style": "collapse",
@@ -9,7 +9,7 @@
99
"indent_char": "\t",
1010
"indent_level": 0,
1111
"indent_size": 1,
12-
"indent_with_tabs": false,
12+
"indent_with_tabs": true,
1313
"jslint_happy": false,
1414
"jslint_happy_align_switch_case": true,
1515
"space_after_anon_function": false,

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@
2828
"component-webpack-plugin": "~0.2.0",
2929
"coveralls": "^2.11.2",
3030
"css-loader": "~0.14.0",
31+
"diff": "^1.4.0",
3132
"eslint": "^0.24.0",
3233
"eslint-plugin-nodeca": "^1.0.3",
3334
"express": "~3.4.8",
3435
"extract-text-webpack-plugin": "~0.8.0",
3536
"file-loader": "~0.8.0",
37+
"glob": "^5.0.14",
3638
"i18n-webpack-plugin": "~0.2.0",
3739
"istanbul": "^0.3.13",
3840
"jade-loader": "~0.7.0",
@@ -71,11 +73,12 @@
7173
"web_modules/"
7274
],
7375
"scripts": {
74-
"pretest": "npm run lint",
76+
"pretest": "npm run lint && npm run beautify-lint",
7577
"test": "mocha",
7678
"travis": "npm run cover -- --report lcovonly",
7779
"lint": "eslint lib",
78-
"jsbeautify": "js-beautify --indent-with-tabs --end-with-newline lib/**/*.js lib/*.js -r",
80+
"beautify-lint": "node ./scripts/beautify-check",
81+
"beautify": "node ./scripts/beautify-rewrite",
7982
"precover": "npm run lint",
8083
"cover": "istanbul cover -x *.runtime.js node_modules/mocha/bin/_mocha",
8184
"publish-patch": "mocha && npm version patch && git push && git push --tags && npm publish"

scripts/beautify-check.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var forEachBeautifiedFile = require("./forEachBeautifiedFile");
2+
var diff = require("diff");
3+
4+
function normalizeNewLines(str) {
5+
return str.replace(/\r\n?/g, "\n");
6+
}
7+
8+
var errors = 0;
9+
10+
forEachBeautifiedFile(function(item, callback) {
11+
var content = normalizeNewLines(item.content);
12+
var beautifiedContent = normalizeNewLines(item.beautifiedContent);
13+
if(content !== beautifiedContent) {
14+
console.log(diff.createPatch(item.file, content, beautifiedContent));
15+
console.log();
16+
errors++;
17+
}
18+
callback();
19+
}, function(err) {
20+
if(err) throw err;
21+
if(errors) {
22+
console.log(errors + " Errors.");
23+
process.exit(1);
24+
} else {
25+
console.log("Fine.");
26+
process.exit(0);
27+
}
28+
});

scripts/beautify-rewrite.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var forEachBeautifiedFile = require("./forEachBeautifiedFile");
2+
var fs = require("fs");
3+
4+
function normalizeNewLines(str) {
5+
return str.replace(/\r\n?/g, "\n");
6+
}
7+
8+
forEachBeautifiedFile(function(item, callback) {
9+
var content = normalizeNewLines(item.content);
10+
var beautifiedContent = normalizeNewLines(item.beautifiedContent);
11+
if(content !== beautifiedContent) {
12+
console.log("- " + item.file);
13+
fs.writeFile(item.path, beautifiedContent, "utf-8", callback);
14+
} else {
15+
callback();
16+
}
17+
}, function(err) {
18+
if(err) throw err;
19+
console.log("Done.");
20+
});

scripts/config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
exports.beautify = {
2+
files: "lib/**/*.js"
3+
};

scripts/forEachBeautifiedFile.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var beautify = require("js-beautify").js_beautify;
2+
var fs = require("fs");
3+
var path = require("path");
4+
var glob = require("glob");
5+
var async = require("async");
6+
var config = require("./config").beautify;
7+
8+
var options = JSON.parse(fs.readFileSync(path.resolve(__dirname, "..", ".jsbeautifyrc"), "utf-8")).js;
9+
10+
module.exports = function forEachBeautifiedFile(fn, callback) {
11+
12+
glob(config.files, {
13+
cwd: path.resolve(__dirname, "..")
14+
}, function(err, files) {
15+
if(err) return callback(err);
16+
async.eachLimit(files, 50, function(file, callback) {
17+
var absPath = path.resolve(__dirname, "..", file);
18+
fs.readFile(absPath, "utf-8", function(err, content) {
19+
if(err) return callback(err);
20+
var beautifiedContent = beautify(content, options);
21+
fn({
22+
file: file,
23+
path: absPath,
24+
content: content,
25+
beautifiedContent: beautifiedContent
26+
}, callback);
27+
});
28+
}, function(err) {
29+
if(err) return callback(err);
30+
callback();
31+
});
32+
})
33+
34+
};

0 commit comments

Comments
 (0)