Skip to content

Commit

Permalink
simplify APIs
Browse files Browse the repository at this point in the history
Signed-off-by: Yan Zhang <[email protected]>
  • Loading branch information
Eskibear committed Dec 10, 2021
1 parent f1df2b1 commit 4b8f5ae
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 38 deletions.
49 changes: 25 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ export interface IOptions {
* whether to check existence of javac or javac.exe
*/
checkJavac?: boolean;
/**
* whether to include all invalid runtimes, e.g. when JAVA_HOME points to an invalid folder.
*/
fuzzy?: boolean;
/**
* whether to include tags for detailed information
*/
Expand All @@ -51,10 +47,6 @@ export interface IJavaRuntime {
* Version information.
*/
version?: IJavaVersion;
/**
* Whether java or java.exe exists, indicating it's a valid runtime. Only available when `options.fuzzy` provided.
*/
isValid?: boolean;
/**
* Whether javac or javac.exe exists.
*/
Expand Down Expand Up @@ -101,7 +93,7 @@ export async function findRuntimes(options?: IOptions): Promise<IJavaRuntime[]>
// SDKMAN
const fromSdkman = await sdkman.candidates();
updateCandidates(fromSdkman, (r) => ({ ...r, isFromSDKMAN: true }));

// platform-specific default location
if (isLinux) {
updateCandidates(await linux.candidates());
Expand Down Expand Up @@ -145,7 +137,7 @@ export async function findRuntimes(options?: IOptions): Promise<IJavaRuntime[]>
if (true /* always check java binary */) {
runtimes = await Promise.all(runtimes.map(checkJavaFile));
if (true /* java binary is required for a valid runtime */) {
runtimes = runtimes.filter(r => r.isValid);
runtimes = (runtimes as Array<IJavaRuntime & CanValidate>).filter(r => r.isValid);
}
}

Expand All @@ -158,10 +150,8 @@ export async function findRuntimes(options?: IOptions): Promise<IJavaRuntime[]>
}

// clean up private fields by default
if (!options?.fuzzy) {
for (const r of runtimes) {
delete r.isValid;
}
for (const r of runtimes) {
delete (r as CanValidate).isValid;
}

return runtimes;
Expand All @@ -174,11 +164,11 @@ export async function findRuntimes(options?: IOptions): Promise<IJavaRuntime[]>
* @param options
* @returns
*/
export async function getRuntime(homedir: string, options?: IOptions): Promise<IJavaRuntime> {
let runtime : IJavaRuntime= {homedir};
export async function getRuntime(homedir: string, options?: IOptions): Promise<IJavaRuntime | undefined> {
let runtime: IJavaRuntime = { homedir };
runtime = await checkJavaFile(runtime);
if (!runtime.isValid) {
return runtime;
if (!(runtime as CanValidate).isValid) {
return undefined;
}

if (options?.checkJavac) {
Expand All @@ -197,6 +187,10 @@ export async function getRuntime(homedir: string, options?: IOptions): Promise<I
if (sList.includes(homedir)) {
runtime.isFromSDKMAN = true;
}
const jbList = await jabba.candidates();
if (jbList.includes(homedir)) {
runtime.isFromJabba = true;
}
const pList = envs.candidatesFromPath();
if (pList.includes(homedir)) {
runtime.isInPathEnv = true;
Expand Down Expand Up @@ -240,16 +234,16 @@ export function getSources(r: IJavaRuntime): string[] {
return sources;
}

async function checkJavaFile(runtime: IJavaRuntime): Promise<IJavaRuntime> {
async function checkJavaFile(runtime: IJavaRuntime): Promise<IJavaRuntime & CanValidate> {
const { homedir } = runtime;
const binary = path.join(homedir, "bin", JAVA_FILENAME);
try {
await fs.promises.access(binary, fs.constants.F_OK);
runtime.isValid = true;
(runtime as IJavaRuntime & CanValidate).isValid = true;
} catch (error) {
runtime.isValid = false;
(runtime as IJavaRuntime & CanValidate).isValid = false;
}
return runtime;
return runtime as IJavaRuntime & CanValidate;
}

async function checkJavacFile(runtime: IJavaRuntime): Promise<IJavaRuntime> {
Expand Down Expand Up @@ -340,10 +334,10 @@ function parseMajorVersion(version: string): number {

class RuntimeStore {
private map: Map<string, IJavaRuntime> = new Map();
constructor() {}
constructor() { }

public updateRuntimes(homedirs: string[], updater?: (r: IJavaRuntime) => IJavaRuntime) {
for(const h of homedirs) {
for (const h of homedirs) {
this.updateRuntime(h, updater);
}
}
Expand All @@ -357,3 +351,10 @@ class RuntimeStore {
return Array.from(this.map.values());
}
}

interface CanValidate {
/**
* Whether java or java.exe exists, indicating it's a valid runtime.
*/
isValid?: boolean;
}
18 changes: 4 additions & 14 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,14 @@ describe("test this module", () => {
jdks.forEach(jdk => console.log(jdk.homedir, jdk.hasJavac));
});

it("should check java binary with fuzzy search", async () => {
const label = "fuzzy";
it("should list all possible JDKs with version", async () => {
const label = "checkJavac,withVersion";
console.time(label);
const jdks = await utils.findRuntimes({ fuzzy: true });
const jdks = await utils.findRuntimes({ checkJavac: true, withVersion: true });
console.timeEnd(label);
console.log("JDK found: ", jdks.length);
console.log("homedir", "hasJava", "hasJavac", "majorVersion");
jdks.forEach(jdk => console.log(jdk.homedir, jdk.hasJava, jdk.hasJavac, jdk.version?.major));
});

it("should list all possible locations with full-fuzzy search", async () => {
const label = "fuzzy,checkJavac,withVersion";
console.time(label);
const jdks = await utils.findRuntimes({ fuzzy: true, checkJavac: true, withVersion: true });
console.timeEnd(label);
console.log("JDK found: ", jdks.length);
console.log("homedir", "hasJava", "hasJavac", "majorVersion");
jdks.forEach(jdk => console.log(jdk.homedir, jdk.hasJava, jdk.hasJavac, jdk.version?.major));
jdks.forEach(jdk => console.log(jdk.homedir, jdk.hasJavac, jdk.version?.major));
});

it("should list with tags", async () => {
Expand Down

0 comments on commit 4b8f5ae

Please sign in to comment.