Skip to content

Commit

Permalink
NPM formatters changed to have their signatures based on package.json…
Browse files Browse the repository at this point in the history
… file instead of the

whole node_modules directory.
  • Loading branch information
lutovich authored and nedtwigg committed Jun 27, 2020
1 parent 5e40de5 commit b695622
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 14 deletions.
43 changes: 43 additions & 0 deletions lib/src/main/java/com/diffplug/spotless/npm/NodeServerLayout.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2020 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.npm;

import java.io.File;

class NodeServerLayout {

private final File nodeModulesDir;
private final File packageJsonFile;
private final File serveJsFile;

NodeServerLayout(File buildDir, String stepName) {
this.nodeModulesDir = new File(buildDir, "spotless-node-modules-" + stepName);
this.packageJsonFile = new File(nodeModulesDir, "package.json");
this.serveJsFile = new File(nodeModulesDir, "serve.js");
}

File nodeModulesDir() {
return nodeModulesDir;
}

File packageJsonFile() {
return packageJsonFile;
}

File serveJsFile() {
return serveJsFile;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ abstract class NpmFormatterStepStateBase implements Serializable {
private static final long serialVersionUID = 1460749955865959948L;

@SuppressWarnings("unused")
private final FileSignature nodeModulesSignature;
private final FileSignature packageJsonSignature;

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

private final String stepName;

protected NpmFormatterStepStateBase(String stepName, NpmConfig npmConfig, File buildDir, @Nullable File npm) throws IOException {
protected NpmFormatterStepStateBase(String stepName, NpmConfig npmConfig, File buildDir,
@Nullable File npm) throws IOException {
this.stepName = requireNonNull(stepName);
this.npmConfig = requireNonNull(npmConfig);
this.npmExecutable = resolveNpm(npm);

this.nodeModulesDir = prepareNodeServer(buildDir);
this.nodeModulesSignature = FileSignature.signAsList(this.nodeModulesDir);
NodeServerLayout layout = prepareNodeServer(buildDir);
this.nodeModulesDir = layout.nodeModulesDir();
this.packageJsonSignature = FileSignature.signAsList(layout.packageJsonFile());
}

private File prepareNodeServer(File buildDir) throws IOException {
File targetDir = new File(buildDir, "spotless-node-modules-" + stepName);
NpmResourceHelper.assertDirectoryExists(targetDir);
NpmResourceHelper.writeUtf8StringToFile(targetDir, "package.json", this.npmConfig.getPackageJsonContent());
NpmResourceHelper.writeUtf8StringToFile(targetDir, "serve.js", this.npmConfig.getServeScriptContent());
runNpmInstall(targetDir);
return targetDir;
private NodeServerLayout prepareNodeServer(File buildDir) throws IOException {
NodeServerLayout layout = new NodeServerLayout(buildDir, stepName);
NpmResourceHelper.assertDirectoryExists(layout.nodeModulesDir());
NpmResourceHelper.writeUtf8StringToFile(layout.packageJsonFile(),
this.npmConfig.getPackageJsonContent());
NpmResourceHelper
.writeUtf8StringToFile(layout.serveJsFile(), this.npmConfig.getServeScriptContent());
runNpmInstall(layout.nodeModulesDir());
return layout;
}

private void runNpmInstall(File npmProjectDir) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ private NpmResourceHelper() {
// no instance required
}

static void writeUtf8StringToFile(File targetDir, String fileName, String stringToWrite) throws IOException {
File packageJsonFile = new File(targetDir, fileName);
Files.write(packageJsonFile.toPath(), stringToWrite.getBytes(StandardCharsets.UTF_8));
static void writeUtf8StringToFile(File file, String stringToWrite) throws IOException {
Files.write(file.toPath(), stringToWrite.getBytes(StandardCharsets.UTF_8));
}

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

0 comments on commit b695622

Please sign in to comment.