-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
dev
executable file
·71 lines (59 loc) · 1.92 KB
/
dev
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
#!/usr/bin/env python3
# ./dev --help
import argparse
from http.client import HTTPException
import os
from subprocess import call
import sys
from threading import Thread
import time
from urllib.error import URLError
from urllib.request import urlopen
import webbrowser
parser = argparse.ArgumentParser(prog='./dev')
subparsers = parser.add_subparsers(metavar='<command>', title='commands')
def open_in_browser():
site = 'http://localhost:8080'
while True:
try:
urlopen(site)
webbrowser.open(site)
return
except (ConnectionError, URLError, HTTPException):
time.sleep(1)
def run(port=False):
command = ['docker', 'run', '--init', '-it', '--rm', '-v',
os.getcwd() + ':/src:cached', '-w=/src']
if port:
command += ['-p', '127.0.0.1:8080:8080']
command.append('node:18.16.0-alpine')
return command
def format(args, remaining):
call(run() + ['yarn', 'format'])
parser_format = subparsers.add_parser('format',
help='format the code')
parser_format.set_defaults(func=format)
def lint(args, remaining):
call(run() + ['yarn', 'lint'])
parser_lint = subparsers.add_parser('lint',
help='lint the code')
parser_lint.set_defaults(func=lint)
def start(args, remaining):
Thread(target=open_in_browser).start()
call(run(True) + ['yarn', 'start'])
parser_start = subparsers.add_parser('start',
help='start a development environment')
parser_start.set_defaults(func=start)
def sh(args, remaining):
call(run() + ['sh'])
parser_sh = subparsers.add_parser('sh', help='bring up a shell')
parser_sh.set_defaults(func=sh)
def yarn(args, remaining):
call(run() + ['yarn'] + remaining or [])
parser_yarn = subparsers.add_parser('yarn', help='run a yarn command')
parser_yarn.set_defaults(func=yarn)
if len(sys.argv) > 1:
args, remaining = parser.parse_known_args()
args.func(args, remaining)
else:
parser.print_help()