Closes #21. Parsing .jsbeautifyrc falls back to YAML, when JSON fails.

This commit is contained in:
Glavin Wiechert 2014-06-14 20:31:37 -03:00
parent 66933d9a69
commit 78e0382c15
5 changed files with 114 additions and 61 deletions

View File

@ -1,31 +1,32 @@
{
"html": {
"brace_style": "collapse",
"indent_char": " ",
"indent_scripts": "normal",
"indent_size": 6,
"max_preserve_newlines": 1,
"preserve_newlines": true,
"unformatted": ["a", "sub", "sup", "b", "i", "u"],
"wrap_line_length": 0
},
"css": {
"indent_char": " ",
"indent_size": 4
},
"js": {
"indent_size": 2,
"indent_char": " ",
"indent_level": 0,
"indent_with_tabs": false,
"preserve_newlines": true,
"max_preserve_newlines": 2,
"jslint_happy": true
},
"sql": {
"indent_size": 4,
"indent_char": " ",
"indent_level": 0,
"indent_with_tabs": false
}
}
---
html:
brace_style: "collapse"
indent_char: " "
indent_scripts: "normal"
indent_size: 6
max_preserve_newlines: 1
preserve_newlines: true
unformatted:
- "a"
- "sub"
- "sup"
- "b"
- "i"
- "u"
wrap_line_length: 0
css:
indent_char: " "
indent_size: 4
js:
indent_size: 2
indent_char: " "
indent_level: 0
indent_with_tabs: false
preserve_newlines: true
max_preserve_newlines: 2
jslint_happy: true
sql:
indent_size: 4
indent_char: " "
indent_level: 0
indent_with_tabs: false

View File

@ -0,0 +1,31 @@
{
"html": {
"brace_style": "collapse",
"indent_char": " ",
"indent_scripts": "normal",
"indent_size": 6,
"max_preserve_newlines": 1,
"preserve_newlines": true,
"unformatted": ["a", "sub", "sup", "b", "i", "u"],
"wrap_line_length": 0
},
"css": {
"indent_char": " ",
"indent_size": 4
},
"js": {
"indent_size": 2,
"indent_char": " ",
"indent_level": 0,
"indent_with_tabs": false,
"preserve_newlines": true,
"max_preserve_newlines": 2,
"jslint_happy": true
},
"sql": {
"indent_size": 4,
"indent_char": " ",
"indent_level": 0,
"indent_with_tabs": false
}
}

View File

@ -2,14 +2,14 @@
<html>
<head>
<title>Test Page</title>
<title>Test Page</title>
</head>
<body>
<h1>Hello</h1>
<p>
World!
</p>
<h1>Hello</h1>
<p>
World!
</p>
</body>
</html>

View File

@ -11,6 +11,8 @@ var path = require('path');
var nopt = require('nopt');
var extend = require('extend');
var _ = require('lodash');
var strip = require('strip-json-comments');
var yaml = require('js-yaml');
// TODO: Copied from jsbeautify, please update it from time to time
var knownOpts = {
@ -180,14 +182,26 @@ function beautify() {
var externalOptions;
if (configPath) {
var strip = require('strip-json-comments');
try {
externalOptions = JSON.parse(strip(fs.readFileSync(configPath, {
encoding: 'utf8'
})));
} catch (e) {
console.log('Failed parsing config JSON at ' + configPath);
var contents = fs.readFileSync(configPath, {
encoding: 'utf8'
});
if (!contents) {
externalOptions = {};
} else {
try {
externalOptions = JSON.parse(strip(contents));
} catch (e) {
console.log('Failed parsing config as JSON: ' + configPath);
// Attempt as YAML
try {
externalOptions = yaml.safeLoad(contents);
} catch (e) {
console.log('Failed parsing config as YAML: ' + configPath);
externalOptions = {};
}
}
}
} else {
externalOptions = {};

View File

@ -17,23 +17,29 @@
"email": "pipo@senaeh.de",
"url": "https://github.com/donaldpipowitch"
},
"contributors": [{
"name": "László Károlyi",
"url": "https://github.com/karolyi"
}, {
"name": "Glavin Wiechert",
"email": "glavin.wiechert@gmail.com",
"url": "https://github.com/Glavin001"
}, {
"name": "Marco Tanzi",
"url": "https://github.com/mtanzi"
}, {
"name": "gvn lazar suntop",
"url": "https://github.com/gvn"
}, {
"name": "Vadim K.",
"url": "https://github.com/vadirn"
}],
"contributors": [
{
"name": "László Károlyi",
"url": "https://github.com/karolyi"
},
{
"name": "Glavin Wiechert",
"email": "glavin.wiechert@gmail.com",
"url": "https://github.com/Glavin001"
},
{
"name": "Marco Tanzi",
"url": "https://github.com/mtanzi"
},
{
"name": "gvn lazar suntop",
"url": "https://github.com/gvn"
},
{
"name": "Vadim K.",
"url": "https://github.com/vadirn"
}
],
"keywords": [
"atom",
"beautify",
@ -62,6 +68,7 @@
"js-beautify": "~1.5.1",
"nopt": "^3.0.0",
"lodash": "2.4.1",
"strip-json-comments": "^0.1.3"
"strip-json-comments": "^0.1.3",
"js-yaml": "^3.0.2"
}
}