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