Skip to content

Commit fdbbd02

Browse files
committed
1162: add unique suffix reflecting package.json content
1 parent 1b990ab commit fdbbd02

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

lib/src/main/java/com/diffplug/spotless/npm/NodeServerLayout.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ class NodeServerLayout {
2929
private final File serveJsFile;
3030
private final File npmrcFile;
3131

32-
NodeServerLayout(File buildDir, String stepName) {
33-
this.nodeModulesDir = new File(buildDir, "spotless-node-modules-" + stepName);
32+
NodeServerLayout(File buildDir, String stepName, String stepSuffix) {
33+
this.nodeModulesDir = new File(buildDir, String.format("spotless-node-modules-%s-%s", stepName, stepSuffix));
3434
this.packageJsonFile = new File(nodeModulesDir, "package.json");
3535
this.serveJsFile = new File(nodeModulesDir, "serve.js");
3636
this.npmrcFile = new File(nodeModulesDir, ".npmrc");

lib/src/main/java/com/diffplug/spotless/npm/NpmFormatterStepStateBase.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,14 @@ protected NpmFormatterStepStateBase(String stepName, NpmConfig npmConfig, NpmFor
5656
this.stepName = requireNonNull(stepName);
5757
this.npmConfig = requireNonNull(npmConfig);
5858
this.locations = locations;
59-
this.nodeServerLayout = new NodeServerLayout(locations.buildDir(), stepName);
59+
String stateSuffix = stateSuffix();
60+
logger.info("Creating {} with name {} and state suffix {}", this.getClass().getSimpleName(), stepName, stateSuffix);
61+
this.nodeServerLayout = new NodeServerLayout(locations.buildDir(), stepName, stateSuffix);
62+
}
63+
64+
protected String stateSuffix() {
65+
String packageJsonContent = npmConfig.getPackageJsonContent();
66+
return NpmResourceHelper.md5(packageJsonContent);
6067
}
6168

6269
protected void prepareNodeServerLayout() throws IOException {

lib/src/main/java/com/diffplug/spotless/npm/NpmResourceHelper.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.nio.file.Path;
2222
import java.nio.file.Paths;
2323
import java.nio.file.StandardCopyOption;
24+
import java.security.MessageDigest;
2425
import java.time.Duration;
2526
import java.util.Arrays;
2627
import java.util.Objects;
@@ -122,4 +123,20 @@ static File copyFileToDirAtSubpath(File file, File targetDir, String relativePat
122123
throw ThrowingEx.asRuntime(e);
123124
}
124125
}
126+
127+
static String md5(File file) {
128+
return md5(readUtf8StringFromFile(file));
129+
}
130+
131+
static String md5(String fileContent) {
132+
MessageDigest md = ThrowingEx.get(() -> MessageDigest.getInstance("MD5"));
133+
md.update(fileContent.getBytes(StandardCharsets.UTF_8));
134+
byte[] digest = md.digest();
135+
// convert byte array digest to hex string
136+
StringBuilder sb = new StringBuilder();
137+
for (byte b : digest) {
138+
sb.append(String.format("%02x", b & 0xff));
139+
}
140+
return sb.toString();
141+
}
125142
}

0 commit comments

Comments
 (0)