Last active
December 18, 2015 22:38
-
-
Save brehaut/5855561 to your computer and use it in GitHub Desktop.
Backporting the TypeScript filter to webassets 0.8 for use with tsc 0.9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""This is an alternate typescript handler from future webassets as there | |
is a bug in the 0.8 version with recent tsc | |
""" | |
import os | |
import subprocess | |
import tempfile | |
from io import open # Give 2 and 3 use same newline behaviour. | |
from webassets.filter import Filter, register_filter | |
from webassets.exceptions import FilterError | |
__all__ = ('TypeScriptAlt',) | |
class TypeScriptAlt(Filter): | |
"""Compile `TypeScript <http://www.typescriptlang.org>`_ to JavaScript. | |
TypeScript is an external tool written for NodeJS. | |
This filter assumes that the ``tsc`` executable is in the path. Otherwise, you | |
may define the ``TYPESCRIPT_BIN`` setting. | |
""" | |
name = 'typescriptalt' | |
max_debug_level = None | |
options = { | |
'binary': 'TYPESCRIPT_BIN', | |
} | |
def output(self, _in, out, **kw): | |
# The typescript compiler cannot read a file which does not have | |
# the .ts extension. The output file needs to have an extension, | |
# or the compiler will want to create a directory in its place. | |
input_filename = tempfile.mktemp() + ".ts" | |
output_filename = tempfile.mktemp() + ".js" | |
with open(input_filename, 'wb') as f: | |
f.write(_in.read()) | |
args = [self.binary or 'tsc', '--out', output_filename, input_filename | |
] | |
proc = subprocess.Popen( | |
args, | |
stdin=subprocess.PIPE, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE) | |
stdout, stderr = proc.communicate() | |
if proc.returncode != 0: | |
raise FilterError("typescript: subprocess had error: stderr=%s," % stderr + | |
"stdout=%s, returncode=%s" % (stdout, proc.returncode)) | |
with open(output_filename, 'r') as f: | |
out.write(f.read()) | |
os.unlink(input_filename) | |
os.unlink(output_filename) | |
register_filter(TypeScriptAlt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment