Package com.vaadin.flow.server.frontend
Class FrontendUtils
- java.lang.Object
-
- com.vaadin.flow.server.frontend.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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classFrontendUtils.CommandExecutionExceptionThrown when the command execution fails.static classFrontendUtils.UnknownVersionExceptionThrown when detecting the version of a tool fails.
-
Field Summary
Fields Modifier and Type Field Description static StringBOOTSTRAP_FILE_NAMEFile name of the bootstrap file that is generated in frontendGENERATEDfolder.static StringBRIGHT_BLUEstatic StringCHUNKSA key in a Json object for chunks list.static StringCSS_IMPORTSA key in a Json object for css imports data.static StringDEAULT_FLOW_RESOURCES_FOLDERDeprecated.This is deprecated due to a typo.static StringDEFAULT_CONNECT_APPLICATION_PROPERTIESDefault application properties file path in Vaadin project.static StringDEFAULT_CONNECT_JAVA_SOURCE_FOLDERDefault Java source folder for OpenAPI generator.static StringDEFAULT_CONNECT_OPENAPI_JSON_FILEDefault generated path for OpenAPI spec file.static StringDEFAULT_FLOW_RESOURCES_FOLDERDefault folder where front-end resources present in the classpath jars are copied to.static StringDEFAULT_FRONTEND_DIRPath of the folder containing application frontend source files, it needs to be relative to theDEFAULT_NODE_DIRBy default it is/frontendin the project folder.static StringDEFAULT_GENERATED_DIRDefault folder name for flow generated stuff relative to theInitParameters.BUILD_FOLDER.static StringDEFAULT_NODE_DIRDefault folder for the node related content.static StringDEFAULT_PROJECT_FRONTEND_GENERATED_DIRDefault generated path for generated frontend files.static StringDISABLE_CHECKstatic StringEXPORT_CHUNKThe entry-point key used for the exported bundle.static StringFALLBACKA key in a Json object for fallback chunk.static StringFALLBACK_IMPORTS_NAMEName 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 StringFEATURE_FLAGS_FILE_NAMEFile name of the feature flags file that is generated in frontendGENERATEDfolder.static StringFLOW_NPM_PACKAGE_NAMEThe npm package name that will be used for the javascript files present in jar resources that will to be copied to the npm folder so as they are accessible to webpack.static StringFRONTENDDefault folder used for source and generated folders.static StringGENERATEDDefault folder for client-side generated files inside the project root frontend folder.static StringGREENstatic StringIMPORTS_D_TS_NAMEThe TypeScript definitions for theIMPORTS_NAMEfile.static StringIMPORTS_NAMEName of the file that contains application imports, javascript, theme and style annotations.static StringINDEX_HTMLFile name of the index.html in client side.static StringINDEX_JSFile name of the index.js in client side.static StringINDEX_TSFile name of the index.ts in client side.static StringJS_MODULESA key in a Json object for js modules data.static StringNODE_MODULESLocation for the installed node packages.static StringPARAM_FRONTEND_DIRA parameter for overriding theDEFAULT_FRONTEND_DIRfolder.static StringPARAM_GENERATED_DIRA parameter for overriding theDEFAULT_GENERATED_DIRfolder.static StringPARAM_IGNORE_VERSION_CHECKSSet totrueto ignore node/npm tool version checks.static StringPARAM_TOKEN_FILEA parameter informing about the location of theTOKEN_FILE.static StringPROJECT_BASEDIRstatic StringREDstatic StringSERVICE_WORKER_SRCThe name of the service worker source file for InjectManifest method of workbox-webpack-plugin.static StringSERVICE_WORKER_SRC_JSThe JavaScript version of the service worker file, for checking if a user has a JavaScript version of a custom service worker file already.static StringTHEME_IMPORTS_D_TS_NAMEstatic StringTHEME_IMPORTS_NAMEstatic StringTOKEN_FILEFile used to enable npm mode.static StringVITE_CONFIGThe name of the vite configuration file.static StringVITE_DEVMODE_TSFile name of Vite helper used in development mode.static StringVITE_GENERATED_CONFIGThe name of the generated vite configuration file.static StringWEBPACK_CONFIGThe name of the webpack configuration file.static StringWEBPACK_GENERATEDThe name of the webpack generated configuration file.static StringWEBPACK_PREFIX_ALIASA special prefix used by webpack to map imports placed in theDEFAULT_FRONTEND_DIR.static StringYELLOW
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidclearCachedStatsContent(VaadinService service)Clears thestats.jsoncache within thisVaadinContext.static StringcommandToString(String baseDir, List<String> command)Pretty prints a command line order.static voidconsole(String format, Object message)Intentionally send to console instead to log, useful when executing external processes.static ProcessBuildercreateProcessBuilder(List<String> command)Creates a process builder for the given list of program and arguments.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 InputStreamgetFrontendFileFromDevModeHandler(VaadinService service, String path)Get the contents of a frontend file from the running dev server.static StringgetIndexHtmlContent(VaadinService service)Gets the content of thefrontend/index.htmlfile which is served by webpack-dev-server in dev-mode and read from classpath in production mode.static StringgetOsName()Get the Operating System name from theos.namesystem property.static 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 StringgetProjectFrontendDir(DeploymentConfiguration configuration)Get directory where project's frontend files are located.static StringgetStatsAssetsByChunkName(VaadinService service)Load the asset chunks fromstats.json.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 FilegetVaadinHomeDirectory()Gets vaadin home directory (".vaadin"folder in the user home dir).protected static FrontendVersiongetVersion(String tool, List<String> versionCommand)static booleanisWebpackConfigFile(File file)Checks whether thefileis a webpack configuration file with the expected content (includes a configuration generated by Flow).static booleanisWindows()Check if the current os is Windows.static FrontendVersionparseFrontendVersion(String versionString)Parse the version number of node/npm from version output string.static FallbackChunkreadFallbackChunk(elemental.json.JsonObject object)Read fallback chunk data from a json object.static FileresolveFrontendPath(File projectRoot, String path)Looks up the front file 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 Detail
-
PROJECT_BASEDIR
public static final String PROJECT_BASEDIR
- See Also:
- Constant Field Values
-
DEFAULT_NODE_DIR
public static final String DEFAULT_NODE_DIR
Default folder for the node related content. It's the base directory forConstants.PACKAGE_JSON,WEBPACK_CONFIG,NODE_MODULES. By default it's the project root folder.- See Also:
- Constant Field Values
-
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 theNODE_PATH.- See Also:
- Constant Field Values
-
FRONTEND
public static final String FRONTEND
Default folder used for source and generated folders.- 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 theDEFAULT_NODE_DIRBy default it is/frontendin 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_CONFIG
public static final String VITE_CONFIG
The name of the vite 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
-
FLOW_NPM_PACKAGE_NAME
public static final String FLOW_NPM_PACKAGE_NAME
The npm package name that will be used for the javascript files present in jar resources that will to be copied to the npm folder so as they are accessible to webpack.- See Also:
- Constant Field Values
-
DEFAULT_FLOW_RESOURCES_FOLDER
public static final String DEFAULT_FLOW_RESOURCES_FOLDER
Default folder where front-end resources present in the classpath jars are copied to. Relative to theInitParameters.BUILD_FOLDER.- See Also:
- Constant Field Values
-
DEAULT_FLOW_RESOURCES_FOLDER
@Deprecated public static final String DEAULT_FLOW_RESOURCES_FOLDER
Deprecated.This is deprecated due to a typo. Use DEFAULT_FLOW_RESOURCES_FOLDER instead.Default folder under build folder for copying front-end resources present in the classpath jars.
-
DEFAULT_GENERATED_DIR
public static final String DEFAULT_GENERATED_DIR
Default folder name for flow generated stuff relative to theInitParameters.BUILD_FOLDER.- See Also:
- Constant Field Values
-
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 theDEFAULT_GENERATED_DIRfolder.- See Also:
- Constant Field Values
-
IMPORTS_D_TS_NAME
public static final String IMPORTS_D_TS_NAME
The TypeScript definitions for theIMPORTS_NAMEfile.- See Also:
- Constant Field Values
-
THEME_IMPORTS_D_TS_NAME
public static final String THEME_IMPORTS_D_TS_NAME
- See Also:
- Constant Field Values
-
THEME_IMPORTS_NAME
public static final String THEME_IMPORTS_NAME
- 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 frontendGENERATEDfolder. The bootstrap file is always executed in a Vaadin app.- 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 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:
- Constant Field Values
-
INDEX_HTML
public static final String INDEX_HTML
File name of the index.html in client side.- See Also:
- Constant Field Values
-
INDEX_TS
public static final String INDEX_TS
File name of the index.ts in client side.- See Also:
- Constant Field Values
-
INDEX_JS
public static final String INDEX_JS
File name of the index.js 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 theIMPORTS_NAMEfile. It is always generated in theDEFAULT_GENERATED_DIRfolder.- See Also:
- Constant Field Values
-
PARAM_GENERATED_DIR
public static final String PARAM_GENERATED_DIR
A parameter for overriding theDEFAULT_GENERATED_DIRfolder.- See Also:
- Constant Field Values
-
PARAM_FRONTEND_DIR
public static final String PARAM_FRONTEND_DIR
A parameter for overriding theDEFAULT_FRONTEND_DIRfolder.- See Also:
- Constant Field Values
-
PARAM_IGNORE_VERSION_CHECKS
public static final String PARAM_IGNORE_VERSION_CHECKS
Set totrueto ignore node/npm tool version checks.- See Also:
- Constant Field Values
-
WEBPACK_PREFIX_ALIAS
public static final String WEBPACK_PREFIX_ALIAS
A special prefix used by webpack to map imports placed in theDEFAULT_FRONTEND_DIR. e.g.import 'Frontend/foo.js';references the filefrontend/foo.js.- See Also:
- Constant Field Values
-
TOKEN_FILE
public static final String TOKEN_FILE
File used to enable npm mode.- See Also:
- Constant Field Values
-
CHUNKS
public static final String CHUNKS
A key in a Json object for chunks list.- See Also:
- Constant Field Values
-
FALLBACK
public static final String FALLBACK
A key in a Json object for fallback chunk.- 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
-
CSS_IMPORTS
public static final String CSS_IMPORTS
A key in a Json object for css imports data.- See Also:
- Constant Field Values
-
JS_MODULES
public static final String JS_MODULES
A key in a Json object for js modules data.- See Also:
- Constant Field Values
-
PARAM_TOKEN_FILE
public static final String PARAM_TOKEN_FILE
A parameter informing about the location of theTOKEN_FILE.- See Also:
- Constant Field Values
-
DISABLE_CHECK
public static final String DISABLE_CHECK
- See Also:
- Constant Field Values
-
YELLOW
public static final String YELLOW
- See Also:
- Constant Field Values
-
RED
public static final String RED
- See Also:
- Constant Field Values
-
GREEN
public static final String GREEN
- See Also:
- Constant Field Values
-
BRIGHT_BLUE
public static final String BRIGHT_BLUE
- See Also:
- Constant Field Values
-
-
Method Detail
-
getOsName
public static String 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
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 thestats.jsoncache within thisVaadinContext.- Parameters:
service- the vaadin service.
-
getIndexHtmlContent
public static String getIndexHtmlContent(VaadinService service) throws IOException
Gets the content of thefrontend/index.htmlfile which is served by webpack-dev-server in dev-mode and read from classpath in production mode. NOTE: In dev mode, the file content file is fetched via webpack 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
-
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 front file at the given path. If the path starts with./, first look inprojectRoot/frontend, then inprojectRoot/node_modules/@vaadin/flow-frontend. If the path does not start with./, look innode_modulesinstead.- Parameters:
projectRoot- the project root folder.path- the file path.- 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 fromstats.json. We will only read the file until we have reached the assetsByChunkName json and return that as a json object string. Note: Thestats.jsonis cached when external stats is enabled orstats.jsonis provided from the class path. To clear the cache useclearCachedStatsContent(VaadinService).- Parameters:
service- the Vaadin service.- Returns:
- json for assetsByChunkName object in stats.json or
nullif stats.json not found or content not found. - Throws:
IOException- if an I/O error occurs while creating the input stream.
-
getProjectFrontendDir
public static String getProjectFrontendDir(DeploymentConfiguration 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.
-
isWebpackConfigFile
public static boolean isWebpackConfigFile(File file) throws IOException
Checks whether thefileis a webpack configuration file with the expected content (includes a configuration generated by Flow).- Parameters:
file- a file to check- Returns:
trueiff 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 pathtarget- 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
-
getVersion
protected static 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 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
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 directorycommand- 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, 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
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 messagemessage- the string to show
-
deleteNodeModules
public static void deleteNodeModules(File nodeModules) throws IOException
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
-
-