UNPKG

4.49 kBMarkdownView Raw
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
6The EditorConfig JavaScript core will provide the same functionality as the
7[EditorConfig C Core][] and [EditorConfig Python Core][].
8
9
10## Installation
11
12You need [node][] to use this package.
13
14To install the package locally:
15
16```bash
17$ npm install editorconfig
18```
19
20To 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
32options is an object with the following defaults:
33
34```js
35{
36 config: '.editorconfig',
37 version: pkg.version,
38 root: '/'
39};
40```
41
42Search for `.editorconfig` starting from the current directory to the root directory.
43
44Example:
45
46```js
47var editorconfig = require('editorconfig');
48var path = require('path');
49var filePath = path.join(__dirname, '/sample.js');
50var promise = editorconfig.parse(filePath);
51promise.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
70Synchronous version of `editorconfig.parse()`.
71
72#### parseString(fileContent)
73
74The `parse()` function above uses `parseString()` under the hood. If you have your file contents
75just pass it to `parseString()` and it'll return the same results as `parse()`.
76
77#### parseFromFiles(filePath, configs[, options])
78
79options is an object with the following defaults:
80
81```js
82{
83 config: '.editorconfig',
84 version: pkg.version,
85 root: '/'
86};
87```
88
89Specify the `.editorconfig`.
90
91Example:
92
93```js
94var editorconfig = require('editorconfig');
95var fs = require('fs');
96var path = require('path');
97var configPath = path.join(__dirname, '/.editorconfig');
98var configs = [
99 {
100 name: configPath,
101 contents: fs.readFileSync(configPath, 'utf8')
102 }
103];
104var filePath = path.join(__dirname, '/sample.js');
105var promise = editorconfig.parseFromFiles(filePath, configs);
106promise.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
125Synchronous 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
146Example:
147
148```bash
149$ ./bin/editorconfig /home/zoidberg/humans/anatomy.md
150charset=utf-8
151insert_final_newline=true
152end_of_line=lf
153tab_width=8
154trim_trailing_whitespace=sometimes
155```
156
157## Development
158
159To install dependencies for this package run this in the package directory:
160
161```bash
162$ npm install
163```
164
165Next, run the following commands:
166
167```bash
168$ npm run build
169$ npm run copy
170$ npm link ./dist
171```
172
173The global editorconfig will now point to the files in your development
174repository instead of a globally-installed version from npm. You can now use
175editorconfig directly to test your changes.
176
177If you ever update from the central repository and there are errors, it might
178be because you are missing some dependencies. If that happens, just run npm
179link again to get the latest dependencies.
180
181To 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
191To run the tests:
192
193```bash
194$ npm test
195```
196
197To 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