-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembed.py
executable file
·56 lines (43 loc) · 1.75 KB
/
embed.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
#!/bin/env python3
index_filename = "dist/index.html"
# Add more files + arguments for choosing names when this all works
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--inject-data')
parser.add_argument('--fetch-data')
parser.add_argument('--template-url')
args = parser.parse_args()
if (args.inject_data and args.fetch_data):
print("Cannot inject data and fetch it in runtime at the same time, the flags are contradictory.")
import sys
sys.exit(1)
original_snippet = 'let originalFiles = {}'
with open(index_filename, 'r+') as f:
content = f.read()
import json
if args.inject_data:
files = {}
from pathlib import Path
import os
cwd = os.getcwd()
os.chdir(args.inject_data)
p = Path()
for filename in list(p.glob("*.info")) + ['logo.svg', 'config.json', 'sources.txt']:
if os.path.isfile(filename):
files[str(filename)] = open(str(filename), 'r').read()
os.chdir(cwd)
content = content.replace(original_snippet, 'let originalFiles = ' + json.dumps(files))
elif args.fetch_data:
fetch_snippet = 'let originalFiles = {}; let fetchData = "%s";' % args.fetch_data
if args.template_url: fetch_snippet += 'let templatedFetchUrl = "%s";' % args.template_url
content = content.replace(original_snippet, fetch_snippet)
import re
r = r'(<script type="module" crossorigin src="/(assets/index-[\w\-]+.js)"></script>)'
matches = re.findall(r, content)
for m in matches:
with open('dist/' + m[1], 'r') as sf:
scriptText = sf.read()
content = content.replace(m[0], '<script type="module">' + scriptText + '</script>')
f.seek(0)
f.write(content)
f.truncate()