#!/usr/bin/env node var fs = require('fs'); var path = require('path'); var asyncToGen = require('./index'); var usage = 'Usage: async-to-gen [options] [sources] \n' + '\nOptions:\n' + ' -h, --help Show this message\n' + ' -v, --version Print the current version of async-to-gen\n' + ' -i, --ignore Paths to ignore, Regular Expression\n' + ' -x, --extensions File extensions to transform\n' + ' -o, --out-file The file path to write transformed file to\n' + ' -d, --out-dir The directory path to write transformed files within\n' + '\nExamples:\n' + '\nTransform one file:\n' + ' async-to-gen --out-file output.js input.js\n' + '\nTransform many files:\n' + ' async-to-gen --out-dir out/ input1.js input2.js\n' + '\nTransform files in directory:\n' + ' async-to-gen --out-dir out/ indir/\n' + '\nTransform stdin:\n' + ' cat input.js | async-to-gen > output.js\n'; function error(msg) { process.stderr.write('\n\033[31m ' + msg + '\033[0m\n\n'); process.exit(1); } var _memo = {}; function mkdirp(dirpath) { if (_memo[dirpath]) { return; } _memo[dirpath] = true; try { fs.mkdirSync(dirpath); } catch (err) { if (err.code === 'ENOENT') { mkdirp(path.dirname(dirpath)); fs.mkdirSync(dirpath); } else { try { stat = fs.statSync(dirpath); } catch (ignored) { throw err; } if (!stat.isDirectory()) { throw err; } } } } // Collect arguments var ignore = /node_modules/; var extensions = [ '.js', '.jsx', '.flow', '.es6' ]; var outDir; var outFile; var sources = []; var i = 2; while (i < process.argv.length) { var arg = process.argv[i++]; if (arg === '-h' || arg === '--help') { process.stdout.write(usage); process.exit(0); } else if (arg === '-v' || arg === '--version') { process.stdout.write('v' + require('./package').version); process.exit(0); } else if (arg === '-i' || arg === '--ignore') { ignore = new RegExp(process.argv[i++]); } else if (arg === '-x' || arg === '--extensions') { extensions = process.argv[i++].split(','); } else if (arg === '-o' || arg === '--out-file') { outFile = process.argv[i++]; } else if (arg === '-d' || arg === '--out-dir') { outDir = process.argv[i++]; } else { sources.push(arg); } } // Validate arguments if (outDir && outFile) { error('Only specify one of --out-dir or --out-file'); } if (outDir && sources.length === 0) { error('Must specify files when providing --out-dir'); } // Ensure all sources exist for (var i = 0; i < sources.length; i++) { try { var stat = fs.lstatSync(sources[i]); if (sources.length > 1 && !stat.isFile()) { error('Source "' + sources[i] + '" is not a file.'); } } catch (err) { error('Source "' + sources[i] + '" does not exist.'); } } // Process stdin if no sources were provided if (sources.length === 0) { var content = ''; process.stdin.setEncoding('utf-8'); process.stdin.resume(); process.stdin.on('data', function (str) { content += str; }); process.stdin.on('end', function () { var result = transformSource(content, ''); if (outFile) { fs.writeFileSync(outFile, result); } else { process.stdout.write(result); } }); return; } var isDirSource = sources.length === 1 && fs.statSync(sources[0]).isDirectory(); if ((sources.length > 1 || isDirSource) && !outDir) { error('Multiple files require providing --out-dir'); } // Process multiple files for (var i = 0; i < sources.length; i++) { var source = sources[i]; var stat = fs.lstatSync(source); if (stat.isDirectory()) { var files = fs.readdirSync(source); for (var j = 0; j < files.length; j++) { var subSource = path.join(source, files[j]); if (!ignore || !ignore.test(subSource)) { sources.push(subSource); } } } else if (stat.isFile() && extensions.indexOf(path.extname(source)) !== -1) { var content = fs.readFileSync(source, 'utf8'); var result = transformSource(content, source); if (outDir) { outFile = path.join(outDir, isDirSource ? path.relative(sources[0], source) : source); mkdirp(path.dirname(outFile)); } if (outFile) { fs.writeFileSync(outFile, result); process.stdout.write(source + '\n \u21B3 \033[32m' + outFile + '\033[0m\n'); } else { process.stdout.write(result); } } } function transformSource(content, filepath) { try { return asyncToGen(content).toString(); } catch (error) { if (error.loc) { var line = error.loc.line - 1; var col = error.loc.column; var text = content.split(/\r\n?|\n|\u2028|\u2029/)[line]; process.stderr.write( filepath + '\n' + ' \u21B3 \033[31mSyntax Error: ' + error.message + '\033[0m\n' + ' \033[90m' + line + ': \033[0m' + text.slice(0, col) + '\033[7;31m' + text[col] + '\033[0m' + text.slice(col + 1) + '\n' ); process.exit(1); } throw error; } }