-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset-npm-shell.py
97 lines (88 loc) · 3.17 KB
/
set-npm-shell.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import re
import subprocess
from os import linesep, path
from sys import platform
from typing import List
DEFAULT_PATH = "C:\\Program Files\\Git\\"
DEFAULT_POWERSHELL_PATH = (
"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
)
CMD_PATH = "C:\\Windows\\System32\\cmd.exe"
def get_git_path() -> str | None:
result: subprocess.CompletedProcess[bytes] = subprocess.run(
["where", "git"], shell=True, capture_output=True
)
if result.returncode == 0:
path_match: re.Match[str] | None = re.match(
r"^[A-Z]:\\.*\\(?!git\.exe$)", result.stdout.decode()
)
if path_match is not None:
return path_match[0].removesuffix(
f"{path.basename(path.dirname(path_match[0]))}\\"
)
else:
if path.exists(DEFAULT_PATH):
return DEFAULT_PATH
return None
def get_pwsh_path() -> str:
result: subprocess.CompletedProcess[bytes] = subprocess.run(
["where", "pwsh"], shell=True, capture_output=True
)
if result.returncode == 0:
path_match: re.Match[str] | None = re.match(
r"^[A-Z]:\\.*\\(?!pwsh\.exe$)", result.stdout.decode()
)
if path_match is not None:
return path_match[0]
else:
if path.exists(DEFAULT_POWERSHELL_PATH):
return DEFAULT_POWERSHELL_PATH
return CMD_PATH
def write_npmrc(npmrc_path: str, lines: List[str]) -> bool:
file_lines: List[str]
if not path.exists(npmrc_path):
with open(npmrc_path, "w", encoding="utf8") as file:
for line in lines:
file.write(f"{line}{linesep}")
with open(npmrc_path, "r", encoding="utf8") as file:
lines.extend([""])
file_lines = [line.strip() for line in file.readlines()]
if file_lines == lines:
return True
else:
with open(npmrc_path, "r", encoding="utf8") as file:
file_lines = [file_line.strip() for file_line in file.readlines()]
with open(npmrc_path, "a", encoding="utf8") as file:
for line in lines:
if line not in file_lines:
file.write(f"{line}{linesep}")
are_set: int = 0
with open(npmrc_path, "r", encoding="utf8") as file:
file_lines = [file_line.strip() for file_line in file.readlines()]
for line in lines:
if line in file_lines:
are_set += 1
if are_set == len(lines):
return True
return False
if __name__ == "__main__":
if platform != "win32":
print("This script is only needed on Windows.")
exit(0)
else:
git_path: str | None = get_git_path()
shell_path: str = (
path.join(git_path, "bin", "sh.exe")
if git_path is not None
else get_pwsh_path()
)
print(f"Shell:\t{shell_path}")
shell_path = shell_path.replace("\\", "\\\\")
print(
"Done!"
if write_npmrc(
npmrc_path=path.join(".", ".npmrc"),
lines=[f'script-shell="{shell_path}"'],
)
else "ERROR: Could not set 'script-shell' in '.npmrc'."
)