-
Notifications
You must be signed in to change notification settings - Fork 0
/
JS2TS.py
56 lines (48 loc) · 1.54 KB
/
JS2TS.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import os
import threading
from queue import Queue
from utils.File import read_all_files
from utils.Converter import converter
def convert(q):
while True:
file_path = q.get()
if file_path is None:
break
try:
with open(file_path, 'r') as file:
content = file.read()
text = converter(content)
try:
os.makedirs(f'./output/{os.path.dirname(file_path)}', exist_ok=True)
with open(f"./output/{file_path}".replace(".js", ".ts"), 'w') as file:
file.write(text)
print(f"Converted {file_path} to ./output/{file_path}")
except IOError as e:
print(f"Error when save: {e}")
except Exception as e:
print(f"Error when process {file_path}")
finally:
q.task_done()
def main():
input_directory = input("Input directory: ")
if not os.path.exists(input_directory):
print(f"{input_directory} doesn't exists!")
return
if not os.path.isdir(input_directory):
print(f"'{input_directory}' is not directory")
return
files = read_all_files(input_directory)
q = Queue()
for file in files:
q.put(file)
threads = []
for i in range(10):
thread = threading.Thread(target=convert, args=(q,))
threads.append(thread)
thread.start()
q.join()
for i in range(10):
q.put(None)
for thread in threads:
thread.join()
main()