|
| 1 | +/* |
| 2 | + * Copyright 2000-2025 Vaadin Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package com.vaadin.flow.server.frontend; |
| 17 | + |
| 18 | +import java.io.File; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +/** |
| 23 | + * FrontendTools class fot manually testing NodeJS installation. |
| 24 | + */ |
| 25 | +public class FrontendToolsExecutor { |
| 26 | + |
| 27 | + /** |
| 28 | + * Manual testing utility to demonstrate which Node.js installation will be |
| 29 | + * used. |
| 30 | + * <p> |
| 31 | + * The resolution logic uses any installed Node.js >= minimum supported |
| 32 | + * version (v24.0.0). If no suitable installation exists, it installs the |
| 33 | + * preferred version specified by -DnodeVersion. |
| 34 | + * <p> |
| 35 | + * Usage examples: |
| 36 | + * <ul> |
| 37 | + * <li>Test with global node: {@code mvn exec:java |
| 38 | + * -Dexec.mainClass="com.vaadin.flow.server.frontend.FrontendToolsTest" |
| 39 | + * -Dexec.classpathScope=test}</li> |
| 40 | + * <li>Test forcing alternative: {@code mvn exec:java ... |
| 41 | + * -Dalternative=true}</li> |
| 42 | + * <li>Test with custom preferred version: {@code mvn exec:java ... |
| 43 | + * -DnodeVersion=v24.5.0}</li> |
| 44 | + * </ul> |
| 45 | + * |
| 46 | + * @param args |
| 47 | + * command line arguments (not used) |
| 48 | + */ |
| 49 | + public static void main(String[] args) { |
| 50 | + System.out.println("=".repeat(80)); |
| 51 | + System.out.println("Node.js Resolution Test"); |
| 52 | + System.out.println("=".repeat(80)); |
| 53 | + |
| 54 | + try { |
| 55 | + // Read configuration from system properties |
| 56 | + boolean forceAlternative = Boolean.getBoolean("alternative"); |
| 57 | + String preferredVersion = System.getProperty("nodeVersion", |
| 58 | + FrontendTools.DEFAULT_NODE_VERSION); |
| 59 | + String baseDir = System.getProperty("baseDir", |
| 60 | + System.getProperty("user.dir")); |
| 61 | + |
| 62 | + System.out.println("\nConfiguration:"); |
| 63 | + System.out.println(" Base directory: " + baseDir); |
| 64 | + System.out.println(" Supported version for global: >= " |
| 65 | + + FrontendTools.SUPPORTED_NODE_VERSION.getFullVersion()); |
| 66 | + System.out |
| 67 | + .println(" Minimum auto-installed version (~/.vaadin): >= " |
| 68 | + + FrontendTools.MINIMUM_AUTO_INSTALLED_NODE |
| 69 | + .getFullVersion()); |
| 70 | + System.out.println(" Maximum major version: " |
| 71 | + + FrontendTools.MAX_SUPPORTED_NODE_MAJOR_VERSION); |
| 72 | + System.out.println(" Preferred version (to install if needed): " |
| 73 | + + preferredVersion); |
| 74 | + System.out.println(" Force alternative node: " + forceAlternative); |
| 75 | + System.out.println(); |
| 76 | + |
| 77 | + // Create FrontendTools instance |
| 78 | + FrontendToolsSettings settings = new FrontendToolsSettings(baseDir, |
| 79 | + () -> FrontendUtils.getVaadinHomeDirectory() |
| 80 | + .getAbsolutePath()); |
| 81 | + settings.setNodeVersion(preferredVersion); |
| 82 | + settings.setForceAlternativeNode(forceAlternative); |
| 83 | + |
| 84 | + FrontendTools tools = new FrontendTools(settings); |
| 85 | + |
| 86 | + // Get resolved node information |
| 87 | + String nodeExecutable = tools.getNodeExecutable(); |
| 88 | + String actualVersionUsed = tools.getNodeVersion().getFullVersion(); |
| 89 | + String npmVersion = tools.getNpmVersion().getFullVersion(); |
| 90 | + |
| 91 | + System.out.println("Resolved Node.js installation:"); |
| 92 | + System.out.println(" Node executable: " + nodeExecutable); |
| 93 | + System.out.println(" Actual version used: " + actualVersionUsed); |
| 94 | + System.out.println(" npm version: " + npmVersion); |
| 95 | + |
| 96 | + // Check if using global or alternative installation |
| 97 | + File nodeFile = new File(nodeExecutable); |
| 98 | + boolean isGlobal = !nodeFile.getAbsolutePath() |
| 99 | + .contains(FrontendUtils.getVaadinHomeDirectory().getName()); |
| 100 | + |
| 101 | + System.out.println("\nInstallation type: " |
| 102 | + + (isGlobal ? "GLOBAL" : "ALTERNATIVE (~/.vaadin)")); |
| 103 | + |
| 104 | + if (!isGlobal) { |
| 105 | + System.out.println(" Location: " + FrontendUtils |
| 106 | + .getVaadinHomeDirectory().getAbsolutePath()); |
| 107 | + } |
| 108 | + |
| 109 | + // Try to run node --version to verify it works |
| 110 | + System.out.println("\nVerification:"); |
| 111 | + try { |
| 112 | + List<String> versionCommand = new ArrayList<>(); |
| 113 | + versionCommand.add(nodeExecutable); |
| 114 | + versionCommand.add("--version"); |
| 115 | + FrontendVersion version = FrontendUtils.getVersion("node", |
| 116 | + versionCommand); |
| 117 | + System.out.println(" ✓ Node executable is working"); |
| 118 | + System.out.println( |
| 119 | + " ✓ Verified version: " + version.getFullVersion()); |
| 120 | + } catch (Exception e) { |
| 121 | + System.out.println(" ✗ Failed to verify node executable: " |
| 122 | + + e.getMessage()); |
| 123 | + } |
| 124 | + |
| 125 | + System.out.println("\n" + "=".repeat(80)); |
| 126 | + System.out.println("Resolution completed successfully"); |
| 127 | + System.out.println("=".repeat(80)); |
| 128 | + |
| 129 | + } catch (Exception e) { |
| 130 | + System.err.println("\n" + "=".repeat(80)); |
| 131 | + System.err.println("ERROR: Resolution failed"); |
| 132 | + System.err.println("=".repeat(80)); |
| 133 | + System.err.println("\nException: " + e.getClass().getName()); |
| 134 | + System.err.println("Message: " + e.getMessage()); |
| 135 | + System.err.println("\nStack trace:"); |
| 136 | + e.printStackTrace(System.err); |
| 137 | + System.exit(1); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments