editorconfig 0.11.3 → 0.12.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.gitmodules CHANGED
@@ -1,3 +1,3 @@
1
- [submodule "tests"]
2
- path = tests
3
- url = git://github.com/editorconfig/editorconfig-core-test.git
1
+ [submodule "tests"]
2
+ path = tests
3
+ url = git://github.com/editorconfig/editorconfig-core-test.git
package/LICENSE CHANGED
@@ -1,19 +1,19 @@
1
- Copyright © 2012 EditorConfig Team
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining a copy
4
- of this software and associated documentation files (the “Software”), to deal
5
- in the Software without restriction, including without limitation the rights
6
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- copies of the Software, and to permit persons to whom the Software is
8
- furnished to do so, subject to the following conditions:
9
-
10
- The above copyright notice and this permission notice shall be included in
11
- all copies or substantial portions of the Software.
12
-
13
- THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
- THE SOFTWARE.
1
+ Copyright © 2012 EditorConfig Team
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the “Software”), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
package/README.md CHANGED
@@ -1,84 +1,104 @@
1
- # EditorConfig JavaScript Core
2
-
3
- [![Build Status](https://travis-ci.org/editorconfig/editorconfig-core-js.png?branch=master)](https://travis-ci.org/editorconfig/editorconfig-core-js)
4
-
5
- The EditorConfig JavaScript core will provide the same functionality as the
6
- [EditorConfig C Core][] and [EditorConfig Python Core][].
7
-
8
- ## Installation
9
-
10
- You need [node][] to use this package.
11
-
12
- To install this package (system-wide):
13
-
14
- sudo npm install -g .
15
-
16
- ## Usage
17
-
18
- Usage as a Node library:
19
-
20
- $ node
21
- > var editorconfig = require('./editorconfig');
22
- undefined
23
- > editorconfig.parse('/home/zoidberg/humans/anatomy.md');
24
- { charset: 'utf-8',
25
- insert_final_newline: 'true',
26
- end_of_line: 'lf',
27
- tab_width: '8',
28
- trim_trailing_whitespace: 'sometimes' }
29
-
30
-
31
- Usage as a command line tool:
32
-
33
- ```
34
- $ ./bin/editorconfig
35
-
36
- Usage: editorconfig [OPTIONS] FILEPATH1 [FILEPATH2 FILEPATH3 ...]
37
-
38
- EditorConfig Node.js Core Version 0.11.4-development
39
-
40
- FILEPATH can be a hyphen (-) if you want path(s) to be read from stdin.
41
-
42
- Options:
43
-
44
- -h, --help output usage information
45
- -V, --version output the version number
46
- -f <path> Specify conf filename other than ".editorconfig"
47
- -b <version> Specify version (used by devs to test compatibility)
48
- ```
49
-
50
- Example:
51
-
52
- $ ./bin/editorconfig /home/zoidberg/humans/anatomy.md
53
- charset=utf-8
54
- insert_final_newline=true
55
- end_of_line=lf
56
- tab_width=8
57
- trim_trailing_whitespace=sometimes
58
-
59
- ## Development
60
-
61
- To install dependencies for this package run this in the package directory:
62
-
63
- npm install
64
-
65
- To test the command line interface:
66
-
67
- ./bin/editorconfig <filepath>
68
-
69
- # Testing
70
-
71
- [CMake][] must be installed to run the tests.
72
-
73
- To run the tests:
74
-
75
- npm test
76
-
77
- To run the tests with increased verbosity (for debugging test failures):
78
-
79
- ctest -VV --output-on-failure .
80
-
81
- [EditorConfig C Core]: https://github.com/editorconfig/editorconfig-core
82
- [EditorConfig Python Core]: https://github.com/editorconfig/editorconfig-core-py
83
- [node]: http://nodejs.org/
84
- [cmake]: http://www.cmake.org
1
+ # EditorConfig JavaScript Core
2
+
3
+ [![Build Status](https://travis-ci.org/editorconfig/editorconfig-core-js.png?branch=master)](https://travis-ci.org/editorconfig/editorconfig-core-js)
4
+
5
+ The EditorConfig JavaScript core will provide the same functionality as the
6
+ [EditorConfig C Core][] and [EditorConfig Python Core][].
7
+
8
+
9
+ ## Installation
10
+
11
+ You need [node][] to use this package.
12
+
13
+ To install this package (system-wide):
14
+
15
+ $ npm install editorconfig
16
+
17
+ To install the package system-wide:
18
+
19
+ $ npm install -g editorconfig
20
+
21
+
22
+ ## Usage
23
+
24
+ Usage as a Node library:
25
+
26
+ $ node
27
+ > var editorconfig = require('./editorconfig');
28
+ undefined
29
+ > editorconfig.parse('/home/zoidberg/humans/anatomy.md');
30
+ { charset: 'utf-8',
31
+ insert_final_newline: 'true',
32
+ end_of_line: 'lf',
33
+ tab_width: '8',
34
+ trim_trailing_whitespace: 'sometimes' }
35
+
36
+
37
+ Usage as a command line tool:
38
+
39
+ ```
40
+ $ ./bin/editorconfig
41
+
42
+ Usage: editorconfig [OPTIONS] FILEPATH1 [FILEPATH2 FILEPATH3 ...]
43
+
44
+ EditorConfig Node.js Core Version 0.11.4-development
45
+
46
+ FILEPATH can be a hyphen (-) if you want path(s) to be read from stdin.
47
+
48
+ Options:
49
+
50
+ -h, --help output usage information
51
+ -V, --version output the version number
52
+ -f <path> Specify conf filename other than ".editorconfig"
53
+ -b <version> Specify version (used by devs to test compatibility)
54
+ ```
55
+
56
+ Example:
57
+
58
+ $ ./bin/editorconfig /home/zoidberg/humans/anatomy.md
59
+ charset=utf-8
60
+ insert_final_newline=true
61
+ end_of_line=lf
62
+ tab_width=8
63
+ trim_trailing_whitespace=sometimes
64
+
65
+
66
+ ## Development
67
+
68
+ To install dependencies for this package run this in the package directory:
69
+
70
+ $ npm install
71
+
72
+ Next, run:
73
+
74
+ $ npm link
75
+
76
+ The global editorconfig will now point to the files in your development
77
+ repository instead of a globally-installed version from npm. You can now use
78
+ editorconfig directly to test your changes.
79
+
80
+ If you ever update from the central repository and there are errors, it might
81
+ be because you are missing some dependencies. If that happens, just run npm
82
+ link again to get the latest dependencies.
83
+
84
+ To test the command line interface:
85
+
86
+ $ editorconfig <filepath>
87
+
88
+
89
+ # Testing
90
+
91
+ [CMake][] must be installed to run the tests.
92
+
93
+ To run the tests:
94
+
95
+ $ npm test
96
+
97
+ To run the tests with increased verbosity (for debugging test failures):
98
+
99
+ $ npm run-script test-verbose
100
+
101
+ [EditorConfig C Core]: https://github.com/editorconfig/editorconfig-core
102
+ [EditorConfig Python Core]: https://github.com/editorconfig/editorconfig-core-py
103
+ [node]: http://nodejs.org/
104
+ [cmake]: http://www.cmake.org
package/bin/editorconfig CHANGED
@@ -1,39 +1,42 @@
1
- #!/usr/bin/env node
2
-
3
- var path = require("path");
4
- var program = require("commander");
5
-
6
- var editorconfig = require("../editorconfig");
7
- var package = require("../package.json");
8
-
9
-
10
- program.version("EditorConfig Node.js Core Version " + package.version);
11
-
12
- program
13
- .usage([
14
- "[OPTIONS] FILEPATH1 [FILEPATH2 FILEPATH3 ...]",
15
- program._version,
16
- "FILEPATH can be a hyphen (-) if you want path(s) to be read from stdin."
17
- ].join("\n\n "))
18
- .option("-f <path>", "Specify conf filename other than \".editorconfig\"")
19
- .option("-b <version>", "Specify version (used by devs to test compatibility)")
20
- .option("-v, --version", "Display version information")
21
- .parse(process.argv);
22
-
23
- // Throw away the native -V flag in lieu of the one we've manually specified
24
- // to adhere to testing requirements
25
- program.options.shift();
26
-
27
- if ( ! program.args.length) {
28
- program.help();
29
- }
30
-
31
- program.args.forEach(function(filePath) {
32
- var parsed = editorconfig.parse(filePath, {config: program.F, version: program.B});
33
- if (program.args.length > 1) {
34
- console.log("[%s]", filePath);
35
- }
36
- Object.keys(parsed).forEach(function(key) {
37
- console.log(key + "=" + parsed[key]);
38
- });
39
- });
1
+ #!/usr/bin/env node
2
+
3
+ var path = require("path");
4
+ var program = require("commander");
5
+ var Promise = require("bluebird");
6
+
7
+ var editorconfig = require("../editorconfig");
8
+ var package = require("../package.json");
9
+
10
+ program.version("EditorConfig Node.js Core Version " + package.version);
11
+
12
+ program
13
+ .usage([
14
+ "[OPTIONS] FILEPATH1 [FILEPATH2 FILEPATH3 ...]",
15
+ program._version,
16
+ "FILEPATH can be a hyphen (-) if you want path(s) to be read from stdin."
17
+ ].join("\n\n "))
18
+ .option("-f <path>", "Specify conf filename other than \".editorconfig\"")
19
+ .option("-b <version>", "Specify version (used by devs to test compatibility)")
20
+ .option("-v, --version", "Display version information")
21
+ .parse(process.argv);
22
+
23
+ // Throw away the native -V flag in lieu of the one we've manually specified
24
+ // to adhere to testing requirements
25
+ program.options.shift();
26
+
27
+ var files = program.args;
28
+
29
+ if (!files.length) {
30
+ program.help();
31
+ }
32
+
33
+ Promise.map(files, function(filePath) {
34
+ return editorconfig.parse(filePath, {config: program.F, version: program.B});
35
+ }).each(function(parsed, i, length) {
36
+ if (length > 1) {
37
+ console.log("[%s]", files[i]);
38
+ }
39
+ Object.keys(parsed).forEach(function(key) {
40
+ console.log(key + "=" + parsed[key]);
41
+ });
42
+ });
package/editorconfig.js CHANGED
@@ -1,15 +1,24 @@
1
- var fs = require('fs');
1
+ var os = require('os');
2
2
  var path = require('path');
3
+ var Promise = require('bluebird');
4
+ var whenReadFile = Promise.promisify(require('fs').readFile);
3
5
 
4
6
  var minimatch = require('./lib/fnmatch');
5
7
  var iniparser = require('./lib/ini');
6
8
  var Version = require('./lib/version');
7
9
  var pkg = require('./package.json');
8
10
 
9
-
10
- var knownProps = ['end_of_line', 'indent_style', 'indent_size',
11
- 'insert_final_newline', 'trim_trailing_whitespace', 'charset'];
12
-
11
+ var knownProps = [
12
+ 'end_of_line',
13
+ 'indent_style',
14
+ 'indent_size',
15
+ 'insert_final_newline',
16
+ 'trim_trailing_whitespace',
17
+ 'charset'
18
+ ].reduce(function (set, prop) {
19
+ set[prop] = true;
20
+ return set;
21
+ }, {});
13
22
 
14
23
  function fnmatch(filepath, glob) {
15
24
  var matchOptions = {matchBase: true, dot: true, noext: true};
@@ -17,21 +26,27 @@ function fnmatch(filepath, glob) {
17
26
  return minimatch(filepath, glob, matchOptions);
18
27
  }
19
28
 
20
-
21
- function getConfigFileNames(filepath, configname) {
22
- var old_dirname = filepath;
23
- var dirname = old_dirname;
29
+ function getConfigFileNames(filepath, options) {
24
30
  var paths = [];
25
31
  do {
26
- paths.push(path.join(dirname, configname || ".editorconfig"));
27
- old_dirname = dirname;
28
- dirname = path.dirname(old_dirname);
29
- } while(dirname != old_dirname);
32
+ filepath = path.dirname(filepath);
33
+ paths.push(path.join(filepath, options.config));
34
+ } while (filepath !== options.root);
30
35
  return paths;
31
36
  }
32
37
 
33
- function processMatches(matches, version) {
38
+ function getFilepathRoot(filepath) {
39
+ if (path.parse !== undefined) {
40
+ // Node.js >= 0.11.15
41
+ return path.parse(filepath).root;
42
+ }
43
+ if (os.platform() === 'win32') {
44
+ return path.resolve(filepath).match(/^(\\\\[^\\]+\\)?[^\\]+\\/)[0];
45
+ }
46
+ return '/';
47
+ }
34
48
 
49
+ function processMatches(matches, version) {
35
50
  // Set indent_size to "tab" if indent_size is unspecified and
36
51
  // indent_style is set to "tab".
37
52
  if ("indent_style" in matches && matches.indent_style === "tab" &&
@@ -42,99 +57,109 @@ function processMatches(matches, version) {
42
57
  // Set tab_width to indent_size if indent_size is specified and
43
58
  // tab_width is unspecified
44
59
  if ("indent_size" in matches && !("tab_width" in matches) &&
45
- matches.indent_size !== "tab")
60
+ matches.indent_size !== "tab")
46
61
  matches.tab_width = matches.indent_size;
47
62
 
48
63
  // Set indent_size to tab_width if indent_size is "tab"
49
64
  if("indent_size" in matches && "tab_width" in matches &&
50
- matches.indent_size === "tab")
65
+ matches.indent_size === "tab")
51
66
  matches.indent_size = matches.tab_width;
52
67
 
53
68
  return matches;
54
69
  }
55
70
 
56
-
57
- function processOptions(options) {
71
+ function processOptions(options, filepath) {
58
72
  options = options || {};
59
- options.version = new Version(options.version || pkg.version);
60
- return options;
73
+ return {
74
+ config: options.config || '.editorconfig',
75
+ version: new Version(options.version || pkg.version),
76
+ root: path.resolve(options.root || getFilepathRoot(filepath))
77
+ };
61
78
  }
62
79
 
63
-
64
- function parseFromFiles(filepath, configs, options) {
65
- var matches = {};
66
-
67
- configs.reverse().forEach(function (file) {
80
+ function parseFromFiles(filepath, files, options) {
81
+ return getConfigsForFiles(files).then(function (configs) {
82
+ return configs.reverse();
83
+ }).reduce(function (matches, file) {
68
84
  var pathPrefix = path.dirname(file.name);
69
- var config = file.contents;
70
- config.forEach(function (section) {
71
- var fullGlob;
72
- var glob = section[0];
73
- var options = section[1];
85
+ file.contents.forEach(function (section) {
86
+ var glob = section[0], options = section[1];
74
87
  if (!glob) return;
75
- if (glob.indexOf('/') === -1) {
76
- fullGlob = path.join(pathPrefix, "**/" + glob);
77
- } else if (glob.indexOf('/') === 0) {
78
- fullGlob = path.join(pathPrefix, glob.substring(1));
79
- } else {
80
- fullGlob = path.join(pathPrefix, glob);
88
+ switch (glob.indexOf('/')) {
89
+ case -1: glob = "**/" + glob; break;
90
+ case 0: glob = glob.substring(1); break;
81
91
  }
82
- if (fnmatch(filepath, fullGlob)) {
83
- for (var key in options) {
84
- var value = options[key];
85
- if (knownProps.indexOf(key) !== -1) {
86
- value = value.toLowerCase();
87
- }
88
- try {
89
- value = JSON.parse(value);
90
- } catch(e){}
91
- matches[key.toLowerCase()] = value;
92
+ var fullGlob = path.join(pathPrefix, glob);
93
+ if (!fnmatch(filepath, fullGlob)) return;
94
+ for (var key in options) {
95
+ var value = options[key];
96
+ key = key.toLowerCase();
97
+ if (knownProps[key]) {
98
+ value = value.toLowerCase();
99
+ }
100
+ try {
101
+ value = JSON.parse(value);
102
+ } catch(e) {}
103
+ if (typeof value === 'undefined' || value === null) {
104
+ // null and undefined are values specific to JSON (no special meaning
105
+ // in editorconfig) & should just be returned as regular strings.
106
+ value = String(value);
92
107
  }
108
+ matches[key] = value;
93
109
  }
94
110
  });
111
+ return matches;
112
+ }, {}).then(function (matches) {
113
+ return processMatches(matches, options.version);
95
114
  });
115
+ }
96
116
 
97
- return processMatches(matches, options.version);
98
-
117
+ function StopReduce(array) {
118
+ this.array = array;
99
119
  }
100
120
 
121
+ StopReduce.prototype = Object.create(Error.prototype);
101
122
 
102
123
  function getConfigsForFiles(files) {
103
- var configs = [];
104
- for (var i = 0; i < files.length; i++) {
105
- files[i].contents = iniparser.parseString(files[i].contents);
106
- configs.push(files[i]);
107
- if (/^true$/i.test(files[i].contents[0][1].root)) break;
108
- }
109
- return configs;
124
+ return Promise.reduce(files, function (configs, file) {
125
+ var contents = iniparser.parseString(file.contents);
126
+ configs.push({
127
+ name: file.name,
128
+ contents: contents
129
+ });
130
+ if ((contents[0][1].root || '').toLowerCase() === 'true') {
131
+ return Promise.reject(new StopReduce(configs));
132
+ }
133
+ return configs;
134
+ }, []).catch(StopReduce, function (stop) {
135
+ return stop.array;
136
+ });
110
137
  }
111
138
 
112
-
113
139
  function readConfigFiles(filepaths) {
114
- var files = [];
115
- filepaths.forEach(function (configFilePath) {
116
- if (fs.existsSync(configFilePath)) {
117
- files.push({
118
- name: configFilePath,
119
- contents: fs.readFileSync(configFilePath, 'utf-8')
120
- });
121
- }
140
+ return Promise.map(filepaths, function (path) {
141
+ return whenReadFile(path, 'utf-8').catch(function () {
142
+ return '';
143
+ }).then(function (contents) {
144
+ return {name: path, contents: contents};
145
+ });
122
146
  });
123
- return files;
124
147
  }
125
148
 
126
-
127
- module.exports.parseFromFiles = function(filepath, files, options) {
128
- filepath = path.resolve(filepath);
129
- options = processOptions(options);
130
- return parseFromFiles(filepath, getConfigsForFiles(files), options);
149
+ module.exports.parseFromFiles = function (filepath, files, options) {
150
+ return new Promise (function (resolve, reject) {
151
+ filepath = path.resolve(filepath);
152
+ options = processOptions(options, filepath);
153
+ resolve(parseFromFiles(filepath, files, option));
154
+ });
131
155
  };
132
156
 
133
-
134
- module.exports.parse = function(filepath, options) {
135
- filepath = path.resolve(filepath);
136
- options = processOptions(options);
137
- var filepaths = getConfigFileNames(path.dirname(filepath), options.config);
138
- var files = readConfigFiles(filepaths);
139
- return parseFromFiles(filepath, getConfigsForFiles(files), options);
157
+ module.exports.parse = function (filepath, options) {
158
+ return new Promise (function (resolve, reject) {
159
+ filepath = path.resolve(filepath);
160
+ options = processOptions(options, filepath);
161
+ var filepaths = getConfigFileNames(filepath, options);
162
+ var files = readConfigFiles(filepaths);
163
+ resolve(parseFromFiles(filepath, files, options));
164
+ });
140
165
  };