Skip to content

Commit b695622

Browse files
lutovichnedtwigg
authored andcommitted
NPM formatters changed to have their signatures based on package.json file instead of the
whole node_modules directory.
1 parent 5e40de5 commit b695622

File tree

3 files changed

+60
-14
lines changed

3 files changed

+60
-14
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2020 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+
20+
class NodeServerLayout {
21+
22+
private final File nodeModulesDir;
23+
private final File packageJsonFile;
24+
private final File serveJsFile;
25+
26+
NodeServerLayout(File buildDir, String stepName) {
27+
this.nodeModulesDir = new File(buildDir, "spotless-node-modules-" + stepName);
28+
this.packageJsonFile = new File(nodeModulesDir, "package.json");
29+
this.serveJsFile = new File(nodeModulesDir, "serve.js");
30+
}
31+
32+
File nodeModulesDir() {
33+
return nodeModulesDir;
34+
}
35+
36+
File packageJsonFile() {
37+
return packageJsonFile;
38+
}
39+
40+
File serveJsFile() {
41+
return serveJsFile;
42+
}
43+
}

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ abstract class NpmFormatterStepStateBase implements Serializable {
4444
private static final long serialVersionUID = 1460749955865959948L;
4545

4646
@SuppressWarnings("unused")
47-
private final FileSignature nodeModulesSignature;
47+
private final FileSignature packageJsonSignature;
4848

4949
@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
5050
public final transient File nodeModulesDir;
@@ -56,22 +56,26 @@ abstract class NpmFormatterStepStateBase implements Serializable {
5656

5757
private final String stepName;
5858

59-
protected NpmFormatterStepStateBase(String stepName, NpmConfig npmConfig, File buildDir, @Nullable File npm) throws IOException {
59+
protected NpmFormatterStepStateBase(String stepName, NpmConfig npmConfig, File buildDir,
60+
@Nullable File npm) throws IOException {
6061
this.stepName = requireNonNull(stepName);
6162
this.npmConfig = requireNonNull(npmConfig);
6263
this.npmExecutable = resolveNpm(npm);
6364

64-
this.nodeModulesDir = prepareNodeServer(buildDir);
65-
this.nodeModulesSignature = FileSignature.signAsList(this.nodeModulesDir);
65+
NodeServerLayout layout = prepareNodeServer(buildDir);
66+
this.nodeModulesDir = layout.nodeModulesDir();
67+
this.packageJsonSignature = FileSignature.signAsList(layout.packageJsonFile());
6668
}
6769

68-
private File prepareNodeServer(File buildDir) throws IOException {
69-
File targetDir = new File(buildDir, "spotless-node-modules-" + stepName);
70-
NpmResourceHelper.assertDirectoryExists(targetDir);
71-
NpmResourceHelper.writeUtf8StringToFile(targetDir, "package.json", this.npmConfig.getPackageJsonContent());
72-
NpmResourceHelper.writeUtf8StringToFile(targetDir, "serve.js", this.npmConfig.getServeScriptContent());
73-
runNpmInstall(targetDir);
74-
return targetDir;
70+
private NodeServerLayout prepareNodeServer(File buildDir) throws IOException {
71+
NodeServerLayout layout = new NodeServerLayout(buildDir, stepName);
72+
NpmResourceHelper.assertDirectoryExists(layout.nodeModulesDir());
73+
NpmResourceHelper.writeUtf8StringToFile(layout.packageJsonFile(),
74+
this.npmConfig.getPackageJsonContent());
75+
NpmResourceHelper
76+
.writeUtf8StringToFile(layout.serveJsFile(), this.npmConfig.getServeScriptContent());
77+
runNpmInstall(layout.nodeModulesDir());
78+
return layout;
7579
}
7680

7781
private void runNpmInstall(File npmProjectDir) throws IOException {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ private NpmResourceHelper() {
2828
// no instance required
2929
}
3030

31-
static void writeUtf8StringToFile(File targetDir, String fileName, String stringToWrite) throws IOException {
32-
File packageJsonFile = new File(targetDir, fileName);
33-
Files.write(packageJsonFile.toPath(), stringToWrite.getBytes(StandardCharsets.UTF_8));
31+
static void writeUtf8StringToFile(File file, String stringToWrite) throws IOException {
32+
Files.write(file.toPath(), stringToWrite.getBytes(StandardCharsets.UTF_8));
3433
}
3534

3635
static void writeUtf8StringToOutputStream(String stringToWrite, OutputStream outputStream) throws IOException {

0 commit comments

Comments
 (0)