Skip to content

Commit 721890b

Browse files
committed
Return the installed version of an NPM recipe bundle
1 parent 8d36199 commit 721890b

8 files changed

Lines changed: 38 additions & 22 deletions

File tree

rewrite-core/src/main/java/org/openrewrite/marketplace/RecipeMarketplace.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,7 @@
2222
import org.openrewrite.NlsRewrite;
2323
import org.openrewrite.config.CategoryDescriptor;
2424

25-
import java.util.ArrayList;
26-
import java.util.List;
27-
import java.util.Set;
28-
import java.util.TreeSet;
29-
30-
import static java.util.Collections.addAll;
25+
import java.util.*;
3126

3227
@Incubating(since = "8.66.0")
3328
public class RecipeMarketplace {
@@ -38,9 +33,9 @@ public class RecipeMarketplace {
3833

3934
private final @Getter List<RecipeBundleResolver> resolvers = new ArrayList<>();
4035

41-
public RecipeMarketplace setResolvers(RecipeBundleResolver... resolvers) {
36+
public RecipeMarketplace setResolvers(Collection<RecipeBundleResolver> resolvers) {
4237
this.resolvers.clear();
43-
addAll(this.resolvers, resolvers);
38+
this.resolvers.addAll(resolvers);
4439
return this;
4540
}
4641

rewrite-core/src/test/java/org/openrewrite/rpc/RewriteRpcTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@
3434
import java.io.IOException;
3535
import java.io.PipedInputStream;
3636
import java.io.PipedOutputStream;
37+
import java.util.Collections;
3738
import java.util.Map;
3839
import java.util.concurrent.CountDownLatch;
3940

41+
import static java.util.Collections.singletonList;
4042
import static java.util.Objects.requireNonNull;
4143
import static org.assertj.core.api.Assertions.assertThat;
4244
import static org.openrewrite.marketplace.RecipeBundle.runtimeClasspath;
@@ -60,7 +62,7 @@ void before() throws IOException {
6062
PipedInputStream clientIn = new PipedInputStream(serverOut);
6163

6264
marketplace = env.toMarketplace(runtimeClasspath())
63-
.setResolvers(new TestRecipeBundleResolver());
65+
.setResolvers(singletonList(new TestRecipeBundleResolver()));
6466

6567
client = new RewriteRpc(new JsonRpc(new HeaderDelimitedMessageHandler(clientIn, clientOut)), marketplace)
6668
.batchSize(1);

rewrite-javascript/rewrite/src/rpc/request/install-recipes.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {RecipeMarketplace} from "../../marketplace";
2222

2323
export interface InstallRecipesResponse {
2424
recipesInstalled: number
25+
version?: string
2526
}
2627

2728
/**
@@ -81,6 +82,7 @@ export class InstallRecipes {
8182
let resolvedPath;
8283
let recipesName = request.recipes;
8384

85+
let installedVersion: string | undefined;
8486
if (typeof request.recipes === "object") {
8587
const recipePackage = request.recipes;
8688
const absoluteInstallDir = path.isAbsolute(installDir) ? installDir : path.join(process.cwd(), installDir);
@@ -112,6 +114,13 @@ export class InstallRecipes {
112114
await spawnNpmCommand("npm", ["install", packageSpec, "--no-fund"], absoluteInstallDir, logger);
113115
resolvedPath = require.resolve(path.join(absoluteInstallDir, "node_modules", recipePackage.packageName));
114116
recipesName = request.recipes.packageName;
117+
118+
// Read the installed package's version from its package.json
119+
const installedPackageJsonPath = path.join(absoluteInstallDir, "node_modules", recipePackage.packageName, "package.json");
120+
if (fs.existsSync(installedPackageJsonPath)) {
121+
const installedPackageJson = JSON.parse(fs.readFileSync(installedPackageJsonPath, "utf8"));
122+
installedVersion = installedPackageJson.version;
123+
}
115124
} else {
116125
resolvedPath = request.recipes;
117126
}
@@ -139,7 +148,10 @@ export class InstallRecipes {
139148
throw new Error(`${recipesName} does not export an 'activate' function`);
140149
}
141150

142-
return {recipesInstalled: marketplace.allRecipes().length - beforeInstall};
151+
return {
152+
recipesInstalled: marketplace.allRecipes().length - beforeInstall,
153+
version: installedVersion
154+
};
143155
}
144156
)
145157
);

rewrite-javascript/src/integTest/java/org/openrewrite/javascript/rpc/JavaScriptRewriteRpcTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ void runSearchRecipeWithJavaRecipeActingAsPrecondition(boolean matchesPreconditi
163163

164164
@Test
165165
void printJava() {
166-
assertThat(client().installRecipes(new File("rewrite/dist-fixtures/modify-all-trees.js")))
166+
assertThat(client().installRecipes(new File("rewrite/dist-fixtures/modify-all-trees.js")).getRecipesInstalled())
167167
.isEqualTo(1);
168168
Recipe modifyAll = client().prepareRecipe("org.openrewrite.java.test.modify-all-trees");
169169

@@ -190,7 +190,7 @@ public J preVisit(J tree, ExecutionContext ctx) {
190190

191191
@Test
192192
void installRecipesFromNpm() {
193-
assertThat(client().installRecipes("@openrewrite/recipes-npm")).isEqualTo(1);
193+
assertThat(client().installRecipes("@openrewrite/recipes-npm").getRecipesInstalled()).isEqualTo(1);
194194
assertThat(client().getMarketplace(new RecipeBundle("npm", "@openrewrite/recipes-npm", null, null, null)).getAllRecipes()).satisfiesExactly(
195195
d -> {
196196
assertThat(d.getDisplayName()).isEqualTo("Change version in `package.json`");
@@ -434,7 +434,7 @@ void parseProjectWithExclusions(@TempDir Path projectDir) throws IOException {
434434
private void installRecipes() {
435435
File exampleRecipes = new File("rewrite/dist-fixtures/example-recipe.js");
436436
assertThat(exampleRecipes).exists();
437-
assertThat(client().installRecipes(exampleRecipes)).isGreaterThan(0);
437+
assertThat(client().installRecipes(exampleRecipes).getRecipesInstalled()).isGreaterThan(0);
438438
}
439439

440440
private JavaScriptRewriteRpc client() {

rewrite-javascript/src/integTest/java/org/openrewrite/javascript/rpc/JavaToJavaScriptRpcTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static void beforeSuite() {
4747
.recipe(toRecipe(() -> {
4848
JavaScriptRewriteRpc client = JavaScriptRewriteRpc.getOrStart();
4949

50-
assertThat(client.installRecipes(new File("rewrite/dist-fixtures/modify-all-trees.js")))
50+
assertThat(client.installRecipes(new File("rewrite/dist-fixtures/modify-all-trees.js")).getRecipesInstalled())
5151
.isEqualTo(1);
5252
Recipe modifyAll = client.prepareRecipe("org.openrewrite.java.test.modify-all-trees");
5353

rewrite-javascript/src/main/java/org/openrewrite/javascript/marketplace/NpmRecipeBundleResolver.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.openrewrite.javascript.marketplace;
1717

1818
import lombok.RequiredArgsConstructor;
19+
import org.openrewrite.javascript.rpc.InstallRecipesResponse;
1920
import org.openrewrite.javascript.rpc.JavaScriptRewriteRpc;
2021
import org.openrewrite.marketplace.RecipeBundle;
2122
import org.openrewrite.marketplace.RecipeBundleReader;
@@ -37,10 +38,14 @@ public String getEcosystem() {
3738
@Override
3839
public RecipeBundleReader resolve(RecipeBundle bundle) {
3940
Path pkgPath = Paths.get(bundle.getPackageName());
41+
InstallRecipesResponse response;
4042
if (Files.exists(pkgPath)) {
41-
rpc.installRecipes(pkgPath.toFile());
43+
response = rpc.installRecipes(pkgPath.toFile());
4244
} else {
43-
rpc.installRecipes(bundle.getPackageName(), bundle.getVersion());
45+
response = rpc.installRecipes(bundle.getPackageName(), bundle.getVersion());
46+
}
47+
if (response.getVersion() != null) {
48+
bundle.setVersion(response.getVersion());
4449
}
4550
return new NpmRecipeBundleReader(bundle, rpc);
4651
}

rewrite-javascript/src/main/java/org/openrewrite/javascript/rpc/InstallRecipesResponse.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
package org.openrewrite.javascript.rpc;
1717

1818
import lombok.Value;
19+
import org.jspecify.annotations.Nullable;
1920

2021
@Value
21-
class InstallRecipesResponse {
22+
public class InstallRecipesResponse {
2223
int recipesInstalled;
24+
@Nullable String version;
2325
}

rewrite-javascript/src/main/java/org/openrewrite/javascript/rpc/JavaScriptRewriteRpc.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,25 +87,25 @@ public static void shutdownCurrent() {
8787
MANAGER.shutdown();
8888
}
8989

90-
public int installRecipes(File recipes) {
90+
public InstallRecipesResponse installRecipes(File recipes) {
9191
return send(
9292
"InstallRecipes",
9393
new InstallRecipesByFile(recipes.getAbsoluteFile().toPath()),
9494
InstallRecipesResponse.class
95-
).getRecipesInstalled();
95+
);
9696
}
9797

98-
public int installRecipes(String packageName) {
98+
public InstallRecipesResponse installRecipes(String packageName) {
9999
return installRecipes(packageName, null);
100100
}
101101

102-
public int installRecipes(String packageName, @Nullable String version) {
102+
public InstallRecipesResponse installRecipes(String packageName, @Nullable String version) {
103103
return send(
104104
"InstallRecipes",
105105
new InstallRecipesByPackage(
106106
new InstallRecipesByPackage.Package(packageName, version)),
107107
InstallRecipesResponse.class
108-
).getRecipesInstalled();
108+
);
109109
}
110110

111111
/**

0 commit comments

Comments
 (0)