-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsMinifier.py
81 lines (67 loc) · 1.92 KB
/
tsMinifier.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import re
import sys
###
# @class TSMinifier
# @brief Minifies Oscar library
###
class TSMinifier:
###
# Runs minifier
# @param folder Reference folder
# @param refFileName Reference file
# @param output Target
###
def run(self, folder, refFileName, output):
print('\nProcessing...')
pattern = '<reference path="(.*)"'
collected = []
if folder[len(folder) - 1] != '/':
folder += '/'
# First collect all files to browse
with open(folder + refFileName, 'r') as refFile:
for line in refFile:
outcome = re.findall(pattern, line)
for e in outcome:
collected.append(e)
# Then, create minified file
with open(output, 'w') as outputFile:
for e in collected:
with open(folder + e) as file:
content = file.read()
# Remove comments
minifiedContent = re.sub('\/\/(.*)\n', '', content)
minifiedContent = re.sub('\/\*\*(.+?)\*\/', '', minifiedContent, 0, re.DOTALL)
# Remove whitespaces
minifiedContent = re.sub('\n', ' ', minifiedContent)
minifiedContent = re.sub('\t', '', minifiedContent)
minifiedContent = re.sub('\s{2,}', ' ', minifiedContent)
minifiedContent = minifiedContent.strip()
outputFile.write(minifiedContent)
print('\nDone!')
if __name__ == '__main__':
folder = 'src'
refFileName = 'ref.ts'
output = 'output.min.ts'
if len(sys.argv) < 2:
prompt = ' > '
print('--- TS minifier Wizard ---')
print('\nSource folder: (default \'src\')')
data = raw_input(prompt)
if data != '':
folder = data
print('\nReference file name: (default \'ref.ts\')')
data = raw_input(prompt)
if data != '':
refFileName = data
print('\nOutput: (default \'output.min.ts\')')
data = raw_input(prompt)
if data != '':
output = data
else:
if len(sys.argv) >= 2:
folder = sys.argv[1]
if len(sys.argv) >= 3:
refFileName = sys.argv[2]
if len(sys.argv) >= 4:
output = sys.argv[3]
TSMinifier().run(folder, refFileName, output)