74ae727e0b07433f164ba26e1624cc4ffd7205732dbb11753966d9d8b45758cd1f836a6a3af957a31c76900e308b8e2e7767713c71d972c6c8d2eb7ed99b75 4.4 KB

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