-
Notifications
You must be signed in to change notification settings - Fork 0
/
structureGenerator.py
71 lines (57 loc) · 2.62 KB
/
structureGenerator.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
import os
import sys
from toast import toast
from Constants import ToastConstants, CodeConstants, FileConstants
class FileManager:
def __init__(self):
self.file_extensions = ['.tsx', 'Container.ts', 'types.ts']
def create_folder(self, folder_name):
try:
os.makedirs(folder_name, exist_ok=True)
except Exception as e:
toast(ToastConstants.Message.FOLDER_CREATING_ERROR.format(folder_name, e), type=ToastConstants.Type.ERROR)
def create_file(self, file_path, content):
try:
with open(file_path, 'w') as file:
file.write(content)
except Exception as e:
toast(ToastConstants.Message.FILE_CREATING_ERROR.format(file_path, e), type=ToastConstants.Type.ERROR)
def create_files(self, file_names):
if not file_names:
toast(ToastConstants.Message.FILE_NAME_NOT_PROVIDED, type=ToastConstants.Type.INFO)
return
for file_name in file_names:
if ' ' in file_name:
toast(ToastConstants.Message.FILE_NAME_SPACES_ERROR.format(file_name), type=ToastConstants.Type.INFO)
continue
file_basename:str = ''
file_basename, _ = os.path.splitext(file_name)
capitalBaseName = file_basename[0].upper() + file_basename[1:]
self.create_folder(file_basename)
component_content = CodeConstants.Structure.COMPONENT_FILE.format(file_basename, capitalBaseName)
container_content = CodeConstants.Structure.CONTAINER_FILE.format(capitalBaseName)
for extension in self.file_extensions:
if(extension == 'Container.ts'):
content = container_content
elif extension == '.tsx':
content = component_content
else:
content = ''
self.create_file(os.path.join(file_basename, f'{file_basename if extension != 'types.ts' else ''}{extension}'), content)
toast(ToastConstants.Message.FILE_CREATING_SUCCESS.format(file_basename), type=ToastConstants.Type.SUCCESS)
def main():
file_manager = FileManager()
# Check if file names are provided as command-line arguments
if len(sys.argv) > 1:
file_names = sys.argv[1:]
else:
# If not provided, ask for file names
file_names = []
while True:
file_name = input("Enter file name (leave blank to finish): ").strip()
if not file_name:
break
file_names.append(file_name)
file_manager.create_files(file_names)
if __name__ == "__main__":
main()