-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy
executable file
·63 lines (52 loc) · 2.3 KB
/
deploy
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
#!/usr/bin/env python3
import shutil
import subprocess
import sys
import os
import glob
DIST = './dist'
DESTDIR = '../exphp.github.io/thpages'
BACKUPDIR = '../exphp.github.io/thpages.bak'
if os.path.exists(BACKUPDIR):
shutil.rmtree(BACKUPDIR)
if os.path.exists(DIST):
shutil.rmtree(DIST)
subprocess.run(['npm', 'run', 'build'], check=True)
npm_bin_dir = subprocess.run(['npm', 'bin'], check=True, stdout=subprocess.PIPE).stdout.decode('utf-8').strip()
def make_js_file_git_friendly(path):
# Make terser use semicolons instead of line breaks.
# .terserrc isn't working so we'll do this the hard way.
terser_bin = os.path.join(npm_bin_dir, 'terser')
proc = subprocess.run([terser_bin, '--beautify', 'beautify=false,semicolons=false', path], check=True, stdout=subprocess.PIPE)
open(path, 'wb').write(proc.stdout)
def make_css_file_git_friendly(path):
perfectionist_bin = os.path.join(npm_bin_dir, 'perfectionist')
proc = subprocess.run([perfectionist_bin, '--format', 'compact', path], check=True, stdout=subprocess.PIPE)
open(path, 'wb').write(proc.stdout)
print('Slightly unminifying files for smaller git diffs...')
for path in glob.glob(os.path.join(DIST, '**', '*.js'), recursive=True):
make_js_file_git_friendly(path)
for path in glob.glob(os.path.join(DIST, '**', '*.css'), recursive=True):
make_css_file_git_friendly(path)
def get_hashed_filenames(dir):
out = {}
for name in os.listdir(dir):
parts = name.split('.')
if len(parts) < 3: continue
if all(c in '0123456789abcdefABCDEF' for c in parts[1]):
del parts[1] # hash
out['.'.join(parts)] = name
return out
# git mv files with hashed names to hopefully reduce commit object size
print("Using 'git mv' to update filename hashes...")
dest_hashed = get_hashed_filenames(DESTDIR)
tmp_hashed = get_hashed_filenames(DIST)
for name in dest_hashed:
if name in tmp_hashed and tmp_hashed[name] != dest_hashed[name]:
if not name.endswith('.map'): # source maps are hostile to git due to long lines that change frequently
subprocess.run(['git', 'mv', dest_hashed[name], tmp_hashed[name]], cwd=DESTDIR)
os.rename(DESTDIR, BACKUPDIR)
# copytree is borked on WSL
shutil.move(DIST, DESTDIR)
with open(os.path.join(DESTDIR, '.nojekyll'), 'w') as f:
f.write('Boo!\n')