-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1486 from tgodzik/add-path-back
improvement: Search PATH for Java to use
- Loading branch information
Showing
6 changed files
with
98 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,43 @@ | ||
import { TaskEither } from "fp-ts/lib/TaskEither"; | ||
import { pipe } from "fp-ts/lib/function"; | ||
import * as E from "fp-ts/lib/Either"; | ||
import path from "path"; | ||
import fs from "fs"; | ||
|
||
export function toPromise<E, A>(te: TaskEither<E, A>): Promise<A> { | ||
return te().then( | ||
(res) => | ||
new Promise((resolve, reject) => pipe(res, E.fold(reject, resolve))) | ||
); | ||
} | ||
|
||
export function findOnPath(names: string[]) { | ||
const envPath = process.env["PATH"] || process.env["Path"]; | ||
const isWindows = process.platform === "win32"; | ||
|
||
if (envPath) { | ||
const possibleExecutable = envPath | ||
.split(path.delimiter) | ||
.flatMap((p) => { | ||
try { | ||
if (fs.statSync(p).isDirectory()) { | ||
return fs.readdirSync(p).map((sub) => path.resolve(p, sub)); | ||
} else return [p]; | ||
} catch (e) { | ||
return []; | ||
} | ||
}) | ||
.find((p) => { | ||
if (isWindows) { | ||
return names.some( | ||
(name) => | ||
p.endsWith(path.sep + name + ".bat") || | ||
p.endsWith(path.sep + name + ".exe") | ||
); | ||
} else { | ||
return names.some((name) => p.endsWith(path.sep + name)); | ||
} | ||
}); | ||
return possibleExecutable; | ||
} | ||
} |