Class FrontendUtils
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Thrown when the command execution fails.static class
Thrown when detecting the version of a tool fails. -
Field Summary
Modifier and TypeFieldDescriptionstatic final String
File name of the bootstrap file that is generated in frontendGENERATED
folder.static final String
static final String
static final String
static final String
A key in a Json object for chunks list.static final String
A key in a Json object for css imports data.static final String
Path of the folder containing application frontend source files, it needs to be relative to theDEFAULT_NODE_DIR
static final String
Default folder name for flow generated stuff relative to theInitParameters.BUILD_FOLDER
.static final String
Default folder for the node related content.static final String
Default generated path for generated frontend files.static final String
static final String
The entry-point key used for the exported bundle.static final String
A key in a Json object for fallback chunk.static final String
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).static final String
File name of the feature flags file that is generated in frontendGENERATED
folder.static final String
static final String
Default folder used for source and generated folders.static final String
static final String
A special prefix used to map imports placed in theDEFAULT_FRONTEND_DIR
.static final String
Default folder for client-side generated files inside the project root frontend folder.static final String
static final String
static final String
The TypeScript definitions for theIMPORTS_NAME
file.static final String
Name of the file that contains application imports, javascript, theme and style annotations.static final String
File name of the index.html in client side.static final String
File name of the index.js in client side.static final String
File name of the index.ts in client side.static final String
File name of the index.tsx in client side.static final String
The folder inside the 'generated' folder where frontend resources from jars are copied.static final String
The location where javascript files present in jar resources are copied and can be imported from.static final String
The location where javascript files present in jar resources are copied and can be imported from, relative to the frontend folder.static final String
A key in a Json object for js modules data.static final String
Location for the installed node packages.static final String
static final String
A parameter for overriding theDEFAULT_FRONTEND_DIR
folder.static final String
A parameter for overriding theDEFAULT_GENERATED_DIR
folder.static final String
Set totrue
to ignore node/npm tool version checks.static final String
static final String
static final String
static final String
static final String
A parameter informing about the location of theTOKEN_FILE
.static final String
static final String
static final String
The name of the service worker source file for InjectManifest method of the workbox plugin.static final String
The 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 String
static final String
static final String
File used to enable npm mode.static final String
static final String
The name of the vite configuration file.static final String
File name of Vite helper used in development mode.static final String
The name of the generated vite configuration file.static final String
File name of the web component bootstrap file that is generated in frontendGENERATED
folder.static final String
File name of the web-component.html in client side.static final String
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
commandToString
(String baseDir, List<String> command) Pretty prints a command line order.static void
static void
Intentionally send to console instead to log, useful when executing external processes.static void
static ProcessBuilder
createProcessBuilder
(List<String> command) Creates a process builder for the given list of program and arguments.static void
deleteDirectory
(File directory) Recursively delete given directory and contents.static void
deleteNodeModules
(File nodeModules) Try to remove thenode_modules
directory, if it exists inside the given base directory.static String
executeCommand
(List<String> command) Executes a given command as a native process.static URL
findBundleFile
(File projectDir, String filename) Finds the given file inside the express mode development bundle that is used.static String
findBundleStatsJson
(File projectDir) Get the stats.json for the application specific development bundle.static int
findFreePort
(int rangeStart, int rangeEnd) static File
getDevBundleFolder
(File projectDir) Get the folder where an application specific bundle is stored.static InputStream
getFrontendFileFromDevModeHandler
(com.vaadin.flow.server.VaadinService service, String path) Get the contents of a frontend file from the running dev server.static String
getFrontendServletPath
(jakarta.servlet.ServletContext servletContext) Gets the servlet path (excluding the context path) for the servlet used for serving the VAADIN frontend bundle.static String
getIndexHtmlContent
(com.vaadin.flow.server.VaadinService service) Gets the content of thefrontend/index.html
file which is served by webpack or vite in dev-mode and read from classpath in production mode.static File
getJarResourcesFolder
(File frontendDirectory) Get the front-end resources folder.static String
getJarResourceString
(String jarImport) Get resource from JAR package.static File
static String
Get the Operating System name from theos.name
system property.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
.getParentThemeNameInFrontend
(File frontendFolder, elemental.json.JsonObject themeJson) getParentThemeNameInFrontend
(File frontendFolder, String projectCustomThemeName) static String
getProjectFrontendDir
(com.vaadin.flow.function.DeploymentConfiguration configuration) Get directory where project's frontend files are located.static InputStream
getResourceAsStream
(String name) static Optional<com.vaadin.flow.theme.Theme>
getThemeAnnotation
(com.vaadin.flow.server.VaadinContext context) Gets the theme annotation for the project.static Optional<elemental.json.JsonObject>
getThemeJsonInFrontend
(Options options, com.vaadin.flow.theme.ThemeDefinition themeDefinition) static Optional<elemental.json.JsonObject>
getThemeJsonInFrontend
(File frontendFolder, String themeName) getThemeName
(File projectFolder) Gets the custom theme name if the custom theme is used in the project.static String
getUnixPath
(Path source) Get path as a String in Unix form.static String
getUnixRelativePath
(Path source, Path target) Get relative path from a source path to a target path in Unix form.static File
Gets vaadin home directory (".vaadin"
folder in the user home dir).protected static com.vaadin.flow.server.frontend.FrontendVersion
getVersion
(String tool, List<String> versionCommand) static String
getWebComponentHtmlContent
(com.vaadin.flow.server.VaadinService service) Gets the content of thefrontend/web-component.html
file which is served by webpack or vite in dev-mode and read from classpath in production mode.static boolean
Check if the current os is Windows.static void
static void
static com.vaadin.flow.server.frontend.FrontendVersion
parseFrontendVersion
(String versionString) Parse the version number of node/npm from version output string.static com.vaadin.flow.server.frontend.FallbackChunk
readFallbackChunk
(elemental.json.JsonObject object) Read fallback chunk data from a json object.static File
resolveFrontendPath
(File projectRoot, String path) Looks up the frontend resource at the given path.static File
resolveFrontendPath
(File projectRoot, String path, File frontendDirectory) Looks up the fronted resource at the given path.static String
streamToString
(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_JSON
andNODE_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_DIR
By default it is
/frontend
in 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. -
DEFAULT_GENERATED_DIR
Default folder name for flow generated stuff relative to theInitParameters.BUILD_FOLDER
.- See Also:
-
IMPORTS_NAME
Name of the file that contains application imports, javascript, theme and style annotations. It is always generated in theDEFAULT_GENERATED_DIR
folder.- See Also:
-
IMPORTS_D_TS_NAME
The TypeScript definitions for theIMPORTS_NAME
file.- 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 frontendGENERATED
folder. 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 frontendGENERATED
folder. 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 frontendGENERATED
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:
-
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:
-
DEFAULT_PROJECT_FRONTEND_GENERATED_DIR
Default generated path for generated frontend files.- See Also:
-
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 theIMPORTS_NAME
file. It is always generated in theDEFAULT_GENERATED_DIR
folder.- See Also:
-
PARAM_GENERATED_DIR
A parameter for overriding theDEFAULT_GENERATED_DIR
folder.- See Also:
-
PARAM_FRONTEND_DIR
A parameter for overriding theDEFAULT_FRONTEND_DIR
folder.- 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:
-
BUILD_FRONTEND_FOLDER
- See Also:
-
FLOW_FRONTEND_FOLDER
- See Also:
-
GENERATED_FRONTEND_FOLDER
- See Also:
-
PARAM_IGNORE_VERSION_CHECKS
Set totrue
to 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:
-
TOKEN_FILE
File used to enable npm mode.- See Also:
-
CHUNKS
A key in a Json object for chunks list.- See Also:
-
FALLBACK
A key in a Json object for fallback chunk.- 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.name
system 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.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(com.vaadin.flow.server.VaadinService service) throws IOException Gets the content of thefrontend/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(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
Looks up the frontend 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_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
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_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.
-
getJarResourceString
Get resource from JAR package.- Parameters:
jarImport
- jar file to get (no resource folder should be added)- Returns:
- resource as String or
null
if 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
File
representing a folder with copied resources
-
getProjectFrontendDir
public static String getProjectFrontendDir(com.vaadin.flow.function.DeploymentConfiguration configuration) Get directory where project's frontend files are located.- Parameters:
configuration
- the current deployment configuration- Returns:
DEFAULT_FRONTEND_DIR
or value ofPARAM_FRONTEND_DIR
if 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.
-
readFallbackChunk
public static com.vaadin.flow.server.frontend.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
-
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.
-
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- See Also:
-
console
- See Also:
-
console
-
logInFile
-
logInFile
-
getLogFile
-
deleteNodeModules
Try to remove thenode_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
- thenode_modules
directory- 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
-
findBundleFile
Finds the given file inside the express mode development bundle that is used.- Parameters:
projectDir
- the project root folderfilename
- the file name inside the bundle- Returns:
- a URL referring to the file inside the bundle or
null
if the file was not found - Throws:
IOException
-
getDevBundleFolder
Get the folder where an application specific bundle is stored.- Parameters:
projectDir
- the project base directory- Returns:
- the bundle directory
-
findBundleStatsJson
Get the stats.json for the application specific development bundle.- Parameters:
projectDir
- the project base directory- Returns:
- stats.json content or
null
if not found - Throws:
IOException
- if an I/O exception occurs.
-
getThemeName
Gets the custom theme name if the custom theme is used in the project.Should be only used in the development mode.
- Parameters:
projectFolder
- the project root folder- Returns:
- custom theme name or empty optional if no theme is used
- Throws:
IOException
- if I/O exceptions occur while trying to extract the theme name.
-
getThemeAnnotation
public static Optional<com.vaadin.flow.theme.Theme> getThemeAnnotation(com.vaadin.flow.server.VaadinContext context) Gets the theme annotation for the project.- Parameters:
context
- the Vaadin context- Returns:
- the theme annotation or an empty optional
-
getThemeJsonInFrontend
public static Optional<elemental.json.JsonObject> getThemeJsonInFrontend(File frontendFolder, String themeName) throws IOException - Throws:
IOException
-
getThemeJsonInFrontend
public static Optional<elemental.json.JsonObject> getThemeJsonInFrontend(Options options, com.vaadin.flow.theme.ThemeDefinition themeDefinition) throws IOException - Throws:
IOException
-
getParentThemeNameInFrontend
-
getParentThemeNameInFrontend
public static Optional<String> getParentThemeNameInFrontend(File frontendFolder, String projectCustomThemeName) throws IOException - Throws:
IOException
-
getResourceAsStream
-
findFreePort
public static int findFreePort(int rangeStart, int rangeEnd)
-