@bubblewrap/core
Version:
Core Library to generate, build and sign TWA projects
128 lines (127 loc) • 5.24 kB
JavaScript
/*
* Copyright 2019 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
exports.JdkHelper = void 0;
const fs_1 = require("fs");
const path = require("path");
const util_1 = require("../util");
const Result_1 = require("../Result");
const ValidatePathError_1 = require("../errors/ValidatePathError");
/**
* Helps getting information relevant to the JDK installed, including
* the approprite environment needed to run Java commands on the JDK
*/
class JdkHelper {
/**
* Constructs a new instance of JdkHelper.
*
* @param {NodeJS.Process} process information from the OS process
* @param {Config} config the bubblewrap general configuration
*/
constructor(process, config) {
this.process = process;
this.config = config;
if (process.platform === 'win32') {
this.joinPath = path.win32.join;
this.pathSeparator = ';';
this.pathEnvironmentKey = 'Path';
}
else {
this.joinPath = path.posix.join;
this.pathSeparator = ':';
this.pathEnvironmentKey = 'PATH';
}
}
/**
* Runs the `java` command, passing args as parameters.
*/
async runJava(args) {
const java = this.process.platform === 'win32' ? '/bin/java.exe' : '/bin/java';
const runJavaCmd = this.joinPath(this.getJavaHome(), java);
return await (0, util_1.executeFile)(runJavaCmd, args, this.getEnv());
}
/**
* Returns information from the JAVA_HOME, based on the config and platform.
*/
getJavaHome() {
return JdkHelper.getJavaHome(this.config.jdkPath, this.process);
}
/**
* Returns information from the JAVA_HOME, based on the config and platform.
* @param {Config} config The bubblewrap general configuration
* @param {NodeJS.Process} process Information from the OS process
*/
static getJavaHome(jdkPath, process) {
const joinPath = (process.platform === 'win32') ? path.win32.join : path.posix.join;
if (process.platform === 'darwin') {
return joinPath(jdkPath, '/Contents/Home/');
}
else if (process.platform === 'linux' || process.platform === 'win32') {
return joinPath(jdkPath, '/');
}
throw new Error(`Unsupported Platform: ${process.platform}`);
}
static getJoinPath(process) {
switch (process.platform) {
case 'win32': return path.win32.join;
default: return path.posix.join;
}
}
/**
* Checks if the given jdkPath is valid.
* @param {string} jdkPath the path to the jdk.
*/
static async validatePath(jdkPath, currentProcess = process) {
const join = JdkHelper.getJoinPath(currentProcess);
if (!(0, fs_1.existsSync)(jdkPath)) {
return Result_1.Result.error(new ValidatePathError_1.ValidatePathError(`jdkPath "${jdkPath}" does not exist.`, 'PathIsNotCorrect'));
}
;
const javaHome = JdkHelper.getJavaHome(jdkPath, currentProcess);
try {
const releaseFilePath = join(javaHome, 'release');
const file = await fs_1.promises.readFile(releaseFilePath, 'utf-8');
if (file.indexOf('JAVA_VERSION="17.0') < 0) { // Checks if the jdk's version is 17 as needed
return Result_1.Result.error(new ValidatePathError_1.ValidatePathError('JDK version not supported. JDK version 17 is required.', 'PathIsNotSupported'));
}
}
catch (e) {
return Result_1.Result.error(new ValidatePathError_1.ValidatePathError(`Error reading the "release" file for the JDK at "${jdkPath}", with error: ${e} `, 'PathIsNotCorrect'));
}
return Result_1.Result.ok(jdkPath);
}
/**
* Returns information from the Java executable location, based on the config and platform.
* @returns {string} the value where the Java executables can be found
*/
getJavaBin() {
return this.joinPath(this.getJavaHome(), 'bin/');
}
/**
* Returns a copy of process.env, customized with the correct JAVA_HOME and PATH.
* @returns {NodeJS.ProcessEnv} an env object configure to run JDK commands
*/
getEnv() {
const env = Object.assign({}, this.process.env);
env['JAVA_HOME'] = this.getJavaHome();
// Concatenates the Java binary path to the existing PATH environment variable.
env[this.pathEnvironmentKey] =
this.getJavaBin() + this.pathSeparator + env[this.pathEnvironmentKey];
return env;
}
}
exports.JdkHelper = JdkHelper;