Skip to content

Commit

Permalink
Add example performing babel conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jb Aviat committed Aug 2, 2016
1 parent 526abee commit 0443eb8
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions helpers/babel.py
@@ -0,0 +1,42 @@
#!/usr/bin/env python
""" Transform the input stream using babel.transform """
import os
import sys

from py_mini_racer import py_mini_racer

def babel_transform(es_string):
""" Transform the provided string using babel.transform """

path_to_babel = os.path.join(os.path.dirname(__file__), '..', 'tests',
'fixtures', 'babel.js')

es6 = '[1,2,3].map(n => n + 1);'

babel_source = open(path_to_babel, "r").read()

# Initializes PyMiniRacer
ctx = py_mini_racer.MiniRacer()

# Parse babel
ctx.eval("""var self = this; %s """ % babel_source)

# Transform stuff :)
val = "babel.transform(`%s`)['code']" % es_string
res = ctx.eval(val)
return res


if __name__ == '__main__':

if len(sys.argv) != 1:
name = sys.argv[0]
sys.stderr.write("Usage: cat es6file.js | %s\n" % name)
sys.stderr.write("Example: echo [1,2,3].map(n => n + 1); | %s\n" % name)
sys.exit(-1)

es6_data = sys.stdin.read()

res = babel_transform(es6_data)

print(res)

0 comments on commit 0443eb8

Please sign in to comment.