-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (41 loc) · 1.24 KB
/
index.js
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
'use strict'
const { lookpath } = require('lookpath')
/**
* Check required executables
* @param config Configuration loaded by cosmiconfig
* @param config.names Names of required executables
* @returns A promise that resolves when requirement is met, rejects for otherwise
*/
async function main (config = {}) {
if (typeof config !== 'object' || !config) {
throw new TypeError('config must be an object')
}
const {
names = []
} = config
if (!(names instanceof Array)) {
throw new TypeError('config.names must be an array')
}
const programDict = {}
const notFoundList = []
for (const executable of names) {
if (typeof executable !== 'string') {
throw new TypeError(`config.names contains non-string member: ${JSON.stringify(executable)}`)
}
const realPath = await lookpath(executable)
if (realPath) {
programDict[executable] = realPath
} else {
notFoundList.push(executable)
}
}
if (notFoundList.length) throw new NotFoundError(notFoundList)
return programDict
}
class NotFoundError extends Error {
constructor (list) {
super(`One or more executables is not found: ${list.join(', ')}`)
this.list = list
}
}
module.exports = Object.assign(main, { NotFoundError })