Skip to content

Commit

Permalink
fix: Remove disused plugins from package.json (#12337)
Browse files Browse the repository at this point in the history
It does not delete them from `target` as this will go away with the next clean.
  • Loading branch information
Johannes Eriksson committed Nov 11, 2021
1 parent 57bfc5e commit 5583283
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,28 @@ private void addWebpackPlugins(JsonObject packageJson) {
packageJson.put(DEV_DEPENDENCIES, devDependencies);
}

String atVaadinPrefix = "@vaadin/";
String pluginTargetPrefix = "./"
+ (npmFolder.toPath().relativize(targetFolder) + "/")
.replace('\\', '/');
plugins.stream().filter(plugin -> targetFolder.toFile().exists())
.forEach(plugin -> {
String pluginTarget = "./" + (npmFolder.toPath()
.relativize(targetFolder).toString() + "/" + plugin)
.replace('\\', '/');
devDependencies.put("@vaadin/" + plugin, pluginTarget);
String pluginTarget = pluginTargetPrefix + plugin;
devDependencies.put(atVaadinPrefix + plugin, pluginTarget);
});

// Remove plugins previously installed but no longer needed
for (String depKey : devDependencies.keys()) {
String depVersion = devDependencies.getString(depKey);
if (depKey.startsWith(atVaadinPrefix)
&& depVersion.startsWith(pluginTargetPrefix)) {
final String pluginName = depKey
.substring(atVaadinPrefix.length());
if (!plugins.contains(pluginName)) {
devDependencies.remove(depKey);
}
}
}
}

JsonObject getResourcesPackageJson() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.vaadin.flow.server.frontend;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.Set;
Expand Down Expand Up @@ -231,6 +232,22 @@ public void getJsonFileContent_incorrectPackageJsonContent_throwsExceptionWithFi
StringContains.containsString("broken-package.json"));
}

@Test
public void removedDisusedPlugins() throws IOException {
File packageJson = new File(npmFolder, "package.json");
FileWriter packageJsonWriter = new FileWriter(packageJson);
packageJsonWriter.write("{\"devDependencies\": {"
+ "\"@vaadin/some-old-plugin\": \"./target/plugins/some-old-plugin\","
+ "\"@vaadin/application-theme-plugin\": \"./target/plugins/application-theme-plugin\"}"
+ "}");
packageJsonWriter.close();
JsonObject actualDevDeps = nodeUpdater.getPackageJson()
.getObject(NodeUpdater.DEV_DEPENDENCIES);
Assert.assertFalse(actualDevDeps.hasKey("some-old-plugin"));
Assert.assertTrue(
actualDevDeps.hasKey("@vaadin/application-theme-plugin"));
}

private String getPolymerVersion(JsonObject object) {
JsonObject deps = object.get("dependencies");
String version = deps.getString("@polymer/polymer");
Expand Down

0 comments on commit 5583283

Please sign in to comment.