codeformat.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. 'use strict';
  2. var _ = require('lodash');
  3. var bluebird = require('bluebird');
  4. var path = require('path');
  5. var glob = bluebird.promisify(require('glob'));
  6. var fs = require('fs');
  7. var readFile = bluebird.promisify(fs.readFile);
  8. var writeFile = bluebird.promisify(fs.writeFile);
  9. var beautify = require('js-beautify').js_beautify;
  10. var beautify_options = {
  11. "indent_size": 2,
  12. "indent_char": " ",
  13. "eol": "\n",
  14. "indent_level": 0,
  15. "indent_with_tabs": false,
  16. "preserve_newlines": true,
  17. "max_preserve_newlines": 10,
  18. "jslint_happy": false,
  19. "space_after_anon_function": false,
  20. "brace_style": "collapse",
  21. "keep_array_indentation": false,
  22. "keep_function_indentation": false,
  23. "space_before_conditional": true,
  24. "break_chained_methods": false,
  25. "eval_code": false,
  26. "end_with_newline": true
  27. };
  28. console.log("Starting formatting.");
  29. var sources = path.join('src', 'languages', '*.js');
  30. bluebird.map(glob(sources), function (name) {
  31. var basename = path.basename(name, '.js');
  32. return readFile(name).then(function (blob) {
  33. return beautify(blob.toString(), beautify_options);
  34. }).then(function (blob) {
  35. return writeFile(name, blob).then(function () {
  36. console.log(" ✓ " + basename);
  37. });
  38. });
  39. }).then(function () {
  40. console.log("Finished formatting.");
  41. });