Advertisement
renix1

Typescript Transpiler Plugin Sublime Text 3

Oct 8th, 2019
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | None | 0 0
  1. import sublime, sublime_plugin
  2. import subprocess
  3. import os
  4. import datetime
  5.  
  6. class Transpiler:
  7.     def __init__(self, fn):
  8.         self.__filename = fn
  9.         self.__validate_file = None
  10.         self.__returncode = 0
  11.         self.status = None
  12.         self.now = datetime.datetime.now()
  13.         self.hour = self.now.strftime("%H:%M:%S")
  14.  
  15.     def __is_valid(self):
  16.         if self.__filename.endswith('.ts'):
  17.             return True
  18.         else:
  19.             return -1
  20.  
  21.     def __run(self):
  22.         process = subprocess.Popen("tsc --p ../ts/",
  23.                                     stdout=subprocess.PIPE,
  24.                                     shell=True)
  25.         out, err = process.communicate()
  26.         return (out.decode('ascii'), process.returncode)
  27.  
  28.     def __get_error (self, error):
  29.         error = error[0]
  30.         return error[:error.find(':')]
  31.  
  32.     def run(self):
  33.         self.__validate_file = self.__is_valid()
  34.         if self.__validate_file == True:
  35.             execution = self.__run()
  36.             if ('error' not in execution[0]) and (execution[1] == 0):
  37.                 self.status = "✓ Transpilation sucessfully at {}".format(self.hour)
  38.             else:
  39.                 self.status = "✗ Transpilation failed at {} - {}".format(self.hour, self.__get_error(execution))
  40.         else:
  41.             self.status = ""
  42.  
  43. class EventListener(sublime_plugin.EventListener):
  44.     def on_post_save_async(self, view):
  45.         transpiler = Transpiler(view.file_name())
  46.         transpiler.run()
  47.         view.set_status('transpiler', transpiler.status)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement