Skip to content

Commit fdce70a

Browse files
authored
feat: Automatic update of node version (#11926)
Enable automatic update of the node version installed by the framework. Auto update is opt in, but for a version that is older than what should work it is updated automatically. Fixes #9864
1 parent e6459b3 commit fdce70a

16 files changed

Lines changed: 576 additions & 194 deletions

File tree

‎flow-plugins/flow-gradle-plugin/src/main/kotlin/com/vaadin/gradle/GradlePluginAdapter.kt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ internal class GradlePluginAdapter(val project: Project, private val isBeforePro
121121
override fun nodeDownloadRoot(): URI =
122122
URI.create(extension.nodeDownloadRoot)
123123

124+
override fun nodeAutoUpdate(): Boolean = extension.nodeAutoUpdate
125+
124126
override fun nodeVersion(): String = extension.nodeVersion
125127

126128
override fun npmFolder(): File = extension.npmFolder

‎flow-plugins/flow-gradle-plugin/src/main/kotlin/com/vaadin/gradle/VaadinFlowPluginExtension.kt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ public open class VaadinFlowPluginExtension(project: Project) {
168168
*/
169169
public var nodeDownloadRoot: String = NodeInstaller.DEFAULT_NODEJS_DOWNLOAD_ROOT
170170

171+
/**
172+
* Allow automatic update of node installed to alternate location. Default `false`
173+
*/
174+
public var nodeAutoUpdate: Boolean = false
175+
171176
/**
172177
* Defines the output directory for generated non-served resources, such as
173178
* the token file. Defaults to `build/vaadin-generated` folder.
@@ -250,6 +255,7 @@ public open class VaadinFlowPluginExtension(project: Project) {
250255
"generatedTsFolder=$generatedTsFolder, " +
251256
"nodeVersion=$nodeVersion, " +
252257
"nodeDownloadRoot=$nodeDownloadRoot, " +
258+
"nodeAutoUpdate=$nodeAutoUpdate" +
253259
"resourceOutputDirectory=$resourceOutputDirectory" +
254260
")"
255261
}

‎flow-plugins/flow-gradle-plugin/src/main/kotlin/com/vaadin/gradle/VaadinUtils.kt‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
*/
1616
package com.vaadin.gradle
1717

18+
import com.vaadin.flow.function.SerializableSupplier
1819
import com.vaadin.flow.server.frontend.FrontendTools
20+
import com.vaadin.flow.server.frontend.FrontendToolsSettings
1921
import com.vaadin.flow.server.frontend.FrontendUtils
2022
import org.gradle.api.Project
2123
import java.io.File
2224
import java.net.URI
23-
import java.util.function.Supplier
2425

2526
/**
2627
* Finds the value of a boolean property. It searches in gradle and system properties.
@@ -62,9 +63,10 @@ public fun Project.vaadin(block: VaadinFlowPluginExtension.() -> Unit) {
6263

6364
internal fun Collection<File>.toPrettyFormat(): String = joinToString(prefix = "[", postfix = "]") { if (it.isFile) it.name else it.absolutePath }
6465

65-
internal fun VaadinFlowPluginExtension.createFrontendTools(): FrontendTools =
66-
FrontendTools(npmFolder.absolutePath,
67-
Supplier { FrontendUtils.getVaadinHomeDirectory().absolutePath },
68-
nodeVersion,
69-
URI(nodeDownloadRoot)
70-
)
66+
internal fun VaadinFlowPluginExtension.createFrontendTools(): FrontendTools {
67+
var settings = FrontendToolsSettings(npmFolder.absolutePath, SerializableSupplier { FrontendUtils.getVaadinHomeDirectory().absolutePath })
68+
settings.setNodeVersion(nodeVersion)
69+
settings.setNodeDownloadRoot(URI(nodeDownloadRoot))
70+
return FrontendTools(settings)
71+
}
72+

‎flow-plugins/flow-maven-plugin/src/main/java/com/vaadin/flow/plugin/maven/FlowModeAbstractMojo.java‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ public abstract class FlowModeAbstractMojo extends AbstractMojo
114114
@Parameter(property = "node.version", defaultValue = FrontendTools.DEFAULT_NODE_VERSION)
115115
private String nodeVersion;
116116

117+
/**
118+
* Setting defining if the automatically installed node version may be
119+
* updated to the default Vaadin node version.
120+
*/
121+
@Parameter(property = InitParameters.NODE_AUTO_UPDATE, defaultValue = Constants.DEFAULT_NODE_AUTO_UPDATE)
122+
private boolean nodeAutoUpdate;
123+
117124
/**
118125
* The folder where `package.json` file is located. Default is project root
119126
* dir.
@@ -162,7 +169,7 @@ public abstract class FlowModeAbstractMojo extends AbstractMojo
162169
* absent. Then it will be used instead of globally 'node' or locally
163170
* installed installed 'node'.
164171
*/
165-
@Parameter(property = Constants.REQUIRE_HOME_NODE_EXECUTABLE, defaultValue = "false")
172+
@Parameter(property = Constants.REQUIRE_HOME_NODE_EXECUTABLE, defaultValue = Constants.DEFAULT_REQUIRE_HOME_NODE_EXECUTABLE)
166173
private boolean requireHomeNodeExec;
167174

168175
/**
@@ -329,6 +336,11 @@ public URI nodeDownloadRoot() throws URISyntaxException {
329336
}
330337
}
331338

339+
@Override
340+
public boolean nodeAutoUpdate() {
341+
return nodeAutoUpdate;
342+
}
343+
332344
@Override
333345
public String nodeVersion() {
334346

‎flow-plugins/flow-plugin-base/src/main/java/com/vaadin/flow/plugin/base/BuildFrontendUtil.java‎

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.vaadin.flow.server.ExecutionFailedException;
3838
import com.vaadin.flow.server.InitParameters;
3939
import com.vaadin.flow.server.frontend.FrontendTools;
40+
import com.vaadin.flow.server.frontend.FrontendToolsSettings;
4041
import com.vaadin.flow.server.frontend.FrontendUtils;
4142
import com.vaadin.flow.server.frontend.NodeTasks;
4243
import com.vaadin.flow.server.frontend.scanner.ClassFinder;
@@ -117,10 +118,9 @@ public static void prepareFrontend(PluginAdapterBase adapter)
117118
throws IOException, ExecutionFailedException, URISyntaxException {
118119

119120
final URI nodeDownloadRootURI = adapter.nodeDownloadRoot();
120-
FrontendTools tools = new FrontendTools(
121-
adapter.npmFolder().getAbsolutePath(),
122-
() -> FrontendUtils.getVaadinHomeDirectory().getAbsolutePath(),
123-
adapter.nodeVersion(), nodeDownloadRootURI);
121+
122+
FrontendToolsSettings settings = getFrontendToolsSettings(adapter);
123+
FrontendTools tools = new FrontendTools(settings);
124124
tools.validateNodeAndNpmVersion();
125125

126126
try {
@@ -147,6 +147,7 @@ public static void prepareFrontend(PluginAdapterBase adapter)
147147
.runNpmInstall(false)
148148
.withNodeVersion(adapter.nodeVersion())
149149
.withNodeDownloadRoot(nodeDownloadRootURI)
150+
.setNodeAutoUpdate(adapter.nodeAutoUpdate())
150151
.withHomeNodeExecRequired(
151152
adapter.requireHomeNodeExec());
152153
// If building a jar project copy jar artifact contents now as we
@@ -170,6 +171,20 @@ public static void prepareFrontend(PluginAdapterBase adapter)
170171

171172
}
172173

174+
private static FrontendToolsSettings getFrontendToolsSettings(
175+
PluginAdapterBase adapter) throws URISyntaxException {
176+
FrontendToolsSettings settings = new FrontendToolsSettings(
177+
adapter.npmFolder().getAbsolutePath(),
178+
() -> FrontendUtils.getVaadinHomeDirectory().getAbsolutePath());
179+
settings.setNodeDownloadRoot(adapter.nodeDownloadRoot());
180+
settings.setNodeVersion(adapter.nodeVersion());
181+
settings.setAutoUpdate(adapter.nodeAutoUpdate());
182+
settings.setUseGlobalPnpm(adapter.useGlobalPnpm());
183+
settings.setForceAlternativeNode(adapter.requireHomeNodeExec());
184+
185+
return settings;
186+
}
187+
173188
/**
174189
* Propagates the BuildInfo.
175190
*
@@ -298,7 +313,8 @@ public static void runNodeUpdater(PluginAdapterBuild adapter)
298313
.withHomeNodeExecRequired(
299314
adapter.requireHomeNodeExec())
300315
.withNodeVersion(adapter.nodeVersion())
301-
.withNodeDownloadRoot(nodeDownloadRootURI).build()
316+
.withNodeDownloadRoot(nodeDownloadRootURI)
317+
.setNodeAutoUpdate(adapter.nodeAutoUpdate()).build()
302318
.execute();
303319
} catch (ExecutionFailedException exception) {
304320
throw exception;
@@ -338,13 +354,9 @@ public static void runWebpack(PluginAdapterBase adapter) throws IOException,
338354
webpackExecutable.getAbsolutePath()));
339355
}
340356

341-
final URI nodeDownloadRootURI;
342-
nodeDownloadRootURI = adapter.nodeDownloadRoot();
343357
String nodePath;
344-
FrontendTools tools = new FrontendTools(
345-
adapter.npmFolder().getAbsolutePath(),
346-
() -> FrontendUtils.getVaadinHomeDirectory().getAbsolutePath(),
347-
adapter.nodeVersion(), nodeDownloadRootURI);
358+
FrontendToolsSettings settings = getFrontendToolsSettings(adapter);
359+
FrontendTools tools = new FrontendTools(settings);
348360
if (adapter.requireHomeNodeExec()) {
349361
nodePath = tools.forceAlternativeNodeExecutable();
350362
} else {

‎flow-plugins/flow-plugin-base/src/main/java/com/vaadin/flow/plugin/base/PluginAdapterBase.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,13 @@ default boolean isUseDeprecatedV14Bootstrapping() {
205205
*/
206206
URI nodeDownloadRoot() throws URISyntaxException;
207207

208+
/**
209+
* Whether the alternative node may be autoupdated or not.
210+
*
211+
* @return {@code true} to update node if older than default
212+
*/
213+
boolean nodeAutoUpdate();
214+
208215
/**
209216
* The node.js version to be used when node.js is installed automatically by
210217
* Vaadin, for example `"v12.18.3"`. Defaults to null which uses the

‎flow-server/src/main/java/com/vaadin/flow/server/Constants.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,16 @@ public final class Constants implements Serializable {
323323
*/
324324
public static final String GLOBAL_PNPM_DEFAULT_STRING = "false";
325325

326+
/**
327+
* The default value for {@link InitParameters#NODE_AUTO_UPDATE}.
328+
*/
329+
public static final String DEFAULT_NODE_AUTO_UPDATE = "false";
330+
331+
/**
332+
* The default value for {@link #REQUIRE_HOME_NODE_EXECUTABLE}.
333+
*/
334+
public static final String DEFAULT_REQUIRE_HOME_NODE_EXECUTABLE = "false";
335+
326336
/**
327337
* @deprecated Use {@link InitParameters#REQUIRE_HOME_NODE_EXECUTABLE}
328338
* instead.

‎flow-server/src/main/java/com/vaadin/flow/server/InitParameters.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,14 @@ public class InitParameters implements Serializable {
164164
*/
165165
public static final String REQUIRE_HOME_NODE_EXECUTABLE = "require.home.node";
166166

167+
/**
168+
* Configuration parameter name for requiring node executable installed in
169+
* home directory.
170+
*
171+
* @since
172+
*/
173+
public static final String NODE_AUTO_UPDATE = "node.auto.update";
174+
167175
/**
168176
* Configuration name for the parameter that sets the compiled web
169177
* components path. The path should be the same as

0 commit comments

Comments
 (0)