MCPcopy
hub / github.com/vercel/hyper / addBinToUserPath

Function addBinToUserPath

app/utils/cli-install.ts:75–122  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

73};
74
75const addBinToUserPath = () => {
76 return new Promise<void>((resolve, reject) => {
77 try {
78 const envKey = Registry.openKey(Registry.HKCU, 'Environment', Registry.Access.ALL_ACCESS)!;
79
80 // C:\Users\<user>\AppData\Local\Programs\hyper\resources\bin
81 const binPath = path.dirname(cliScriptPath);
82 // C:\Users\<user>\AppData\Local\hyper
83 const oldPath = path.resolve(process.env.LOCALAPPDATA!, 'hyper');
84
85 const items = Registry.enumValueNames(envKey);
86 const pathItem = items.find((item) => item.toUpperCase() === 'PATH');
87 const pathItemName = pathItem || 'PATH';
88
89 let newPathValue = binPath;
90 let type: ValueType = Registry.ValueType.SZ;
91 if (pathItem) {
92 type = Registry.queryValueRaw(envKey, pathItem)!.type;
93 if (type !== Registry.ValueType.SZ && type !== Registry.ValueType.EXPAND_SZ) {
94 reject(`Registry key type is ${type}`);
95 return;
96 }
97 const value = Registry.queryValue(envKey, pathItem) as string;
98 let pathParts = value.split(';');
99 const existingPath = pathParts.includes(binPath);
100 const existingOldPath = pathParts.some((pathPart) => pathPart.startsWith(oldPath));
101 if (existingPath && !existingOldPath) {
102 console.log('Hyper CLI already in PATH');
103 Registry.closeKey(envKey);
104 resolve();
105 return;
106 }
107
108 // Because nsis install path is different from squirrel we need to remove old path if present
109 // and add current path if absent
110 if (existingOldPath) pathParts = pathParts.filter((pathPart) => !pathPart.startsWith(oldPath));
111 if (!pathParts.includes(binPath)) pathParts.push(binPath);
112 newPathValue = pathParts.join(';');
113 }
114 console.log('Adding HyperCLI path (registry)');
115 Registry.setValueRaw(envKey, pathItemName, type, Registry.formatString(newPathValue));
116 Registry.closeKey(envKey);
117 resolve();
118 } catch (error) {
119 reject(error);
120 }
121 });
122};
123
124const logNotify = (withNotification: boolean, title: string, body: string, details?: {error?: any}) => {
125 console.log(title, body, details);

Callers 1

installCLIFunction · 0.85

Calls 7

rejectFunction · 0.85
resolveFunction · 0.85
filterMethod · 0.80
resolveMethod · 0.45
findMethod · 0.45
logMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected