|
| 1 | +/* |
| 2 | + * Copyright 2016-2023 DiffPlug |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.diffplug.spotless.npm; |
| 17 | + |
| 18 | +import java.io.File; |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.concurrent.ExecutionException; |
| 26 | +import java.util.stream.Collectors; |
| 27 | + |
| 28 | +import com.diffplug.spotless.ProcessRunner; |
| 29 | +import com.diffplug.spotless.ProcessRunner.LongRunningProcess; |
| 30 | + |
| 31 | +class StandardNpmProcess implements NpmProcess { |
| 32 | + |
| 33 | + private final File workingDir; |
| 34 | + |
| 35 | + private final File npmExecutable; |
| 36 | + |
| 37 | + private final File nodeExecutable; |
| 38 | + |
| 39 | + private final ProcessRunner processRunner; |
| 40 | + |
| 41 | + StandardNpmProcess(File workingDir, File npmExecutable, File nodeExecutable) { |
| 42 | + this.workingDir = workingDir; |
| 43 | + this.npmExecutable = npmExecutable; |
| 44 | + this.nodeExecutable = nodeExecutable; |
| 45 | + processRunner = ProcessRunner.usingRingBuffersOfCapacity(100 * 1024); // 100kB |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void install() { |
| 50 | + npmAwait("install", |
| 51 | + "--no-audit", |
| 52 | + "--no-package-lock", |
| 53 | + "--no-fund", |
| 54 | + "--prefer-offline"); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public LongRunningProcess start() { |
| 59 | + // adding --scripts-prepend-node-path=true due to https://github.com/diffplug/spotless/issues/619#issuecomment-648018679 |
| 60 | + return npm("start", "--scripts-prepend-node-path=true"); |
| 61 | + } |
| 62 | + |
| 63 | + private void npmAwait(String... args) { |
| 64 | + try (LongRunningProcess npmProcess = npm(args)) { |
| 65 | + if (npmProcess.waitFor() != 0) { |
| 66 | + throw new NpmProcessException("Running npm command '" + commandLine(args) + "' failed with exit code: " + npmProcess.exitValue() + "\n\n" + npmProcess.result()); |
| 67 | + } |
| 68 | + } catch (InterruptedException e) { |
| 69 | + throw new NpmProcessException("Running npm command '" + commandLine(args) + "' was interrupted.", e); |
| 70 | + } catch (ExecutionException e) { |
| 71 | + throw new NpmProcessException("Running npm command '" + commandLine(args) + "' failed.", e); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private LongRunningProcess npm(String... args) { |
| 76 | + List<String> processCommand = processCommand(args); |
| 77 | + try { |
| 78 | + return processRunner.start(this.workingDir, environmentVariables(), null, true, processCommand); |
| 79 | + } catch (IOException e) { |
| 80 | + throw new NpmProcessException("Failed to launch npm command '" + commandLine(args) + "'.", e); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private List<String> processCommand(String... args) { |
| 85 | + List<String> command = new ArrayList<>(args.length + 1); |
| 86 | + command.add(this.npmExecutable.getAbsolutePath()); |
| 87 | + command.addAll(Arrays.asList(args)); |
| 88 | + return command; |
| 89 | + } |
| 90 | + |
| 91 | + private Map<String, String> environmentVariables() { |
| 92 | + Map<String, String> environmentVariables = new HashMap<>(); |
| 93 | + environmentVariables.put("PATH", this.nodeExecutable.getParentFile().getAbsolutePath() + File.pathSeparator + System.getenv("PATH")); |
| 94 | + return environmentVariables; |
| 95 | + } |
| 96 | + |
| 97 | + private String commandLine(String... args) { |
| 98 | + return "npm " + Arrays.stream(args).collect(Collectors.joining(" ")); |
| 99 | + } |
| 100 | + |
| 101 | + static class NpmProcessException extends RuntimeException { |
| 102 | + private static final long serialVersionUID = 6424331316676759525L; |
| 103 | + |
| 104 | + public NpmProcessException(String message) { |
| 105 | + super(message); |
| 106 | + } |
| 107 | + |
| 108 | + public NpmProcessException(String message, Throwable cause) { |
| 109 | + super(message, cause); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments