UNPKG

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