• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

devonfw / IDEasy / 18949830015

30 Oct 2025 05:38PM UTC coverage: 68.876% (-0.004%) from 68.88%
18949830015

push

github

web-flow
#1553: speed up environment commandlet (#1554)

Co-authored-by: jan-vcapgemini <59438728+jan-vcapgemini@users.noreply.github.com>

3486 of 5545 branches covered (62.87%)

Branch coverage included in aggregate %.

9121 of 12759 relevant lines covered (71.49%)

3.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.56
cli/src/main/java/com/devonfw/tools/ide/tool/node/NodeBasedCommandlet.java
1
package com.devonfw.tools.ide.tool.node;
2

3
import java.nio.file.Files;
4
import java.nio.file.Path;
5
import java.util.Set;
6

7
import com.devonfw.tools.ide.cache.CachedValue;
8
import com.devonfw.tools.ide.common.Tag;
9
import com.devonfw.tools.ide.context.IdeContext;
10
import com.devonfw.tools.ide.process.ProcessContext;
11
import com.devonfw.tools.ide.process.ProcessErrorHandling;
12
import com.devonfw.tools.ide.process.ProcessMode;
13
import com.devonfw.tools.ide.process.ProcessResult;
14
import com.devonfw.tools.ide.tool.LocalToolCommandlet;
15
import com.devonfw.tools.ide.tool.corepack.Corepack;
16
import com.devonfw.tools.ide.tool.repository.ToolRepository;
17
import com.devonfw.tools.ide.version.VersionIdentifier;
18

19
/**
20
 * {@link LocalToolCommandlet} for tools based on <a href="https://www.npmjs.com/">npm</a>.
21
 */
