-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathflatten_jsx.py
executable file
·88 lines (73 loc) · 2.29 KB
/
flatten_jsx.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
82
83
84
85
86
87
88
#!env python
#
# flatten jsx to a single file, for example:
# //@include "xtools.jsx"
# this script would try to find "xtools.jsx" file in "include search path" and replace it
#
import argparse
import sys
import os
import codecs
parser = argparse.ArgumentParser(description='photoshop jsx fatten')
parser.add_argument('inputfile')
parser.add_argument('outputfile')
parser.add_argument('-I', type=str, nargs='*',
help='inlcude search path')
args = parser.parse_args()
inputfile = args.inputfile
outputfile = args.outputfile
include_paths = [ ]
if not os.path.isfile(inputfile):
print('inputfile %s notfound' % inputfile)
exit(1)
src_dir = os.path.dirname(inputfile)
if src_dir == '':
include_paths.append('.')
else:
include_paths.append(src_dir)
if args.I is not None:
for item in args.I:
include_paths.append(item)
def search_include_file(filename):
if filename is None:
return None
path = None
for dir in include_paths:
tmp = dir + '/' + filename
if os.path.isfile(tmp):
path = tmp
break
return path
included_path = {}
def flatten(file):
print('including: ' + file)
data = open(file, "r", encoding="utf-8").read()
new = ''
line_idx = 0
for line in data.splitlines():
line_idx += 1
stripline = line.strip()
if stripline.startswith("exports."): # delete
continue
elif stripline.startswith("//@include ") or stripline.startswith("require("):
in_file = line.split('"')[1]
if in_file.endswith('.ts'):
in_file = in_file.replace('.ts', '.js')
in_path = search_include_file(in_file)
if in_path is None:
print("error: file %s(%s, line %d) notfound.." % (in_file, file, line_idx))
exit(1)
if in_file in included_path: # already include, skip
# print('skip', in_file)
continue
included_path[in_file] = True
new += flatten(in_path)
elif stripline.startswith("//"): # delete all comments
continue
else:
new += line
new += '\n'
return new
data = flatten(inputfile)
with open(outputfile, 'w', encoding="utf-8-sig") as f:
f.writelines(data)