-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathvercel_commands.py
62 lines (56 loc) · 2.01 KB
/
vercel_commands.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
import pathlib
import subprocess
def extract_commands(s):
if "Commands:" not in s:
return []
# Need everything between "Commands:" and "Options:"
text = s.split("Commands:")[1].split("Options:")[0]
# Every line with more than one token in represents a sub-command
# - this excludes the Basic and Advanced headings on "vercel --help"
commands = [
l.strip().split()[0] for l in text.split("\n") if len(l.strip().split()) > 1
]
# vercel login --help starts an interactive process, so skip it
# Skip vercel deploy because it is alias of vercel
return [c for c in commands if c not in ("help", "login", "deploy")]
def fetch_and_save(bits, previous_content=None):
previous_content = previous_content or set()
if bits == ("vercel",):
filename = "index.txt"
dir = pathlib.Path("vercel")
else:
filename = bits[-1] + ".txt"
dir = pathlib.Path("/".join(bits[:-1]))
args = list(bits) + ["--help"]
print(args)
proc = subprocess.run(args, capture_output=True)
s = proc.stderr.decode("utf-8")
if proc.stdout:
# Use that instead, if provided
s = proc.stdout.decode("utf-8")
if s in previous_content:
# Avoid storing duplicates
return None
dir.mkdir(parents=True, exist_ok=True)
print(dir, filename)
(dir / filename).write_text(s, "utf-8")
return s
if __name__ == "__main__":
to_do = [("vercel",)]
done = set()
previous_content = set()
while to_do:
next_bits = to_do.pop()
print(next_bits)
s = fetch_and_save(next_bits, previous_content)
done.add(next_bits)
if s is None:
continue
previous_content.add(s)
# For index page, extract commands and add them to the list
if next_bits == ("vercel",):
for command in extract_commands(s):
path = tuple(list(next_bits) + [command])
print(" ", path)
if path not in done:
to_do.append(path)