Skip to content

Commit

Permalink
Merge pull request #3 from chenlevy24/newDoctor
Browse files Browse the repository at this point in the history
Bug fixes on Doctor
  • Loading branch information
chenlevy24 committed Aug 25, 2020
2 parents e1a6f2c + d67cbf1 commit dac84bb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
33 changes: 19 additions & 14 deletions packages/cli/src/lib/cmds/doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,49 @@
* limitations under the License.
*/

import {ConsoleLog, Log} from '@bubblewrap/core';
import {ConsoleLog, Log, Config} from '@bubblewrap/core';
import {join} from 'path';
import {existsSync, promises as fsPromises} from 'fs';
import {loadOrCreateConfig} from '../config';
import {enUS as messages} from '../strings';
import {getJavaHome} from '../../../../core/src/lib/jdk/JdkHelper';

async function jdkDoctor(log: Log): Promise<boolean> {
const config = await loadOrCreateConfig();
const jdkPath = config.jdkPath;
const file = await fsPromises.readFile(join(jdkPath, 'release'), 'utf-8');
async function jdkDoctor(config: Config, log: Log): Promise<boolean> {
const jdkPath = getJavaHome(config, process);
// Checks if the path given is valid.
if (!file || !existsSync(jdkPath)) {
if (!existsSync(jdkPath)) {
log.error(messages.jdkPathIsNotCorrect);
return false;
};
if (file.indexOf('JAVA_VERSION="1.8') < 0) { // Checks if the jdk's version is 8 as needed.
log.error(messages.jdkIsNotSupported);
try {
const file = await fsPromises.readFile(join(jdkPath, 'release'), 'utf-8');
if (file.indexOf('JAVA_VERSION="1.8') < 0) { // Checks if the jdk's version is 8 as needed
log.error(messages.jdkIsNotSupported);
return false;
}
} catch (e) {
log.error(messages.jdkPathIsNotCorrect + '\n' + e.message);
return false;
}
return true;
}

async function androidSdkDoctor(log: Log): Promise<boolean> {
const config = await loadOrCreateConfig();
async function androidSdkDoctor(config: Config, log: Log): Promise<boolean> {
const androidSdkPath = config.androidSdkPath;
// Checks if the path given is valid.
if (!existsSync(join(androidSdkPath, 'build-tools')) || !existsSync(androidSdkPath)) {
if (!existsSync(join(androidSdkPath, 'tools')) || !existsSync(androidSdkPath)) {
log.error(messages.androidSdkPathIsNotCorrect);
return false;
};
return true;
}

export async function doctor(log: Log = new ConsoleLog('doctor')): Promise<boolean> {
const jdkResult = await jdkDoctor(log);
const androidSdkResult = await androidSdkDoctor(log);
const config = await loadOrCreateConfig();
const jdkResult = await jdkDoctor(config, log);
const androidSdkResult = await androidSdkDoctor(config, log);
if (jdkResult && androidSdkResult) {
log.info('Your jdkpath and androidSdkPath are valid.');
log.info(messages.bothPathsAreValid);
}
return jdkResult && androidSdkResult;
}
4 changes: 3 additions & 1 deletion packages/cli/src/lib/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type Messages = {
jdkPathIsNotCorrect: string;
jdkIsNotSupported: string;
androidSdkPathIsNotCorrect: string;
bothPathsAreValid: string;
}

export const enUS: Messages = {
Expand Down Expand Up @@ -252,11 +253,12 @@ the PWA:
updateConfigUsage: 'Usage\n\n:[--jdk <path-to-jdk>] [--androidSdk <path-to-android-sdk>]' +
'(You can insert one or both of them)',
jdkPathIsNotCorrect: 'The jdkPath isn\'t correct, please run the following command to update ' +
'it:\nbubblewrap updateConfig --jdkPath path/here, such that the folder "here" contains' +
'it:\nbubblewrap updateConfig --jdkPath <path-to-jdk>, such that the folder "here" contains' +
' the file "release". Then run bubblewrap doctor again.',
jdkIsNotSupported: 'Unsupported jdk version. Please download "OpenJDK 8(LTS)" at the link ' +
'below:\nhttps://adoptopenjdk.net/releases.html?variant=openjdk8&jvmVariant=hotspot.',
androidSdkPathIsNotCorrect: 'The androidSdkPath isn\'t correct, please run the following ' +
'command to update it:\nbubblewrap updateConfig --androidSdkPath <path-to-sdk>, such that ' +
'the folder of the path contains the folder "build-tools". Then run bubblewrap doctor again.',
bothPathsAreValid: 'Your jdkpath and androidSdkPath are valid.',
};
22 changes: 16 additions & 6 deletions packages/core/src/lib/jdk/JdkHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ import {Config} from '../Config';
import * as path from 'path';
import {executeFile} from '../util';

/**
* Returns the Home folder of the jdk.
* @param {Config} config The bubblewrap general configuration
* @param {NodeJS.Process} process Information from the OS process
*/
export function getJavaHome(config: Config, process: NodeJS.Process): string {
const joinPath = (process.platform === 'win32') ? path.win32.join : path.posix.join;
if (process.platform === 'darwin') {
return joinPath(config.jdkPath, '/Contents/Home/');
} else if (process.platform === 'linux' || process.platform === 'win32') {
return joinPath(config.jdkPath, '/');
}
throw new Error(`Unsupported Platform: ${process.platform}`);
}

/**
* Helps getting information relevant to the JDK installed, including
* the approprite environment needed to run Java commands on the JDK
Expand Down Expand Up @@ -65,12 +80,7 @@ export class JdkHelper {
* @returns {string} the value for the JAVA_HOME
*/
getJavaHome(): string {
if (this.process.platform === 'darwin') {
return this.joinPath(this.config.jdkPath, '/Contents/Home/');
} else if (this.process.platform === 'linux' || this.process.platform === 'win32') {
return this.joinPath(this.config.jdkPath, '/');
}
throw new Error(`Unsupported Platform: ${this.process.platform}`);
return getJavaHome(this.config, this.process);
}

/**
Expand Down

0 comments on commit dac84bb

Please sign in to comment.