Class FrontendUtils


  • public class FrontendUtils
    extends Object
    A class for static methods and definitions that might be used in different locations.

    For internal use only. May be renamed or removed in a future release.

    Since:
    2.0
    • Field Detail

      • NODE_MODULES

        public static final String NODE_MODULES
        Location for the installed node packages. This folder is always considered by node, even though we define extra folders with the NODE_PATH.
        See Also:
        Constant Field Values
      • GENERATED

        public static final String GENERATED
        Default folder for client-side generated files inside the project root frontend folder.
        See Also:
        Constant Field Values
      • DEFAULT_FRONTEND_DIR

        public static final String DEFAULT_FRONTEND_DIR
        Path of the folder containing application frontend source files, it needs to be relative to the DEFAULT_NODE_DIR By default it is /frontend in the project folder.
        See Also:
        Constant Field Values
      • WEBPACK_CONFIG

        public static final String WEBPACK_CONFIG
        The name of the webpack configuration file.
        See Also:
        Constant Field Values
      • VITE_GENERATED_CONFIG

        public static final String VITE_GENERATED_CONFIG
        The name of the generated vite configuration file.
        See Also:
        Constant Field Values
      • WEBPACK_GENERATED

        public static final String WEBPACK_GENERATED
        The name of the webpack generated configuration file.
        See Also:
        Constant Field Values
      • SERVICE_WORKER_SRC

        public static final String SERVICE_WORKER_SRC
        The name of the service worker source file for InjectManifest method of workbox-webpack-plugin.
        See Also:
        Constant Field Values
      • SERVICE_WORKER_SRC_JS

        public static final String SERVICE_WORKER_SRC_JS
        The JavaScript version of the service worker file, for checking if a user has a JavaScript version of a custom service worker file already.
        See Also:
        Constant Field Values
      • JAR_RESOURCES_FOLDER

        public static final String JAR_RESOURCES_FOLDER
        The folder inside the 'generated' folder where frontend resources from jars are copied.
        See Also:
        Constant Field Values
      • JAR_RESOURCES_IMPORT

        public static final String JAR_RESOURCES_IMPORT
        The location where javascript files present in jar resources are copied and can be imported from.
        See Also:
        Constant Field Values
      • JAR_RESOURCES_IMPORT_FRONTEND_RELATIVE

        public static final String JAR_RESOURCES_IMPORT_FRONTEND_RELATIVE
        The location where javascript files present in jar resources are copied and can be imported from, relative to the frontend folder.
      • IMPORTS_NAME

        public static final String IMPORTS_NAME
        Name of the file that contains application imports, javascript, theme and style annotations. It is also the entry-point for webpack. It is always generated in the DEFAULT_GENERATED_DIR folder.
        See Also:
        Constant Field Values
      • BOOTSTRAP_FILE_NAME

        public static final String BOOTSTRAP_FILE_NAME
        File name of the bootstrap file that is generated in frontend GENERATED folder. The bootstrap file is always executed in a Vaadin app.
        See Also:
        Constant Field Values
      • WEB_COMPONENT_BOOTSTRAP_FILE_NAME

        public static final String WEB_COMPONENT_BOOTSTRAP_FILE_NAME
        File name of the web component bootstrap file that is generated in frontend GENERATED folder. The bootstrap file is always executed in an exported web component.
        See Also:
        Constant Field Values
      • FEATURE_FLAGS_FILE_NAME

        public static final String FEATURE_FLAGS_FILE_NAME
        File name of the feature flags file that is generated in frontend GENERATED folder. The feature flags file contains code to define feature flags as globals that might be used by Vaadin web components or application code.
        See Also:
        Constant Field Values
      • WEB_COMPONENT_HTML

        public static final String WEB_COMPONENT_HTML
        File name of the web-component.html in client side.
        See Also:
        Constant Field Values
      • VITE_DEVMODE_TS

        public static final String VITE_DEVMODE_TS
        File name of Vite helper used in development mode.
        See Also:
        Constant Field Values
      • DEFAULT_CONNECT_JAVA_SOURCE_FOLDER

        public static final String DEFAULT_CONNECT_JAVA_SOURCE_FOLDER
        Default Java source folder for OpenAPI generator.
        See Also:
        Constant Field Values
      • DEFAULT_CONNECT_APPLICATION_PROPERTIES

        public static final String DEFAULT_CONNECT_APPLICATION_PROPERTIES
        Default application properties file path in Vaadin project.
        See Also:
        Constant Field Values
      • DEFAULT_CONNECT_OPENAPI_JSON_FILE

        public static final String DEFAULT_CONNECT_OPENAPI_JSON_FILE
        Default generated path for OpenAPI spec file.
        See Also:
        Constant Field Values
      • DEFAULT_PROJECT_FRONTEND_GENERATED_DIR

        public static final String DEFAULT_PROJECT_FRONTEND_GENERATED_DIR
        Default generated path for generated frontend files.
        See Also:
        Constant Field Values
      • FALLBACK_IMPORTS_NAME

        public static final String FALLBACK_IMPORTS_NAME
        Name of the file that contains all application imports, javascript, theme and style annotations which are not discovered by the current scanning strategy (but they are in the project classpath). This file is dynamically imported by the IMPORTS_NAME file. It is always generated in the DEFAULT_GENERATED_DIR folder.
        See Also:
        Constant Field Values
      • PARAM_IGNORE_VERSION_CHECKS

        public static final String PARAM_IGNORE_VERSION_CHECKS
        Set to true to ignore node/npm tool version checks.
        See Also:
        Constant Field Values
      • FRONTEND_FOLDER_ALIAS

        public static final String FRONTEND_FOLDER_ALIAS
        A special prefix used to map imports placed in the DEFAULT_FRONTEND_DIR. e.g. import 'Frontend/foo.js'; references the filefrontend/foo.js.
        See Also:
        Constant Field Values
      • EXPORT_CHUNK

        public static final String EXPORT_CHUNK
        The entry-point key used for the exported bundle.
        See Also:
        Constant Field Values
    • Method Detail

      • getOsName

        public static String getOsName()
        Get the Operating System name from the os.name system property.
        Returns:
        operating system name
      • isWindows

        public static boolean isWindows()
        Check if the current os is Windows.
        Returns:
        true if windows
      • streamToString

        public static String streamToString​(InputStream inputStream)
        Read a stream and copy the content into a String using system line separators for all 'carriage return' characters.
        Parameters:
        inputStream - the input stream
        Returns:
        the string
      • createProcessBuilder

        public static ProcessBuilder createProcessBuilder​(List<String> command)
        Creates a process builder for the given list of program and arguments. If the program is defined as an absolute path, then the directory that contains the program is also appended to PATH so that the it can locate related tools.
        Parameters:
        command - a list with the program and arguments
        Returns:
        a configured process builder
      • clearCachedStatsContent

        public static void clearCachedStatsContent​(VaadinService service)
        Clears the stats.json cache within this VaadinContext.
        Parameters:
        service - the vaadin service.
      • getIndexHtmlContent

        public static String getIndexHtmlContent​(VaadinService service)
                                          throws IOException
        Gets the content of the frontend/index.html file which is served by webpack or vite in dev-mode and read from classpath in production mode.

        NOTE: In dev mode, the file content is fetched using an http request so that we don't need to have a separate index.html's content watcher. Auto-reloading will work automatically, like other files managed by webpack in `frontend/` folder.

        Parameters:
        service - the vaadin service
        Returns:
        the content of the index html file as a string, null if not found.
        Throws:
        IOException - on error when reading file
      • getWebComponentHtmlContent

        public static String getWebComponentHtmlContent​(VaadinService service)
                                                 throws IOException
        Gets the content of the frontend/web-component.html file which is served by webpack or vite in dev-mode and read from classpath in production mode.

        NOTE: In dev mode, the file content is fetched using an http request so that we don't need to have a separate web-component.html's content watcher. Auto-reloading will work automatically, like other files managed by webpack in `frontend/` folder.

        Parameters:
        service - the vaadin service
        Returns:
        the content of the web-component.html file as a string, null if not found.
        Throws:
        IOException - on error when reading file
      • getFrontendFileFromDevModeHandler

        public static InputStream getFrontendFileFromDevModeHandler​(VaadinService service,
                                                                    String path)
        Get the contents of a frontend file from the running dev server.
        Parameters:
        service - the Vaadin service.
        path - the file path.
        Returns:
        an input stream for reading the file contents; null if there is no such file or the dev server is not running.
      • resolveFrontendPath

        public static File resolveFrontendPath​(File projectRoot,
                                               String path)
        Looks up the frontend resource at the given path. If the path starts with ./, first look in frontend, then in "jar-resources". If the path does not start with ./, look in node_modules instead.
        Parameters:
        projectRoot - the project root folder.
        path - the file path.
        Returns:
        an existing File , or null if the file doesn't exist.
      • resolveFrontendPath

        public static File resolveFrontendPath​(File projectRoot,
                                               String path,
                                               File frontendDirectory)
        Looks up the fronted resource at the given path. If the path starts with ./, first look in frontend, then in "jar-resources". If the path does not start with ./, look in node_modules instead.
        Parameters:
        projectRoot - the project root folder.
        path - the file path.
        frontendDirectory - the frontend directory.
        Returns:
        an existing File , or null if the file doesn't exist.
      • getStatsAssetsByChunkName

        public static String getStatsAssetsByChunkName​(VaadinService service)
                                                throws IOException
        Load the asset chunks from stats.json. We will only read the file until we have reached the assetsByChunkName json and return that as a json object string. Note: The stats.json is cached when external stats is enabled or stats.json is provided from the class path. To clear the cache use clearCachedStatsContent(VaadinService).
        Parameters:
        service - the Vaadin service.
        Returns:
        json for assetsByChunkName object in stats.json or null if stats.json not found or content not found.
        Throws:
        IOException - if an I/O error occurs while creating the input stream.
      • isWebpackConfigFile

        public static boolean isWebpackConfigFile​(File file)
                                           throws IOException
        Checks whether the file is a webpack configuration file with the expected content (includes a configuration generated by Flow).
        Parameters:
        file - a file to check
        Returns:
        true iff the file exists and includes a generated configuration
        Throws:
        IOException - if an I/O error occurs while reading the file
      • getUnixRelativePath

        public static String getUnixRelativePath​(Path source,
                                                 Path target)
        Get relative path from a source path to a target path in Unix form. All the Windows' path separator will be replaced.
        Parameters:
        source - the source path
        target - the target path
        Returns:
        unix relative path from source to target
      • getUnixPath

        public static String getUnixPath​(Path source)
        Get path as a String in Unix form.
        Parameters:
        source - path to get
        Returns:
        path as a String in Unix form.
      • readFallbackChunk

        public static FallbackChunk readFallbackChunk​(elemental.json.JsonObject object)
        Read fallback chunk data from a json object.
        Parameters:
        object - json object to read fallback chunk data
        Returns:
        a fallback chunk data
      • parseFrontendVersion

        public static FrontendVersion parseFrontendVersion​(String versionString)
                                                    throws IOException
        Parse the version number of node/npm from version output string.
        Parameters:
        versionString - string containing version output, typically produced by tool --version
        Returns:
        FrontendVersion of versionString
        Throws:
        IOException - if parsing fails
      • getVaadinHomeDirectory

        public static File getVaadinHomeDirectory()
        Gets vaadin home directory (".vaadin" folder in the user home dir).

        The directory is created if it's doesn't exist.

        Returns:
        a vaadin home directory
      • commandToString

        public static String commandToString​(String baseDir,
                                             List<String> command)
        Pretty prints a command line order. It split in lines adapting to 80 columns, and allowing copy and paste in console. It also removes the current directory to avoid security issues in log files.
        Parameters:
        baseDir - the current directory
        command - the command and it's arguments
        Returns:
        the string for printing in logs
      • getPackageVersionFromJson

        public static FrontendVersion getPackageVersionFromJson​(elemental.json.JsonObject sourceJson,
                                                                String pkg,
                                                                String versionOrigin)
        Tries to parse the given package's frontend version or if it doesn't exist, returns null. In case the value cannot be parsed, logs an error and returns null.
        Parameters:
        sourceJson - json object that has the package
        pkg - the package name
        versionOrigin - origin of the version (like a file), used in error message
        Returns:
        the frontend version the package or null
      • console

        public static void console​(String format,
                                   Object message)
        Intentionally send to console instead to log, useful when executing external processes.
        Parameters:
        format - Format of the line to send to console, it must contain a `%s` outlet for the message
        message - the string to show
      • deleteNodeModules

        public static void deleteNodeModules​(File nodeModules)
                                      throws IOException
        Try to remove the node_modules directory, if it exists inside the given base directory. Note that pnpm uses symlinks internally, so delete utilities that follow symlinks when deleting and/or modifying permissions may not work as intended.
        Parameters:
        nodeModules - the node_modules directory
        Throws:
        IOException - on failure to delete any one file, or if the directory name is not node_modules
      • deleteDirectory

        public static void deleteDirectory​(File directory)
                                    throws IOException
        Recursively delete given directory and contents.

        Will not delete contents of symlink or junction directories, only the link file.

        Parameters:
        directory - directory to delete
        Throws:
        IOException - on failure to delete or read any one file
      • getFrontendServletPath

        public static String getFrontendServletPath​(javax.servlet.ServletContext servletContext)
        Gets the servlet path (excluding the context path) for the servlet used for serving the VAADIN frontend bundle.
        Returns:
        the path to the servlet used for the frontend bundle. Empty for a /* mapping, otherwise always starts with a slash but never ends with a slash