editorconfig 0.12.2 → 0.13.3

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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # EditorConfig JavaScript Core
2
2
 
3
- [![Build Status](https://travis-ci.org/editorconfig/editorconfig-core-js.png?branch=master)](https://travis-ci.org/editorconfig/editorconfig-core-js)
3
+ [![Build Status](https://travis-ci.org/editorconfig/editorconfig-core-js.svg?branch=master)](https://travis-ci.org/editorconfig/editorconfig-core-js)
4
4
 
5
5
  The EditorConfig JavaScript core will provide the same functionality as the
6
6
  [EditorConfig C Core][] and [EditorConfig Python Core][].
@@ -12,31 +12,120 @@ You need [node][] to use this package.
12
12
 
13
13
  To install this package (system-wide):
14
14
 
15
- $ npm install editorconfig
15
+ ```bash
16
+ $ npm install editorconfig
17
+ ```
16
18
 
17
19
  To install the package system-wide:
18
20
 
19
- $ npm install -g editorconfig
20
-
21
+ ```bash
22
+ $ npm install -g editorconfig
23
+ ```
21
24
 
22
25
  ## Usage
23
26
 
24
- Usage as a Node library:
27
+ ### in Node.js:
28
+
29
+ #### parse(filePath[, options])
30
+
31
+ options is an object with the following defaults:
32
+
33
+ ```js
34
+ {
35
+ config: '.editorconfig',
36
+ version: pkg.version,
37
+ root: '/'
38
+ };
39
+ ```
40
+
41
+ Search for `.editorconfig` starting from the current directory to the root directory.
42
+
43
+ Example:
44
+
45
+ ```js
46
+ var editorconfig = require('editorconfig');
47
+ var path = require('path');
48
+ var filePath = path.join(__dirname, '/sample.js');
49
+ var promise = editorconfig.parse(filePath);
50
+ promise.then(function onFulfilled(result) {
51
+ console.log(result);
52
+ });
53
+
54
+ /*
55
+ {
56
+ indent_style: 'space',
57
+ indent_size: 2,
58
+ end_of_line: 'lf',
59
+ charset: 'utf-8',
60
+ trim_trailing_whitespace: true,
61
+ insert_final_newline: true,
62
+ tab_width: 2
63
+ };
64
+ */
65
+ ```
66
+
67
+ #### parseSync(filePath[, options])
68
+
69
+ Synchronous version of `editorconfig.parse()`.
70
+
71
+ #### parseString(fileContent)
72
+
73
+ The `parse()` function above uses `parseString()` under the hood. If you have your file contents
74
+ just pass it to `parseString()` and it'll return the same results as `parse()`.
25
75
 
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' }
76
+ #### parseFromFiles(filePath, configs[, options])
35
77
 
78
+ options is an object with the following defaults:
36
79
 
37
- Usage as a command line tool:
80
+ ```js
81
+ {
82
+ config: '.editorconfig',
83
+ version: pkg.version,
84
+ root: '/'
85
+ };
86
+ ```
87
+
88
+ Specify the `.editorconfig`.
89
+
90
+ Example:
38
91
 
92
+ ```js
93
+ var editorconfig = require('editorconfig');
94
+ var fs = require('fs');
95
+ var path = require('path');
96
+ var configPath = path.join(__dirname, '/.editorconfig');
97
+ var configs = [
98
+ {
99
+ name: configPath,
100
+ contents: fs.readFileSync(configPath, 'utf8')
101
+ }
102
+ ];
103
+ var filePath = path.join(__dirname, '/sample.js');
104
+ var promise = editorconfig.parseFromFiles(filePath, configs);
105
+ promise.then(function onFulfilled(result) {
106
+ console.log(result)
107
+ });
108
+
109
+ /*
110
+ {
111
+ indent_style: 'space',
112
+ indent_size: 2,
113
+ end_of_line: 'lf',
114
+ charset: 'utf-8',
115
+ trim_trailing_whitespace: true,
116
+ insert_final_newline: true,
117
+ tab_width: 2
118
+ };
119
+ */
39
120
  ```
121
+
122
+ #### parseFromFilesSync(filePath, configs[, options])
123
+
124
+ Synchronous version of `editorconfig.parseFromFiles()`.
125
+
126
+ ### in Command Line
127
+
128
+ ```bash
40
129
  $ ./bin/editorconfig
41
130
 
42
131
  Usage: editorconfig [OPTIONS] FILEPATH1 [FILEPATH2 FILEPATH3 ...]
@@ -55,23 +144,28 @@ $ ./bin/editorconfig
55
144
 
56
145
  Example:
57
146
 
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
-
147
+ ```bash
148
+ $ ./bin/editorconfig /home/zoidberg/humans/anatomy.md
149
+ charset=utf-8
150
+ insert_final_newline=true
151
+ end_of_line=lf
152
+ tab_width=8
153
+ trim_trailing_whitespace=sometimes
154
+ ```
65
155
 
66
156
  ## Development
67
157
 
68
158
  To install dependencies for this package run this in the package directory:
69
159
 
70
- $ npm install
160
+ ```bash
161
+ $ npm install
162
+ ```
71
163
 
72
164
  Next, run:
73
165
 
74
- $ npm link
166
+ ```bash
167
+ $ npm link
168
+ ```
75
169
 
76
170
  The global editorconfig will now point to the files in your development
77
171
  repository instead of a globally-installed version from npm. You can now use
@@ -83,8 +177,9 @@ link again to get the latest dependencies.
83
177
 
84
178
  To test the command line interface:
85
179
 
86
- $ editorconfig <filepath>
87
-
180
+ ```bash
181
+ $ editorconfig <filepath>
182
+ ```
88
183
 
89
184
  # Testing
90
185
 
@@ -92,11 +187,15 @@ To test the command line interface:
92
187
 
93
188
  To run the tests:
94
189
 
95
- $ npm test
190
+ ```bash
191
+ $ npm test
192
+ ```
96
193
 
97
194
  To run the tests with increased verbosity (for debugging test failures):
98
195
 
99
- $ npm run-script test-verbose
196
+ ```bash
197
+ $ npm run-script test-verbose
198
+ ```
100
199
 
101
200
  [EditorConfig C Core]: https://github.com/editorconfig/editorconfig-core
102
201
  [EditorConfig Python Core]: https://github.com/editorconfig/editorconfig-core-py
package/bin/editorconfig CHANGED
File without changes
package/editorconfig.js CHANGED
@@ -1,11 +1,13 @@
1
+ var Promise = require('bluebird');
2
+ var fs = require('fs');
1
3
  var os = require('os');
2
4
  var path = require('path');
3
- var Promise = require('bluebird');
4
- var whenReadFile = Promise.promisify(require('fs').readFile);
5
+ var semver = require('semver');
6
+ var util = require('util');
7
+ var whenReadFile = Promise.promisify(fs.readFile);
5
8
 
6
- var minimatch = require('./lib/fnmatch');
7
9
  var iniparser = require('./lib/ini');
8
- var Version = require('./lib/version');
10
+ var minimatch = require('./lib/fnmatch');
9
11
  var pkg = require('./package.json');
10
12
 
11
13
  var knownProps = [
@@ -50,7 +52,7 @@ function processMatches(matches, version) {
50
52
  // Set indent_size to "tab" if indent_size is unspecified and
51
53
  // indent_style is set to "tab".
52
54
  if ("indent_style" in matches && matches.indent_style === "tab" &&
53
- !("indent_size" in matches) && version.gte(new Version(0, 10))) {
55
+ !("indent_size" in matches) && semver.gte(version, "0.10.0")) {
54
56
  matches.indent_size = "tab";
55
57
  }
56
58
 
@@ -72,11 +74,39 @@ function processOptions(options, filepath) {
72
74
  options = options || {};
73
75
  return {
74
76
  config: options.config || '.editorconfig',
75
- version: new Version(options.version || pkg.version),
77
+ version: options.version || pkg.version,
76
78
  root: path.resolve(options.root || getFilepathRoot(filepath))
77
79
  };
78
80
  }
79
81
 
82
+ function buildFullGlob(pathPrefix, glob) {
83
+ switch (glob.indexOf('/')) {
84
+ case -1: glob = "**/" + glob; break;
85
+ case 0: glob = glob.substring(1); break;
86
+ }
87
+ return path.join(pathPrefix, glob);
88
+ }
89
+
90
+ function extendProps(props, options) {
91
+ for (var key in options) {
92
+ var value = options[key];
93
+ key = key.toLowerCase();
94
+ if (knownProps[key]) {
95
+ value = value.toLowerCase();
96
+ }
97
+ try {
98
+ value = JSON.parse(value);
99
+ } catch(e) {}
100
+ if (typeof value === 'undefined' || value === null) {
101
+ // null and undefined are values specific to JSON (no special meaning
102
+ // in editorconfig) & should just be returned as regular strings.
103
+ value = String(value);
104
+ }
105
+ props[key] = value;
106
+ }
107
+ return props;
108
+ }
109
+
80
110
  function parseFromFiles(filepath, files, options) {
81
111
  return getConfigsForFiles(files).then(function (configs) {
82
112
  return configs.reverse();
@@ -85,28 +115,9 @@ function parseFromFiles(filepath, files, options) {
85
115
  file.contents.forEach(function (section) {
86
116
  var glob = section[0], options = section[1];
87
117
  if (!glob) return;
88
- switch (glob.indexOf('/')) {
89
- case -1: glob = "**/" + glob; break;
90
- case 0: glob = glob.substring(1); break;
91
- }
92
- var fullGlob = path.join(pathPrefix, glob);
118
+ var fullGlob = buildFullGlob(pathPrefix, glob);
93
119
  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);
107
- }
108
- matches[key] = value;
109
- }
120
+ matches = extendProps(matches, options);
110
121
  });
111
122
  return matches;
112
123
  }, {}).then(function (matches) {
@@ -114,6 +125,23 @@ function parseFromFiles(filepath, files, options) {
114
125
  });
115
126
  }
116
127
 
128
+ function parseFromFilesSync(filepath, files, options) {
129
+ var configs = getConfigsForFilesSync(files);
130
+ configs.reverse();
131
+ var matches = {};
132
+ configs.forEach(function(config) {
133
+ var pathPrefix = path.dirname(config.name);
134
+ config.contents.forEach(function(section) {
135
+ var glob = section[0], options = section[1];
136
+ if (!glob) return;
137
+ var fullGlob = buildFullGlob(pathPrefix, glob);
138
+ if (!fnmatch(filepath, fullGlob)) return;
139
+ matches = extendProps(matches, options);
140
+ });
141
+ });
142
+ return processMatches(matches, options.version);
143
+ }
144
+
117
145
  function StopReduce(array) {
118
146
  this.array = array;
119
147
  }
@@ -136,6 +164,22 @@ function getConfigsForFiles(files) {
136
164
  });
137
165
  }
138
166
 
167
+ function getConfigsForFilesSync(files) {
168
+ var configs = [];
169
+ for (var i in files) {
170
+ var file = files[i];
171
+ var contents = iniparser.parseString(file.contents);
172
+ configs.push({
173
+ name: file.name,
174
+ contents: contents
175
+ });
176
+ if ((contents[0][1].root || '').toLowerCase() === 'true') {
177
+ break;
178
+ }
179
+ };
180
+ return configs;
181
+ }
182
+
139
183
  function readConfigFiles(filepaths) {
140
184
  return Promise.map(filepaths, function (path) {
141
185
  return whenReadFile(path, 'utf-8').catch(function () {
@@ -146,14 +190,34 @@ function readConfigFiles(filepaths) {
146
190
  });
147
191
  }
148
192
 
193
+ function readConfigFilesSync(filepaths) {
194
+ var files = [];
195
+ var file;
196
+ filepaths.forEach(function(filepath) {
197
+ try {
198
+ file = fs.readFileSync(filepath, 'utf8');
199
+ } catch (e) {
200
+ file = '';
201
+ }
202
+ files.push({name: filepath, contents: file});
203
+ });
204
+ return files;
205
+ }
206
+
149
207
  module.exports.parseFromFiles = function (filepath, files, options) {
150
208
  return new Promise (function (resolve, reject) {
151
209
  filepath = path.resolve(filepath);
152
210
  options = processOptions(options, filepath);
153
- resolve(parseFromFiles(filepath, files, option));
211
+ resolve(parseFromFiles(filepath, files, options));
154
212
  });
155
213
  };
156
214
 
215
+ module.exports.parseFromFilesSync = function (filepath, files, options) {
216
+ filepath = path.resolve(filepath);
217
+ options = processOptions(options, filepath);
218
+ return parseFromFilesSync(filepath, files, options);
219
+ };
220
+
157
221
  module.exports.parse = function (filepath, options) {
158
222
  return new Promise (function (resolve, reject) {
159
223
  filepath = path.resolve(filepath);
@@ -163,3 +227,11 @@ module.exports.parse = function (filepath, options) {
163
227
  resolve(parseFromFiles(filepath, files, options));
164
228
  });
165
229
  };
230
+
231
+ module.exports.parseSync = function (filepath, options) {
232
+ filepath = path.resolve(filepath);
233
+ options = processOptions(options, filepath);
234
+ var filepaths = getConfigFileNames(filepath, options);
235
+ var files = readConfigFilesSync(filepaths);
236
+ return parseFromFilesSync(filepath, files, options);
237
+ };
package/lib/ini.js CHANGED
@@ -23,7 +23,7 @@ var regex = {
23
23
  * @param: {Function} callback, the function that will be called when parsing is done
24
24
  * @return: none
25
25
  */
26
- module.exports.parse = function (file, callback) {
26
+ exports.parse = function (file, callback) {
27
27
  if (!callback) {
28
28
  return;
29
29
  }
@@ -36,11 +36,11 @@ module.exports.parse = function (file, callback) {
36
36
  });
37
37
  };
38
38
 
39
- module.exports.parseSync = function (file) {
39
+ exports.parseSync = function (file) {
40
40
  return parse(fs.readFileSync(file, 'utf8'));
41
41
  };
42
42
 
43
- function parse (data) {
43
+ exports.parseString = function (data) {
44
44
  var sectionBody = {};
45
45
  var sectionName = null;
46
46
  var value = [[sectionName, sectionBody]];
@@ -60,6 +60,4 @@ function parse (data) {
60
60
  }
61
61
  });
62
62
  return value;
63
- }
64
-
65
- module.exports.parseString = parse;
63
+ };
package/package.json CHANGED
@@ -1,15 +1,18 @@
1
1
  {
2
2
  "name": "editorconfig",
3
- "version": "0.12.2",
3
+ "version": "0.13.3",
4
4
  "description": "EditorConfig File Locator and Interpreter for Node.js",
5
5
  "keywords": [
6
6
  "editorconfig",
7
7
  "core"
8
8
  ],
9
9
  "main": "editorconfig.js",
10
+ "bin": {
11
+ "editorconfig": "bin/editorconfig"
12
+ },
10
13
  "contributors": [
11
14
  "Hong Xu (topbug.net)",
12
- "Jed Hunsaker <jed.hunsaker[at]gmail.com>",
15
+ "Jed Mao (https://github.com/jedmao/)",
13
16
  "Trey Hunner (http://treyhunner.com)"
14
17
  ],
15
18
  "directories": {
@@ -18,9 +21,9 @@
18
21
  },
19
22
  "scripts": {
20
23
  "pretest": "cmake .",
21
- "test": "ctest .",
22
- "test-verbose": "ctest -VV --output-on-failure .",
23
- "codepaint": "codepainter xform -e **/**.js"
24
+ "test": "npm run lint && ctest .",
25
+ "test-verbose": "npm run lint && ctest -VV --output-on-failure .",
26
+ "lint": "eclint check --indent_size ignore editorconfig.js README.md \"bin/**\" \"lib/**\""
24
27
  },
25
28
  "repository": {
26
29
  "type": "git",
@@ -28,17 +31,17 @@
28
31
  },
29
32
  "bugs": "https://github.com/editorconfig/editorconfig-core-js/issues",
30
33
  "author": "EditorConfig Team",
31
- "license": {
32
- "type": "MIT",
33
- "url": "http://editorconfig.mit-license.org/2012"
34
- },
34
+ "license": "MIT",
35
35
  "dependencies": {
36
- "bluebird": "^2.3.6",
37
- "commander": "~1.1.1",
38
- "lru-cache": "~2.0.0",
39
- "sigmund": "~1.0.0"
36
+ "bluebird": "^3.0.5",
37
+ "commander": "^2.9.0",
38
+ "lru-cache": "^3.2.0",
39
+ "semver": "^5.1.0",
40
+ "sigmund": "^1.0.1"
40
41
  },
41
42
  "devDependencies": {
42
- "codepainter": "^0.4.4"
43
+ "eclint": "^1.1.5",
44
+ "mocha": "^2.3.4",
45
+ "should": "^7.1.1"
43
46
  }
44
47
  }
package/lib/version.js DELETED
@@ -1,48 +0,0 @@
1
- function Version(version) {
2
- var args = arguments;
3
- this.components = typeof version === "string" ?
4
- version.split(".").map(function(x){return parseInt(x, 10);}) :
5
- Object.keys(arguments).map(function(k){return args[k];});
6
-
7
- var len = this.components.length;
8
- this.major = len ? this.components[0] : 0;
9
- this.minor = len > 1 ? this.components[1] : 0;
10
- this.build = len > 2 ? this.components[2] : 0;
11
- this.revision = len > 3 ? this.components[3] : 0;
12
-
13
- if (typeof version !== "string") {
14
- return;
15
- }
16
-
17
- var ext = version.split("-");
18
- if (ext.length === 2) {
19
- this.configuration = ext[1];
20
- }
21
- }
22
-
23
- Version.prototype = {
24
- toString: function() {
25
- var version = this.components.join(".");
26
- if (typeof this.configuration !== "undefined") {
27
- version += "-" + this.configuration;
28
- }
29
- return version;
30
- },
31
- gte: function(other){
32
- if (this.major < other.major) {
33
- return false;
34
- }
35
- if (this.minor < other.minor) {
36
- return false;
37
- }
38
- if (this.build < other.build) {
39
- return false;
40
- }
41
- if (this.revision < other.revision) {
42
- return false;
43
- }
44
- return true;
45
- }
46
- };
47
-
48
- module.exports = Version;