/
buildHelper.py
57 lines (44 loc) · 2.02 KB
/
buildHelper.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
import shutil
import subprocess
import os
final = 'index.html'
temp = 'temp.html'
#Before building, delete 'dist' folder and tags to the 'dist' folder inside the main 'index.html'
shutil.rmtree('dist')
ignored_lines = [7, 12, 13, 14]
with open(final, 'r') as fin, open(temp, 'w') as fout:
for lineno, line in enumerate(fin, 1):
if lineno not in ignored_lines:
fout.write(line)
subprocess.run('npm run build', shell=True)
#Copy all link and script tags in 'dist/index.html' and paste them into the main 'index.html'
#add 'dist/' at the beginning of the 'src' in all of the pasted tags
distfile = 'dist/index.html'
with open(distfile, 'r') as fin:
content = fin.read()
start = content.index('<link'); print(start)
end = content.index('stylesheet>') + 11; print(end)
link = content[start:end].replace('=/static','=dist/static'); print(link)
start = content.index('<script'); print(start)
end = content.index('</script>') + 9; print(end)
script1 = content[start:end].replace('=/static','=dist/static'); print(script1)
start = content.replace('<script','XXXXXXX', 1).find('<script'); print(start)
end = content.replace('</script>','XXXXXXXXX', 1).find('</script>') + 9; print(end)
script2 = content[start:end].replace('=/static','=dist/static'); print(script2)
start = content.replace('<script','XXXXXXX', 2).find('<script'); print(start)
end = content.replace('</script>','XXXXXXXXX', 2).find('</script>') + 9; print(end)
script3 = content[start:end].replace('=/static','=dist/static'); print(script3)
copyfrom = 'index.html'
with open(copyfrom, 'r') as readfile:
content = readfile.readlines()
content.insert(6, ' ' + link + '\n');
content.insert(11, ' ' + script1 + '\n');
content.insert(12, ' ' + script2 + '\n');
content.insert(13, ' ' + script3 + '\n');
print('\n'.join(content))
with open(temp, 'w') as writefile:
content = ''.join(content)
writefile.write(content)
#replace the old 'index.html' with the new file
os.remove(final)
os.rename(temp, final)