forked from jagenjo/litegraph.js
-
Notifications
You must be signed in to change notification settings - Fork 6
/
build.py
175 lines (156 loc) · 5.86 KB
/
build.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import os
import subprocess
import json
import sys
import re
# Define the lists of JS files to concatenate
js_files_lists = [
{
"output_filename": "litegraph.js",
"js_files": [
"./src/litegraph.js",
"./src/contextmenu.js",
"./src/lgraphcanvas.js",
"./src/dragandscale.js",
"./src/lgraphnode.js",
"./src/lgraphgroup.js",
"./src/lgraph.js",
"./src/llink.js",
"./src/nodes/base.js",
"./src/nodes/events.js",
"./src/nodes/interface.js",
"./src/nodes/input.js",
"./src/nodes/math.js",
"./src/nodes/math3d.js",
"./src/nodes/strings.js",
"./src/nodes/logic.js",
"./src/nodes/graphics.js",
"./src/nodes/gltextures.js",
"./src/nodes/glshaders.js",
"./src/nodes/geometry.js",
"./src/nodes/glfx.js",
"./src/nodes/midi.js",
"./src/nodes/audio.js",
"./src/nodes/network.js",
]
},
{
"output_filename": "litegraph.mini.js",
"js_files": [
"./src/litegraph.js",
"./src/contextmenu.js",
"./src/lgraphcanvas.js",
"./src/dragandscale.js",
"./src/lgraphnode.js",
"./src/lgraphgroup.js",
"./src/lgraph.js",
"./src/llink.js",
"./src/nodes/base.js",
"./src/nodes/events.js",
"./src/nodes/input.js",
"./src/nodes/math.js",
"./src/nodes/strings.js",
"./src/nodes/logic.js",
"./src/nodes/network.js",
]
},
{
"output_filename": "litegraph.core.js",
"js_files": [
"./src/litegraph.js",
"./src/contextmenu.js",
"./src/lgraphcanvas.js",
"./src/dragandscale.js",
"./src/lgraphnode.js",
"./src/lgraphgroup.js",
"./src/lgraph.js",
"./src/llink.js",
]
}
]
# Specify the build folder
build_folder = "build"
# Function to concatenate JS files
def concatenate_js_files(js_files, output_filename):
concatenated_data = "// readable version"
for js_file in js_files:
print("Processing " + js_file + " ", end="")
try:
with open(js_file, "r") as f:
concatenated_data += f.read() + "\n"
print("\033[92mOK\033[0m")
except FileNotFoundError:
print("\033[91mJS File not found\033[0m")
# Write the concatenated data to the output file
with open(os.path.join(build_folder, output_filename), "w") as output_file:
output_file.write(concatenated_data)
print("Concatenated JS files saved to: " + output_filename)
def pack_js_files(js_files, output_filename):
concatenated_data = "/*packed*/"
for js_file in js_files:
print("Processing " + js_file + " ", end="")
if os.path.exists(js_file):
# Minify the JS file using uglifyjs
minified_js = subprocess.run(["uglifyjs", js_file, "-c"], stdout=subprocess.PIPE, text=True)
concatenated_data += minified_js.stdout + "\n"
print("\033[92mMinified\033[0m")
else:
print("\033[91mJS File not found\033[0m")
# Write the concatenated data to the output file
with open(os.path.join(build_folder, output_filename), "w") as output_file:
output_file.write(concatenated_data)
print("Concatenated and minified JS files saved to: " + output_filename)
def update_version(version, command=""):
if "--minor" in command:
version_parts = version.split('.')
version_parts[1] = str(int(version_parts[1]) + 1)
version = '.'.join(version_parts)
elif "--major" in command:
version_parts = version.split('.')
version_parts[0] = str(int(version_parts[0]) + 1)
version_parts[1] = '0'
version_parts[2] = '0'
version = '.'.join(version_parts)
else:
version_parts = version.split('.')
version_parts[2] = str(int(version_parts[2]) + 1)
version = '.'.join(version_parts)
return version
def update_version_in_files(version, files):
for file_path in files:
try:
with open(file_path, 'r') as file:
data = file.read()
data = re.sub(r'("version": )"\d+.\d+.\d+"', r'\1"{}"'.format(version), data)
data = re.sub(r'(this.VERSION = )"\d+.\d+.\d+";', r'\1"{}";'.format(version), data)
with open(file_path, 'w') as file:
file.write(data)
print("Version updated in file: " + file_path)
except FileNotFoundError:
print("File not found: " + file_path)
def get_version_from_package_json():
try:
with open('package.json', 'r') as file:
data = json.load(file)
return data.get('version')
except (FileNotFoundError, json.JSONDecodeError):
sys.exit()
def get_new_version():
# Specify the version number and files to update
version_number = get_version_from_package_json()
if "--minor" in sys.argv:
version_number = update_version(version_number, "--minor")
elif "--major" in sys.argv:
version_number = update_version(version_number, "--major")
else:
version_number = update_version(version_number)
return version_number
# Create build folder if it does not exist
if not os.path.exists(build_folder):
os.makedirs(build_folder)
# Concatenate JS files from each list and save to the respective output filenames
for js_files_list in js_files_lists:
pack_js_files(js_files_list["js_files"], js_files_list["output_filename"])
files_to_update = ["src/litegraph.js", "package.json"]
# Update the version number in the specified files
update_version_in_files(get_new_version(), files_to_update)