-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathpath-check.ts
More file actions
41 lines (37 loc) · 1.35 KB
/
path-check.ts
File metadata and controls
41 lines (37 loc) · 1.35 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
33
34
35
36
37
38
39
40
41
import { spawn } from 'child_process';
/**
* This type is used to bypass the `defaultImpl` when running the tests.
*/
export type ExecForCode = {
run: (execName: string, args: Array<string>, cwd: string) => Promise<number>;
};
const defaultImpl: ExecForCode = {
run: (execName: string, args: Array<string>, cwd: string) => {
return new Promise((resolve, reject) => {
const options = { cwd };
const resolveProcess = spawn(execName, args, options);
resolveProcess.on('exit', (exitCode) => {
resolve(exitCode);
});
resolveProcess.on('error', (err) => {
reject(err);
});
});
},
};
export function findCoursierOnPath(cwd: string, execForCode: ExecForCode = defaultImpl): Promise<Array<string>> {
function availableOnPath(execName: string): Promise<boolean> {
return execForCode
.run(execName, ['--help'], cwd)
.then((ec) => ec === 0)
.catch(() => false);
}
const possibleCoursierNames = ['cs', 'coursier'];
return possibleCoursierNames.reduce((accP, current) => {
return accP.then((acc) => {
return availableOnPath(current).then((succeeeded) => {
return succeeeded ? [...acc, current] : acc;
});
});
}, Promise.resolve([]));
}