You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.6 KiB

  1. #!/usr/bin/env node
  2. /*
  3. Copyright (C) 2012 Yusuke Suzuki <utatane.tea@gmail.com>
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  12. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  13. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  14. ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  15. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  16. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  17. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  18. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  19. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  20. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. */
  22. /*jslint sloppy:true node:true */
  23. var fs = require('fs'),
  24. path = require('path'),
  25. root = path.join(path.dirname(fs.realpathSync(__filename)), '..'),
  26. esprima = require('esprima'),
  27. escodegen = require(root),
  28. optionator = require('optionator')({
  29. prepend: 'Usage: escodegen [options] file...',
  30. options: [
  31. {
  32. option: 'config',
  33. alias: 'c',
  34. type: 'String',
  35. description: 'configuration json for escodegen'
  36. }
  37. ]
  38. }),
  39. args = optionator.parse(process.argv),
  40. files = args._,
  41. options,
  42. esprimaOptions = {
  43. raw: true,
  44. tokens: true,
  45. range: true,
  46. comment: true
  47. };
  48. if (files.length === 0) {
  49. console.log(optionator.generateHelp());
  50. process.exit(1);
  51. }
  52. if (args.config) {
  53. try {
  54. options = JSON.parse(fs.readFileSync(args.config, 'utf-8'));
  55. } catch (err) {
  56. console.error('Error parsing config: ', err);
  57. }
  58. }
  59. files.forEach(function (filename) {
  60. var content = fs.readFileSync(filename, 'utf-8'),
  61. syntax = esprima.parse(content, esprimaOptions);
  62. if (options.comment) {
  63. escodegen.attachComments(syntax, syntax.comments, syntax.tokens);
  64. }
  65. console.log(escodegen.generate(syntax, options));
  66. });
  67. /* vim: set sw=4 ts=4 et tw=80 : */