22
public abstract class NodeBasedCommandlet extends LocalToolCommandlet {
23

24
  private final CachedValue<VersionIdentifier> installedVersion;
25

26
  /**
27
   * The constructor.
28
   *
29
   * @param context the {@link IdeContext}.
30
   * @param tool the {@link #getName() tool name}.
31
   * @param tags the {@link #getTags() tags} classifying the tool. Should be created via {@link Set#of(Object) Set.of} method.
32
   */
33
  public NodeBasedCommandlet(IdeContext context, String tool, Set<Tag> tags) {
34

35
    super(context, tool, tags);
5✔
36
    this.installedVersion = new CachedValue<>(this::computeInstalledVersion);
7✔
37
  }
1✔
38

39
  /**
40
   * @return the computed value of the {@link #getInstalledVersion() installed version}.
41
   */
42
  protected abstract VersionIdentifier computeInstalledVersion();
43

44
  @Override
45
  protected boolean isIgnoreSoftwareRepo() {
46

47
    // node and node.js are messy - see https://github.com/devonfw/IDEasy/issues/352
48
    return true;
2✔
49
  }
50

51
  /**
52
   * @return the package of this tool from the NPM registry.
53
   */
54
  public String getPackageName() {
55

56
    return this.tool;
3✔
57
  }
58

59
  @Override
60
  public Path getToolPath() {
61

62
    return this.context.getSoftwarePath().resolve("node");
6✔
63
  }
64

65
  @Override
66
  public VersionIdentifier getInstalledVersion() {
67

68
    return this.installedVersion.get();
5✔
69
  }
70

71
  @Override
72
  public boolean isInstalled() {
73

74
    return hasNodeBinary(this.tool);
5✔
75
  }
76

77
  @Override
78
  public String getInstalledEdition() {
79

80
    if (getInstalledVersion() != null) {
3!
81
      return this.tool;
×
82
    }
83
    return null;
2✔
84
  }
85

86
  @Override
87
  protected void performToolInstallation(ToolRepository toolRepository, VersionIdentifier resolvedVersion, Path installationPath, String edition,
88
      ProcessContext processContext) {
89

90
    runPackageInstall(getPackageName() + "@" + resolvedVersion);
7✔
91
    this.installedVersion.invalidate();
3✔
92
  }
1✔
93

94
  /**
95
   * Checks if the tool can be uninstalled e.g. if the uninstall command for the tool should be disabled or not.
96
   *
97
   * @return {@code true} if the tool can be uninstalled, {@code false} if not.
98
   */
99
  protected boolean canBeUninstalled() {
100
    return true;
2✔
101
  }
102

103
  @Override
104
  protected void performUninstall(Path toolPath) {
105
    if (canBeUninstalled()) {
3✔
106
      runPackageUninstall(getPackageName());
4✔
107
      this.installedVersion.invalidate();
4✔
108
    } else {
109
      this.context.info("IDEasy does not support uninstalling the tool {} since this will break your installation.\n"
9✔
110
          + "If you really want to uninstall it, please uninstall the entire node installation:\n"
111
          + "ide uninstall node", getPackageName());
2✔
112
    }
113
  }
1✔
114

115
  /**
116
   * Checks if a provided binary can be found within node.
117
   *
118
   * @param binary name of the binary.
119
   * @return {@code true} if a binary was found in the node installation, {@code false} if not.
120
   */
121
  protected boolean hasNodeBinary(String binary) {
122

123
    return Files.exists(getToolBinPath().resolve(binary));
8✔
124
  }
125

126
  /**
127
   * Runs uninstall using the package manager.
128
   *
129
   * @param npmPackage the npm package to uninstall.
130
   */
131
  protected void runPackageUninstall(String npmPackage) {
132

133
    runPackageManager("uninstall", "-g", npmPackage).failOnError();
17✔
134
  }
1✔
135

136
  /**
137
   * Runs install using the package manager.
138
   *
139
   * @param npmPackage the npm package to install.
140
   */
141
  protected void runPackageInstall(String npmPackage) {
142

143
    runPackageManager("install", "-gf", npmPackage).failOnError();
17✔
144
  }
1✔
145

146
  /**
147
   * @param args the arguments for the package manager.
148
   * @return the {@link ProcessResult}.
149
   */
150
  protected ProcessResult runPackageManager(String... args) {
151

152
    return runPackageManager(ProcessMode.DEFAULT, ProcessErrorHandling.THROW_CLI, args);
6✔
153
  }
154

155
  /**
156
   * @param args the arguments for {@link com.devonfw.tools.ide.tool.corepack.Corepack}.
157
   * @return the {@link ProcessResult}.
158
   */
159
  protected ProcessResult runCorepack(String... args) {
160

161
    return runCorepack(ProcessMode.DEFAULT, ProcessErrorHandling.THROW_CLI, args);
×
162
  }
163

164
  /**
165
   * @param processMode the {@link ProcessMode}.
166
   * @param errorHandling the {@link ProcessErrorHandling}.
167
   * @param args the arguments for the package manager.
168
   * @return the {@link ProcessResult}.
169
   */
170
  protected abstract ProcessResult runPackageManager(ProcessMode processMode, ProcessErrorHandling errorHandling, String... args);
171

172
  /**
173
   * @param processMode the {@link ProcessMode}.
174
   * @param errorHandling the {@link ProcessErrorHandling}.
175
   * @param args the arguments for {@link com.devonfw.tools.ide.tool.corepack.Corepack}.
176
   * @return the {@link ProcessResult}.
177
   */
178
  protected ProcessResult runCorepack(ProcessMode processMode, ProcessErrorHandling errorHandling, String... args) {
179
    ProcessContext pc = this.context.newProcess().errorHandling(errorHandling);
×
180
    Corepack corepack = this.context.getCommandletManager().getCommandlet(Corepack.class);
×
181
    return corepack.runTool(processMode, null, pc, args);
×
182
  }
183

184

185
  @Override
186
  public String getToolHelpArguments() {
187

188
    return "--help";
×
189
  }
190
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc