UNPKG

2.03 kBJavaScriptView Raw
1const path = require('path')
2const fs = require('fs')
3const FirescriptParser = require('./FirescriptParser')
4const Parser = require('./Parser')
5const JSParser = require('./JSParser')
6const FirescriptTranspiler = require('./FirescriptTranspiler')
7const JSTranspiler = require('./JSTranspiler')
8const FSConfig = require('./utils/FSConfig')
9
10module.exports = {
11 FirescriptParser,
12 FirescriptTranspiler,
13 JSTranspiler,
14 JSParser,
15 Parser,
16 tokenize (input, opts) {
17 const parser = new FirescriptParser(opts)
18 return parser.tokenize(input)
19 },
20 transpileFile (filename, opts = {}) {
21 const ext = path.extname(filename)
22 opts.type = opts.type || ext === '.fire' ? 'fire' : 'js'
23 return this.transpile(fs.readFileSync(filename, 'utf8'), opts)
24 },
25 transpile (input, opts) {
26 opts = Object.assign({
27 type: 'fire'
28 }, opts || {})
29
30 let ast
31
32 if (typeof input === 'string') {
33 if (opts.verbose) console.log(`[TRANSPILER] Transpile source into ${opts.type === 'fire' ? 'Javascript' : 'Firescript'}`)
34 const parser = opts.type === 'js' ? new JSParser(opts) : new FirescriptParser(opts)
35 ast = parser.parse(input)
36 } else {
37 if (opts.verbose) console.log(`[TRANSPILER] Transpile AST into ${opts.type === 'fire' ? 'Javascript' : 'Firescript'}`)
38 ast = input
39 }
40
41 if (opts.type === 'js') {
42 const transpiler = new FirescriptTranspiler(opts)
43 return transpiler.transpile(ast)
44 } else {
45 const fsConf = new FSConfig()
46 if (opts && opts.features) {
47 fsConf.merge({
48 features: opts.features
49 })
50 }
51
52 opts.features = fsConf.getConf('features')
53 const transpiler = new JSTranspiler(opts)
54 return transpiler.transpile(ast)
55 }
56 },
57 parse (input, opts) {
58 opts = opts || {}
59 const parser = opts.type === 'js' ? new JSParser(opts) : new FirescriptParser(opts)
60 return parser.parse(input)
61 },
62 loadConf (customConf) {
63 const config = new FSConfig()
64 return config.merge(customConf)
65 }
66}