UNPKG

firescript

Version:
48 lines (41 loc) 1.31 kB
const fs = require('fs') const path = require('path') const { Command } = require('supercmd') const FirescriptParser = require('firescript-parser').FirescriptParser const command = new Command() module.exports.command = command command .cmd('parse [file] [output?]') .description('Parse a .fire or .js file into an AST tree') .option('-c, --comments', 'Include comments') .option('-l, --location', 'Add location') .option('--no-colors', 'Disable cli colors') .option('-v, --verbose', 'Verbose log') // .option('-r, --range', 'Add range') .action((ctx, file, output) => { const fsSource = fs.readFileSync(file, { encoding: 'utf8' }) const parser = new FirescriptParser({ filename: file, }) try { const ast = parser.parse(fsSource, { type: 'fire', includeComments: ctx.comments, setLocation: !!ctx.location, setRange: !!ctx.range, filename: file }) const source = JSON.stringify(ast, null, ' ') if (output) { fs.writeFileSync(output, source, { encoding: 'utf8' }) } else { console.log(source) } } catch (err) { if (ctx.noColors) { err.colorsEnabled = false } console.log(ctx.verbose ? err.stack : err.toString()) process.exit(1) } })