Class FrontendUtils
java.lang.Object
io.jmix.flowui.devserver.frontend.FrontendUtils
A class for static methods and definitions that might be used in different
locations.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classThrown when the command execution fails.static classThrown when detecting the version of a tool fails. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringFile name of the bootstrap file that is generated in frontendGENERATEDfolder.static final Stringstatic final Stringstatic final StringA key in a Json object for chunks list.static final StringA key in a Json object for css imports data.static final StringPath of the folder containing application frontend source files, it needs to be relative to theDEFAULT_NODE_DIRBy default it is/src/main/frontendin the project folder.static final StringDefault folder for the node related content.static final StringDefault generated path for generated frontend files.static final Stringstatic final StringThe entry-point key used for the exported bundle.static final StringFile name of the feature flags file that is generated in frontendGENERATEDfolder.static final Stringstatic final StringDefault folder used for source and generated folders.static final Stringstatic final StringA special prefix used to map imports placed in theDEFAULT_FRONTEND_DIR.static final StringThe prefix used to import files generated by Flow.static final StringDefault folder for client-side generated files inside the project root frontend folder.static final Stringstatic final Stringstatic final StringThe default directory in frontend directory, where Hilla views are located.static final StringThe TypeScript definitions for theIMPORTS_NAMEfile.static final StringName of the file that contains application imports, javascript, theme and style annotations.static final StringName of the file that contains application imports, javascript, theme and style annotations used when embedding Flow as web-component.static final StringFile name of the index.html in client side.static final StringFile name of the index.js in client side.static final StringFile name of the index.ts in client side.static final StringFile name of the index.tsx in client side.static final StringThe folder inside the 'generated' folder where frontend resources from jars are copied.static final StringThe location where javascript files present in jar resources are copied and can be imported from.static final StringThe location where javascript files present in jar resources are copied and can be imported from, relative to the frontend folder.static final StringA key in a Json object for js modules data.static final StringPath of the old folder containing application frontend source files, it needs to be relative to theDEFAULT_NODE_DIRBy default the old folder is/frontendin the project folder.static final StringLocation for the installed node packages.static final Stringstatic final StringA parameter for overriding theDEFAULT_FRONTEND_DIRfolder.static final StringSet totrueto ignore node/npm tool version checks.static final Stringstatic final Stringstatic final Stringstatic final Stringstatic final StringA parameter informing about the location of theTOKEN_FILE.static final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final StringThe name of the service worker source file for InjectManifest method of the workbox plugin.static final StringThe JavaScript version of the service worker file, for checking if a user has a JavaScript version of a custom service worker file already.static final Stringstatic final Stringstatic final StringFile used to enable npm mode.static final Stringstatic final StringThe name of the vite configuration file.static final StringFile name of Vite helper used in development mode.static final StringThe name of the generated vite configuration file.static final StringFile name of the web component bootstrap file that is generated in frontendGENERATEDfolder.static final StringFile name of the web-component.html in client side.static final String -
Method Summary
Modifier and TypeMethodDescriptionstatic StringcommandToString(String baseDir, List<String> command) Pretty prints a command line order.static voidstatic voidstatic voidIntentionally send to console instead to log, useful when executing external processes.static voidstatic CompletableFuture<com.vaadin.flow.internal.Pair<String,String>> consumeProcessStreams(Process process) Reads input and error stream from the give process asynchronously.static ProcessBuildercreateProcessBuilder(List<String> command) Creates a process builder for the given list of program and arguments.static voiddeleteDirectory(File directory) Recursively delete given directory and contents.static voiddeleteNodeModules(File nodeModules) Try to remove thenode_modulesdirectory, if it exists inside the given base directory.static StringexecuteCommand(List<String> command) Executes a given command as a native process.static intfindFreePort(int rangeStart, int rangeEnd) Get all available client routes in a distinct list of route paths collected from allClientRoutesProviderimplementations found with VaadinLookup.static FilegetFlowGeneratedFolder(File frontendFolder) Gets the folder where Flow generated frontend files are placed.static FilegetFlowGeneratedImports(File frontendFolder) Gets the location of the generated import file for Flow.static FilegetFlowGeneratedWebComponentsFolder(File frontendFolder) Gets the folder where exported web components are generated.static FilegetFlowGeneratedWebComponentsImports(File frontendFolder) Gets the location of the generated import file for exported web components.static InputStreamgetFrontendFileFromDevModeHandler(com.vaadin.flow.server.VaadinService service, String path) Get the contents of a frontend file from the running dev server.static FilegetFrontendGeneratedFolder(File frontendDirectory) static StringgetFrontendServletPath(jakarta.servlet.ServletContext servletContext) Gets the servlet path (excluding the context path) for the servlet used for serving the VAADIN frontend bundle.static StringgetIndexHtmlContent(com.vaadin.flow.server.VaadinService service) Gets the content of thefrontend/index.htmlfile which is served by vite in dev-mode and read from classpath in production mode.static FilegetJarResourcesFolder(File frontendDirectory) Get the front-end resources folder.static StringgetJarResourceString(String jarImport, com.vaadin.flow.server.frontend.scanner.ClassFinder finder) Get resource from JAR package.static FilegetLegacyFrontendFolderIfExists(File projectRoot, File frontendDir) Get the legacy frontend folder if available and new folder doesn't exist.static Filestatic StringGet the Operating System name from theos.namesystem property.static com.vaadin.flow.server.frontend.FrontendVersiongetPackageVersionFromJson(elemental.json.JsonObject sourceJson, String pkg, String versionOrigin) Tries to parse the given package's frontend version or if it doesn't exist, returnsnull.static FilegetProjectBaseDir(com.vaadin.flow.server.startup.ApplicationConfiguration configuration) static FilegetProjectFrontendDir(com.vaadin.flow.server.AbstractConfiguration configuration) Get directory where project's frontend files are located.static URLgetResource(String name) static InputStreamgetResourceAsStream(String name) static StringgetUnixPath(Path source) Get path as a String in Unix form.static StringgetUnixRelativePath(Path source, Path target) Get relative path from a source path to a target path in Unix form.static FileGets vaadin home directory (".vaadin"folder in the user home dir).protected static com.vaadin.flow.server.frontend.FrontendVersiongetVersion(String tool, List<String> versionCommand) static StringgetWebComponentHtmlContent(com.vaadin.flow.server.VaadinService service) Gets the content of thefrontend/web-component.htmlfile which is served by vite in dev-mode and read from classpath in production mode.static booleanisHillaUsed(File frontendDirectory) Checks if Hilla is available and Hilla views are used in the project based on what is in routes.ts or routes.tsx file.static booleanisHillaUsed(File frontendDirectory, com.vaadin.flow.server.frontend.scanner.ClassFinder classFinder) Checks if Hilla is available and Hilla views are used in the project based on what is in routes.ts or routes.tsx file.static booleanisHillaViewsUsed(File frontendDirectory) Auto-detects if hilla views are used in the project based on what is in routes.ts or routes.tsx file.static booleanisReactModuleAvailable(Options options) Is the React module available in the classpath.static booleanisReactRouterRequired(File frontendDirectory) Auto-detects what router is used in a project based on what is imported inINDEX_TSfile.static booleanCheck if the current os is Windows.static voidstatic voidstatic voidstatic com.vaadin.flow.server.frontend.FrontendVersionparseFrontendVersion(String versionString) Parse the version number of node/npm from version output string.static FileresolveFrontendPath(File projectRoot, com.vaadin.flow.function.DeploymentConfiguration deploymentConfiguration, String path) Looks up the frontend resource at the given path.static FileresolveFrontendPath(File projectRoot, String path, File frontendDirectory) Looks up the fronted resource at the given path.static StringstreamToString(InputStream inputStream) Read a stream and copy the content into a String using system line separators for all 'carriage return' characters.
-
Field Details
-
PROJECT_BASEDIR
- See Also:
-
DEFAULT_NODE_DIR
Default folder for the node related content. It's the base directory forConstants.PACKAGE_JSONandNODE_MODULES. By default it's the project root folder.- See Also:
-
NODE_MODULES
Location for the installed node packages. This folder is always considered by node, even though we define extra folders with theNODE_PATH.- See Also:
-
FRONTEND
Default folder used for source and generated folders.- See Also:
-
GENERATED
Default folder for client-side generated files inside the project root frontend folder.- See Also:
-
DEFAULT_FRONTEND_DIR
Path of the folder containing application frontend source files, it needs to be relative to theDEFAULT_NODE_DIRBy default it is/src/main/frontendin the project folder.- See Also:
-
LEGACY_FRONTEND_DIR
Path of the old folder containing application frontend source files, it needs to be relative to theDEFAULT_NODE_DIRBy default the old folder is/frontendin the project folder.- See Also:
-
VITE_CONFIG
The name of the vite configuration file.- See Also:
-
VITE_GENERATED_CONFIG
The name of the generated vite configuration file.- See Also:
-
SERVICE_WORKER_SRC
The name of the service worker source file for InjectManifest method of the workbox plugin.- See Also:
-
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:
-
JAR_RESOURCES_FOLDER
The folder inside the 'generated' folder where frontend resources from jars are copied.- See Also:
-
JAR_RESOURCES_IMPORT
The location where javascript files present in jar resources are copied and can be imported from.- See Also:
-
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
Name of the file that contains application imports, javascript, theme and style annotations.- See Also:
-
IMPORTS_D_TS_NAME
The TypeScript definitions for theIMPORTS_NAMEfile.- See Also:
-
IMPORTS_WEB_COMPONENT_NAME
Name of the file that contains application imports, javascript, theme and style annotations used when embedding Flow as web-component.- See Also:
-
THEME_IMPORTS_D_TS_NAME
- See Also:
-
THEME_IMPORTS_NAME
- See Also:
-
BOOTSTRAP_FILE_NAME
File name of the bootstrap file that is generated in frontendGENERATEDfolder. The bootstrap file is always executed in a Vaadin app.- See Also:
-
WEB_COMPONENT_BOOTSTRAP_FILE_NAME
File name of the web component bootstrap file that is generated in frontendGENERATEDfolder. The bootstrap file is always executed in an exported web component.- See Also:
-
FEATURE_FLAGS_FILE_NAME
File name of the feature flags file that is generated in frontendGENERATEDfolder. 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:
-
INDEX_HTML
File name of the index.html in client side.- See Also:
-
WEB_COMPONENT_HTML
File name of the web-component.html in client side.- See Also:
-
INDEX_TS
File name of the index.ts in client side.- See Also:
-
INDEX_JS
File name of the index.js in client side.- See Also:
-
INDEX_TSX
File name of the index.tsx in client side.- See Also:
-
VITE_DEVMODE_TS
File name of Vite helper used in development mode.- See Also:
-
ROUTES_TS
- See Also:
-
ROUTES_TSX
- See Also:
-
ROUTES_FLOW_TSX
- See Also:
-
ROUTES_JS
- See Also:
-
DEFAULT_PROJECT_FRONTEND_GENERATED_DIR
Default generated path for generated frontend files.- See Also:
-
PARAM_FRONTEND_DIR
A parameter for overriding theDEFAULT_FRONTEND_DIRfolder.- See Also:
-
PARAM_FLOW_FRONTEND_DIR
- See Also:
-
PARAM_THEME_VALUE
- See Also:
-
PARAM_THEME_VARIANT
- See Also:
-
PARAM_THEME_CLASS
- See Also:
-
PARAM_STUDIO_DIR
- See Also:
-
VIEW_DESIGNER_FOLDER
- See Also:
-
FRONTEND_FOLDER
- See Also:
-
BUILD_FOLDER
- See Also:
-
FLOW_FRONTEND_FOLDER
- See Also:
-
GENERATED_FRONTEND_FOLDER
- See Also:
-
PARAM_IGNORE_VERSION_CHECKS
Set totrueto ignore node/npm tool version checks.- See Also:
-
FRONTEND_FOLDER_ALIAS
A special prefix used to map imports placed in theDEFAULT_FRONTEND_DIR. e.g.import 'Frontend/foo.js';references the filefrontend/foo.js.- See Also:
-
FRONTEND_GENERATED_FLOW_IMPORT_PATH
The prefix used to import files generated by Flow.- See Also:
-
HILLA_VIEWS_PATH
The default directory in frontend directory, where Hilla views are located.- See Also:
-
TOKEN_FILE
File used to enable npm mode.- See Also:
-
CHUNKS
A key in a Json object for chunks list.- See Also:
-
EXPORT_CHUNK
The entry-point key used for the exported bundle.- See Also:
-
CSS_IMPORTS
A key in a Json object for css imports data.- See Also:
-
JS_MODULES
A key in a Json object for js modules data.- See Also:
-
PARAM_TOKEN_FILE
A parameter informing about the location of theTOKEN_FILE.- See Also:
-
DISABLE_CHECK
- See Also:
-
YELLOW
- See Also:
-
RED
- See Also:
-
GREEN
- See Also:
-
BRIGHT_BLUE
- See Also:
-
-
Method Details
-
getOsName
Get the Operating System name from theos.namesystem property.- Returns:
- operating system name
-
isWindows
public static boolean isWindows()Check if the current os is Windows.- Returns:
- true if windows
-
streamToString
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
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
-
getIndexHtmlContent
public static String getIndexHtmlContent(com.vaadin.flow.server.VaadinService service) throws IOException Gets the content of thefrontend/index.htmlfile which is served by 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 in the `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(com.vaadin.flow.server.VaadinService service) throws IOException Gets the content of thefrontend/web-component.htmlfile which is served by 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 in the `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(com.vaadin.flow.server.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, com.vaadin.flow.function.DeploymentConfiguration deploymentConfiguration, String path) Looks up the frontend resource at the given path. If the path starts with./, first look in "./src/main/frontend/", then in "jar-resources". If the path does not start with./, look innode_modulesinstead.- Parameters:
projectRoot- the project root folder.deploymentConfiguration- the active deployment configurationpath- the file path.- Returns:
- an existing
File, or null if the file doesn't exist.
-
getLegacyFrontendFolderIfExists
Get the legacy frontend folder if available and new folder doesn't exist.- Parameters:
projectRoot- project's root directoryfrontendDir- the frontend directory location from project's configuration- Returns:
- correct folder or legacy folder if not user defined
-
resolveFrontendPath
Looks up the fronted resource at the given path. If the path starts with./, first look infrontend, then in "jar-resources". If the path does not start with./, look innode_modulesinstead.- 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.
-
getJarResourceString
public static String getJarResourceString(String jarImport, com.vaadin.flow.server.frontend.scanner.ClassFinder finder) Get resource from JAR package.- Parameters:
jarImport- jar file to get (no resource folder should be added)- Returns:
- resource as String or
nullif not found
-
getJarResourcesFolder
Get the front-end resources folder. This is where the contents of JAR dependencies are copied to.- Parameters:
frontendDirectory- project's frontend directory- Returns:
- a
Filerepresenting a folder with copied resources
-
getFrontendGeneratedFolder
-
getProjectFrontendDir
public static File getProjectFrontendDir(com.vaadin.flow.server.AbstractConfiguration configuration) Get directory where project's frontend files are located.- Parameters:
configuration- the current deployment configuration- Returns:
DEFAULT_FRONTEND_DIRor value ofPARAM_FRONTEND_DIRif it is set.
-
getUnixRelativePath
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 pathtarget- the target path- Returns:
- unix relative path from source to target
-
getUnixPath
Get path as a String in Unix form.- Parameters:
source- path to get- Returns:
- path as a String in Unix form.
-
getVersion
protected static com.vaadin.flow.server.frontend.FrontendVersion getVersion(String tool, List<String> versionCommand) throws FrontendUtils.UnknownVersionException -
executeCommand
public static String executeCommand(List<String> command) throws FrontendUtils.CommandExecutionException Executes a given command as a native process.- Parameters:
command- the command to be executed and it's arguments.- Returns:
- process output string.
- Throws:
FrontendUtils.CommandExecutionException- if the process completes exceptionally.
-
consumeProcessStreams
public static CompletableFuture<com.vaadin.flow.internal.Pair<String,String>> consumeProcessStreams(Process process) Reads input and error stream from the give process asynchronously. The method returns aCompletableFuturethat is completed when both the streams are consumed. Streams are converted into strings and wrapped into aPair, mapping input stream intoPair.getFirst()and error stream intoPair.getSecond(). This method should be mainly used to avoid thatProcess.waitFor()hangs indefinitely on some operating systems because process streams are not consumed. See https://github.com/vaadin/flow/issues/15339 for an example case.- Parameters:
process- the process whose streams should be read- Returns:
- a
CompletableFuturethat return the string contents of the process input and error streams when both are consumed, wrapped into aPair.
-
parseFrontendVersion
public static com.vaadin.flow.server.frontend.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 bytool --version- Returns:
- FrontendVersion of versionString
- Throws:
IOException- if parsing fails
-
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
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 directorycommand- the command and it's arguments- Returns:
- the string for printing in logs
-
getPackageVersionFromJson
public static com.vaadin.flow.server.frontend.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, returnsnull. In case the value cannot be parsed, logs an error and returnsnull.- Parameters:
sourceJson- json object that has the packagepkg- the package nameversionOrigin- origin of the version (like a file), used in error message- Returns:
- the frontend version the package or
null
-
console
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 messagemessage- the string to show
-
console
- See Also:
-
console
-
console
-
logInFile
public static void logInFile(String text, boolean createLogFileIfNotExist, boolean throwExceptionIfLogFileNotExist) -
logInFile
-
logInFile
-
getLogFile
-
deleteNodeModules
Try to remove thenode_modulesdirectory, 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- thenode_modulesdirectory- Throws:
IOException- on failure to delete any one file, or if the directory name is notnode_modules
-
deleteDirectory
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
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
-
getFlowGeneratedFolder
Gets the folder where Flow generated frontend files are placed.- Parameters:
frontendFolder- the project frontend folder- Returns:
- the folder for Flow generated files
-
getFlowGeneratedImports
Gets the location of the generated import file for Flow.- Parameters:
frontendFolder- the project frontend folder- Returns:
- the location of the generated import JS file
-
getFlowGeneratedWebComponentsImports
Gets the location of the generated import file for exported web components.- Parameters:
frontendFolder- the project frontend folder- Returns:
- the location of the generated import JS file for exported web components
-
getFlowGeneratedWebComponentsFolder
Gets the folder where exported web components are generated.- Parameters:
frontendFolder- the project frontend folder- Returns:
- the exported web components folder
-
isReactRouterRequired
Auto-detects what router is used in a project based on what is imported inINDEX_TSfile.- Parameters:
frontendDirectory- path to the frontend folder in a project.- Returns:
falseif vaadin-router is used,trueotherwise.
-
isHillaViewsUsed
Auto-detects if hilla views are used in the project based on what is in routes.ts or routes.tsx file.getProjectFrontendDir(AbstractConfiguration)can be used to get the frontend directory.- Parameters:
frontendDirectory- Target frontend directory.- Returns:
trueif hilla views are used,falseotherwise.
-
isHillaUsed
Checks if Hilla is available and Hilla views are used in the project based on what is in routes.ts or routes.tsx file.getProjectFrontendDir(AbstractConfiguration)can be used to get the frontend directory.- Returns:
trueif Hilla is available and Hilla views are used,falseotherwise
-
isHillaUsed
public static boolean isHillaUsed(File frontendDirectory, com.vaadin.flow.server.frontend.scanner.ClassFinder classFinder) Checks if Hilla is available and Hilla views are used in the project based on what is in routes.ts or routes.tsx file.getProjectFrontendDir(AbstractConfiguration)can be used to get the frontend directory. Given class finder is used to check the presence of Hilla in a classpath.- Parameters:
classFinder- class finder to check the presence of Hilla endpoint class- Returns:
trueif Hilla is available and Hilla views are used,falseotherwise
-
isReactModuleAvailable
Is the React module available in the classpath.- Returns:
- true if the React module is available, false otherwise
-
getClientRoutes
Get all available client routes in a distinct list of route paths collected from allClientRoutesProviderimplementations found with VaadinLookup.- Returns:
- a list of available client routes
-
getResourceAsStream
-
getResource
-
findFreePort
public static int findFreePort(int rangeStart, int rangeEnd) -
getProjectBaseDir
public static File getProjectBaseDir(com.vaadin.flow.server.startup.ApplicationConfiguration configuration)
-