import os
import subprocess
import tempfile

from webassets.filter import Filter
from webassets.exceptions import FilterError


__all__ = ('TypeScript',)


class TypeScript(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 = 'typescript'
    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
        input_filename = tempfile.mktemp() + ".ts"
        output_filename = tempfile.mktemp()

        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, 'rb') as f:
            out.write(f.read())

        os.unlink(input_filename)
        os.unlink(output_filename)

import os
import subprocess
import tempfile

from webassets.filter import Filter
from webassets.exceptions import FilterError


__all__ = ('TypeScript',)


class TypeScript(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 = 'typescript'
    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
        input_filename = tempfile.mktemp() + ".ts"
        output_filename = tempfile.mktemp()

        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, 'rb') as f:
            out.write(f.read())

        os.unlink(input_filename)
        os.unlink(output_filename)

Fix a typo