|
15 | 15 | */ |
16 | 16 | package com.diffplug.webtools.node; |
17 | 17 |
|
18 | | -import com.github.eirslett.maven.plugins.frontend.lib.ProxyConfig; |
| 18 | +import java.io.BufferedReader; |
19 | 19 | import java.io.File; |
20 | 20 | import java.io.IOException; |
| 21 | +import java.io.InputStream; |
| 22 | +import java.io.InputStreamReader; |
21 | 23 | import java.nio.charset.StandardCharsets; |
22 | 24 | import java.nio.file.Files; |
23 | | -import java.util.Collections; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.List; |
24 | 27 | import java.util.Objects; |
25 | 28 | import java.util.TreeMap; |
26 | | -import org.gradle.api.Action; |
27 | | -import org.gradle.api.DefaultTask; |
28 | | -import org.gradle.api.Plugin; |
29 | | -import org.gradle.api.Project; |
| 29 | +import java.util.concurrent.CompletableFuture; |
| 30 | +import org.gradle.api.*; |
30 | 31 | import org.gradle.api.file.DirectoryProperty; |
31 | 32 | import org.gradle.api.provider.Property; |
32 | 33 | import org.gradle.api.tasks.CacheableTask; |
@@ -94,14 +95,66 @@ public TreeMap<String, String> getEnvironment() { |
94 | 95 | @Internal |
95 | 96 | public abstract DirectoryProperty getProjectDir(); |
96 | 97 |
|
| 98 | + private static CompletableFuture<Void> readStream(InputStream inputStream, List<String> outputLines, String streamName) { |
| 99 | + return CompletableFuture.runAsync(() -> { |
| 100 | + try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { |
| 101 | + String line; |
| 102 | + while ((line = reader.readLine()) != null) { |
| 103 | + synchronized (outputLines) { |
| 104 | + outputLines.add(line); |
| 105 | + } |
| 106 | + } |
| 107 | + } catch (IOException e) { |
| 108 | + synchronized (outputLines) { |
| 109 | + outputLines.add("Error reading " + streamName + ": " + e.getMessage()); |
| 110 | + } |
| 111 | + } |
| 112 | + }); |
| 113 | + } |
| 114 | + |
97 | 115 | @TaskAction |
98 | 116 | public void npmCiRunTask() throws Exception { |
99 | 117 | SetupCleanupNode setup = getSetup().get(); |
| 118 | + File projectDir = getProjectDir().get().getAsFile(); |
100 | 119 | // install node, npm, and package-lock.json |
101 | | - setup.start(getProjectDir().get().getAsFile()); |
102 | | - // run the gulp task |
103 | | - ProxyConfig proxyConfig = new ProxyConfig(Collections.emptyList()); |
104 | | - setup.factory().getNpmRunner(proxyConfig, null).execute("run " + npmTaskName, environment); |
| 120 | + setup.start(projectDir); |
| 121 | + |
| 122 | + // Use ProcessBuilder for direct console output instead of NpmRunner |
| 123 | + File installDir = new File(projectDir, "build/node-install"); |
| 124 | + File npmExe; |
| 125 | + if (System.getProperty("os.name").toLowerCase().contains("win")) { |
| 126 | + npmExe = new File(installDir, "node/npm.cmd"); |
| 127 | + } else { |
| 128 | + npmExe = new File(installDir, "node/npm"); |
| 129 | + } |
| 130 | + |
| 131 | + ProcessBuilder processBuilder = new ProcessBuilder(npmExe.getAbsolutePath(), "run", npmTaskName); |
| 132 | + processBuilder.directory(projectDir); |
| 133 | + processBuilder.environment().putAll(environment); |
| 134 | + Process process = processBuilder.start(); |
| 135 | + |
| 136 | + // Buffer output to only show on failure |
| 137 | + List<String> stdoutLines = new ArrayList<>(); |
| 138 | + List<String> stderrLines = new ArrayList<>(); |
| 139 | + |
| 140 | + // Create threads to read stdout and stderr concurrently |
| 141 | + CompletableFuture<Void> stdoutFuture = readStream(process.getInputStream(), stdoutLines, "stdout"); |
| 142 | + CompletableFuture<Void> stderrFuture = readStream(process.getErrorStream(), stderrLines, "stderr"); |
| 143 | + int exitCode = process.waitFor(); |
| 144 | + CompletableFuture.allOf(stdoutFuture, stderrFuture).join(); |
| 145 | + if (exitCode == 0) { |
| 146 | + return; |
| 147 | + } |
| 148 | + |
| 149 | + var cmd = new StringBuilder().append("> npm run ").append(npmTaskName).append(" FAILED\n"); |
| 150 | + environment.forEach((key, value) -> cmd.append(" env ").append(key).append("=").append(value).append("\n")); |
| 151 | + for (String line : stdoutLines) { |
| 152 | + cmd.append(line).append("\n"); |
| 153 | + } |
| 154 | + for (String line : stderrLines) { |
| 155 | + cmd.append(line).append("\n"); |
| 156 | + } |
| 157 | + throw new GradleException(cmd.toString()); |
105 | 158 | } |
106 | 159 | } |
107 | 160 |
|
|
0 commit comments