-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
146 lines (123 loc) · 4.67 KB
/
main.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import sys, os
import time
import platform
from typing import Union
PAUSE = "pause" if platform.system() == "Windows" else "read var"
CLEAR = "cls" if platform.system() == "Windows" else "clear"
path = sys.argv[1]
try:
file = sys.argv[2]
except IndexError:
file = "index.js"
iPath = os.path.join(path, "src")
if not os.path.exists(iPath):
print("Error: Could not find './src/'")
os.system(PAUSE)
exit()
pFile = os.path.join(iPath, file)
if not os.path.exists(pFile):
print("Error: Could not find './src/" + file + "'")
os.system(PAUSE)
exit()
os.system("title=React Parameters")
lu = 0
while 1:
# if file is edited, enter in condition
if os.path.getmtime(pFile) > lu:
os.system(CLEAR)
print("React Parameters initialized")
print("")
print("Last update: " + time.strftime("%H:%M:%S"))
if os.path.exists(pFile):
iFile = open(pFile, "r").read()
else:
print(f"Error: Could not find './src/{file}'")
os.system(PAUSE)
continue
iFile = iFile.split("\n")
app = []
# found const app = {...} in file
appIndex = 0
spaceNb = 0
for i in range(len(iFile)):
if("const app" in iFile[i]):
appIndex = i
spaceNb = iFile[i].index("const")
break
if not appIndex:
print(f"Error: Could not find the constant 'app' object in './src/{file}'")
os.system(PAUSE)
continue
# found end of consr app = {...} in file (if the const is not on one line)
sep = []
closeIndex = -1
if not "}" in iFile[appIndex]:
for i in range(appIndex, len(iFile)):
if("}" in iFile[i]):
closeIndex = i
break
sep.append(iFile[0:appIndex])
sep.append(iFile[closeIndex+1:])
else:
sep.append(iFile[0:appIndex])
sep.append(iFile[appIndex+1:])
# found all parameters in const app = {...} in file and put them in params list
params = []
for l in iFile:
# if line index is under const app = {...} index, continue
if(iFile.index(l) > appIndex):
continue
l = "".join(l.split(" ")).strip()
types = "any"
if(("const[" in l or "const [" in l) and not l.strip().startswith("//")):
if not "useState" in l:
continue
if file.endswith(("tsx", "ts")) and "<" in l and ">" in l:
# split and keep the types between firsts "<" and ">" (to catch Component<Array<Type>>() for example)
types: str = ">".join("<".join(l.split("<")[1:]).split(">")[:-1])
l = l.split("=")[0].strip()
l = l.split("[")[1].split("]")[0].strip().split(",")
l = [x.strip() for x in l]
if len(l) != 2:
continue
if l[0] != "":
params.append({
"var": l[0],
"type": types
})
if l[1] != "":
params.append({
"var": l[1],
"type": f"React.Dispatch<React.SetStateAction<{types}>>"
})
# format params list to make new interface with all parameters and types
interfaceContent = None
if(params and file.endswith(("tsx", "ts"))):
interfaceContent = ["{}: {}".format(x["var"], x["type"] if x["type"] else "any") for x in params]
# interfaceContent.append("[key: string]: any;")
interfaceContent = "export interface NkContext extends NkContextVariant {\n " + ";\n ".join(interfaceContent) + "\n}\n"
# format params list to become new const app = {...} in file
app = ("\t" * spaceNb) + "const app{}".format((": Partial<NkContext>" if file.endswith(("tsx", "ts")) else "")) + " = {" + ",".join([x["var"] for x in params]) + "};"
# include new const app = {...} in file
nFile = "\n".join(sep[0] + [app] + sep[1])
# write file
open(pFile, "w").write(nFile)
if(interfaceContent):
imports = ""
contextVariant = os.path.join(iPath, "types", "nkContextVariant.types.ts")
contextTypepath = os.path.join(iPath, "types", "nkContext.types.ts")
if not os.path.exists(os.path.join(iPath, "types")):
os.mkdir(os.path.join(iPath, "types"))
if not os.path.exists(contextVariant):
open(contextVariant, "w").write("export interface NkContextVariant {\n\t\n}\n")
if os.path.exists(contextTypepath):
importVariant = "import { NkContextVariant } from './nkContextVariant.types';"
imports = open(contextTypepath, "r").read().split("\n")
imports = [x for x in imports if x.startswith("import")]
if not importVariant in imports:
imports.append(importVariant)
imports = "\n".join(imports) + "\n\n"
open(os.path.join(iPath, "types", "nkContext.types.ts"), "w").write(imports + interfaceContent)
# update last update time
lu = os.path.getmtime(pFile)
time.sleep(0.5)