|
| 1 | +/*global process:false*/ |
| 2 | +/*global module:true*/ |
| 3 | +/*global exports:true*/ |
| 4 | +"use strict"; |
| 5 | + |
| 6 | +var transform = require('jstransform').transform; |
| 7 | +var typesSyntax = require('jstransform/visitors/type-syntax'); |
| 8 | +var visitors = require('./visitors'); |
| 9 | + |
| 10 | +/** |
| 11 | + * @typechecks |
| 12 | + * @param {string} source |
| 13 | + * @param {object?} options |
| 14 | + * @param {array?} excludes |
| 15 | + * @return {string} |
| 16 | + */ |
| 17 | +function transformAll(source, options, excludes) { |
| 18 | + excludes = excludes || []; |
| 19 | + |
| 20 | + // Stripping types needs to happen before the other transforms |
| 21 | + // unfortunately, due to bad interactions. For example, |
| 22 | + // es6-rest-param-visitors conflict with stripping rest param type |
| 23 | + // annotation |
| 24 | + source = transform(typesSyntax.visitorList, source, options).code; |
| 25 | + |
| 26 | + // The typechecker transform must run in a second pass in order to operate on |
| 27 | + // the entire source code -- so exclude it from the first pass |
| 28 | + var visitorsList = visitors.getAllVisitors(excludes.concat('typechecker')); |
| 29 | + source = transform(visitorsList, source, options); |
| 30 | + if (excludes.indexOf('typechecks') == -1 && /@typechecks/.test(source.code)) { |
| 31 | + source = transform( |
| 32 | + visitors.transformVisitors.typechecker, |
| 33 | + source.code, |
| 34 | + options |
| 35 | + ); |
| 36 | + } |
| 37 | + return source; |
| 38 | +} |
| 39 | + |
| 40 | +function runCli(argv) { |
| 41 | + var options = {}; |
| 42 | + for (var optName in argv) { |
| 43 | + if (optName === '_' || optName === '$0') { |
| 44 | + continue; |
| 45 | + } |
| 46 | + options[optName] = optimist.argv[optName]; |
| 47 | + } |
| 48 | + |
| 49 | + if (options.help) { |
| 50 | + optimist.showHelp(); |
| 51 | + process.exit(0); |
| 52 | + } |
| 53 | + |
| 54 | + var excludes = options.excludes; |
| 55 | + delete options.excludes; |
| 56 | + |
| 57 | + var source = ''; |
| 58 | + process.stdin.resume(); |
| 59 | + process.stdin.setEncoding('utf8'); |
| 60 | + process.stdin.on('data', function (chunk) { |
| 61 | + source += chunk; |
| 62 | + }); |
| 63 | + process.stdin.on('end', function () { |
| 64 | + try { |
| 65 | + source = transformAll(source, options, excludes); |
| 66 | + } catch (e) { |
| 67 | + console.error(e.stack); |
| 68 | + process.exit(1); |
| 69 | + } |
| 70 | + process.stdout.write(source.code); |
| 71 | + }); |
| 72 | +} |
| 73 | + |
| 74 | +if (require.main === module) { |
| 75 | + var optimist = require('optimist'); |
| 76 | + |
| 77 | + optimist = optimist |
| 78 | + .usage('Usage: $0 [options]') |
| 79 | + .default('exclude', []) |
| 80 | + .boolean('help').alias('h', 'help') |
| 81 | + .boolean('minify') |
| 82 | + .describe( |
| 83 | + 'minify', |
| 84 | + 'Best-effort minification of the output source (when possible)' |
| 85 | + ) |
| 86 | + .describe( |
| 87 | + 'exclude', |
| 88 | + 'A list of transformNames to exclude' |
| 89 | + ); |
| 90 | + |
| 91 | + runCli(optimist.argv); |
| 92 | +} else { |
| 93 | + exports.transformAll = transformAll; |
| 94 | +} |
0 commit comments