forked from ioBroker/ioBroker.javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodeModulesManagement.ts
More file actions
32 lines (30 loc) · 1.09 KB
/
nodeModulesManagement.ts
File metadata and controls
32 lines (30 loc) · 1.09 KB
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
import { execFile, type ExecFileException } from 'node:child_process';
/**
* Request a module name by given url using `npm view`
*
* @param url the url to the package which should be installed via npm
*/
export async function requestModuleNameByUrl(url: string): Promise<string> {
return new Promise((resolve, reject) => {
execFile(
'npm',
['view', url, 'name'],
{ windowsHide: true, encoding: 'utf8', shell: false },
(error: ExecFileException | null, stdout: string) => {
if (error) {
reject(error as Error);
} else {
if (typeof stdout !== 'string') {
reject(
new Error(
`Could not determine module name for url "${url}". Unexpected stdout: "${stdout ? JSON.stringify(stdout) : ''}"`,
),
);
return;
}
resolve(stdout.trim());
}
},
);
});
}