1 | # EditorConfig JavaScript Core
|
2 |
|
3 | [![Build Status](https://travis-ci.org/editorconfig/editorconfig-core-js.svg?branch=master)](https://travis-ci.org/editorconfig/editorconfig-core-js)
|
4 | [![dependencies Status](https://david-dm.org/editorconfig/editorconfig-core-js/status.svg)](https://david-dm.org/editorconfig/editorconfig-core-js)
|
5 |
|
6 | The EditorConfig JavaScript core will provide the same functionality as the
|
7 | [EditorConfig C Core][] and [EditorConfig Python Core][].
|
8 |
|
9 |
|
10 | ## Installation
|
11 |
|
12 | You need [node][] to use this package.
|
13 |
|
14 | To install the package locally:
|
15 |
|
16 | ```bash
|
17 | $ npm install editorconfig
|
18 | ```
|
19 |
|
20 | To install the package system-wide:
|
21 |
|
22 | ```bash
|
23 | $ npm install -g editorconfig
|
24 | ```
|
25 |
|
26 | ## Usage
|
27 |
|
28 | ### in Node.js:
|
29 |
|
30 | #### parse(filePath[, options])
|
31 |
|
32 | options is an object with the following defaults:
|
33 |
|
34 | ```js
|
35 | {
|
36 | config: '.editorconfig',
|
37 | version: pkg.version,
|
38 | root: '/'
|
39 | };
|
40 | ```
|
41 |
|
42 | Search for `.editorconfig` starting from the current directory to the root directory.
|
43 |
|
44 | Example:
|
45 |
|
46 | ```js
|
47 | var editorconfig = require('editorconfig');
|
48 | var path = require('path');
|
49 | var filePath = path.join(__dirname, '/sample.js');
|
50 | var promise = editorconfig.parse(filePath);
|
51 | promise.then(function onFulfilled(result) {
|
52 | console.log(result);
|
53 | });
|
54 |
|
55 | /*
|
56 | {
|
57 | indent_style: 'space',
|
58 | indent_size: 2,
|
59 | end_of_line: 'lf',
|
60 | charset: 'utf-8',
|
61 | trim_trailing_whitespace: true,
|
62 | insert_final_newline: true,
|
63 | tab_width: 2
|
64 | };
|
65 | */
|
66 | ```
|
67 |
|
68 | #### parseSync(filePath[, options])
|
69 |
|
70 | Synchronous version of `editorconfig.parse()`.
|
71 |
|
72 | #### parseString(fileContent)
|
73 |
|
74 | The `parse()` function above uses `parseString()` under the hood. If you have your file contents
|
75 | just pass it to `parseString()` and it'll return the same results as `parse()`.
|
76 |
|
77 | #### parseFromFiles(filePath, configs[, options])
|
78 |
|
79 | options is an object with the following defaults:
|
80 |
|
81 | ```js
|
82 | {
|
83 | config: '.editorconfig',
|
84 | version: pkg.version,
|
85 | root: '/'
|
86 | };
|
87 | ```
|
88 |
|
89 | Specify the `.editorconfig`.
|
90 |
|
91 | Example:
|
92 |
|
93 | ```js
|
94 | var editorconfig = require('editorconfig');
|
95 | var fs = require('fs');
|
96 | var path = require('path');
|
97 | var configPath = path.join(__dirname, '/.editorconfig');
|
98 | var configs = [
|
99 | {
|
100 | name: configPath,
|
101 | contents: fs.readFileSync(configPath, 'utf8')
|
102 | }
|
103 | ];
|
104 | var filePath = path.join(__dirname, '/sample.js');
|
105 | var promise = editorconfig.parseFromFiles(filePath, configs);
|
106 | promise.then(function onFulfilled(result) {
|
107 | console.log(result)
|
108 | });
|
109 |
|
110 | /*
|
111 | {
|
112 | indent_style: 'space',
|
113 | indent_size: 2,
|
114 | end_of_line: 'lf',
|
115 | charset: 'utf-8',
|
116 | trim_trailing_whitespace: true,
|
117 | insert_final_newline: true,
|
118 | tab_width: 2
|
119 | };
|
120 | */
|
121 | ```
|
122 |
|
123 | #### parseFromFilesSync(filePath, configs[, options])
|
124 |
|
125 | Synchronous version of `editorconfig.parseFromFiles()`.
|
126 |
|
127 | ### in Command Line
|
128 |
|
129 | ```bash
|
130 | $ ./bin/editorconfig
|
131 |
|
132 | Usage: editorconfig [OPTIONS] FILEPATH1 [FILEPATH2 FILEPATH3 ...]
|
133 |
|
134 | EditorConfig Node.js Core Version 0.11.4-development
|
135 |
|
136 | FILEPATH can be a hyphen (-) if you want path(s) to be read from stdin.
|
137 |
|
138 | Options:
|
139 |
|
140 | -h, --help output usage information
|
141 | -V, --version output the version number
|
142 | -f <path> Specify conf filename other than ".editorconfig"
|
143 | -b <version> Specify version (used by devs to test compatibility)
|
144 | ```
|
145 |
|
146 | Example:
|
147 |
|
148 | ```bash
|
149 | $ ./bin/editorconfig /home/zoidberg/humans/anatomy.md
|
150 | charset=utf-8
|
151 | insert_final_newline=true
|
152 | end_of_line=lf
|
153 | tab_width=8
|
154 | trim_trailing_whitespace=sometimes
|
155 | ```
|
156 |
|
157 | ## Development
|
158 |
|
159 | To install dependencies for this package run this in the package directory:
|
160 |
|
161 | ```bash
|
162 | $ npm install
|
163 | ```
|
164 |
|
165 | Next, run the following commands:
|
166 |
|
167 | ```bash
|
168 | $ npm run build
|
169 | $ npm run copy
|
170 | $ npm link ./dist
|
171 | ```
|
172 |
|
173 | The global editorconfig will now point to the files in your development
|
174 | repository instead of a globally-installed version from npm. You can now use
|
175 | editorconfig directly to test your changes.
|
176 |
|
177 | If you ever update from the central repository and there are errors, it might
|
178 | be because you are missing some dependencies. If that happens, just run npm
|
179 | link again to get the latest dependencies.
|
180 |
|
181 | To test the command line interface:
|
182 |
|
183 | ```bash
|
184 | $ editorconfig <filepath>
|
185 | ```
|
186 |
|
187 | # Testing
|
188 |
|
189 | [CMake][] must be installed to run the tests.
|
190 |
|
191 | To run the tests:
|
192 |
|
193 | ```bash
|
194 | $ npm test
|
195 | ```
|
196 |
|
197 | To run the tests with increased verbosity (for debugging test failures):
|
198 |
|
199 | ```bash
|
200 | $ npm run-script test-verbose
|
201 | ```
|
202 |
|
203 | [EditorConfig C Core]: https://github.com/editorconfig/editorconfig-core
|
204 | [EditorConfig Python Core]: https://github.com/editorconfig/editorconfig-core-py
|
205 | [node]: http://nodejs.org/
|
206 | [cmake]: http://www.cmake.org
|