From 24583cd6e320bebaa7ef4b576b3564f65151e179 Mon Sep 17 00:00:00 2001 From: Artur Date: Thu, 9 Jun 2022 07:11:12 +0300 Subject: [PATCH] fix: Run postinstall for the project itself when there is a script defined (#13917) Fixes #13537 --- .../server/frontend/TaskRunNpmInstall.java | 29 ++++++++++++------- .../test-frontend/vite-basics/package.json | 3 ++ .../com/vaadin/viteapp/PostinstallIT.java | 19 ++++++++++++ 3 files changed, 41 insertions(+), 10 deletions(-) create mode 100644 flow-tests/test-frontend/vite-basics/src/test/java/com/vaadin/viteapp/PostinstallIT.java diff --git a/flow-server/src/main/java/com/vaadin/flow/server/frontend/TaskRunNpmInstall.java b/flow-server/src/main/java/com/vaadin/flow/server/frontend/TaskRunNpmInstall.java index ff0767786f9..4738f2aa2bc 100644 --- a/flow-server/src/main/java/com/vaadin/flow/server/frontend/TaskRunNpmInstall.java +++ b/flow-server/src/main/java/com/vaadin/flow/server/frontend/TaskRunNpmInstall.java @@ -415,26 +415,21 @@ private void runNpmInstall() throws ExecutionFailedException { } List postinstallPackages = new ArrayList<>(); + postinstallPackages.add("."); postinstallPackages.add("esbuild"); postinstallPackages.add("@vaadin/vaadin-usage-statistics"); postinstallPackages.addAll(additionalPostinstallPackages); for (String postinstallPackage : postinstallPackages) { - if (postinstallPackage.trim().equals("")) { + File packageJsonFile = getPackageJsonForModule(postinstallPackage); + if (packageJsonFile == null || !packageJsonFile.exists()) { continue; } + File packageFolder = packageJsonFile.getParentFile(); - // Execute "npm run postinstall" in the desired folders in - // node_modules - File packageFolder = new File(packageUpdater.nodeModulesFolder, - postinstallPackage); - if (!packageFolder.exists()) { - continue; - } try { JsonObject packageJson = TaskGeneratePackageJson - .getJsonFileContent( - new File(packageFolder, "package.json")); + .getJsonFileContent(packageJsonFile); if (!containsPostinstallScript(packageJson)) { logger.debug( "Skipping postinstall for '{}' as no postinstall script was found in the package.json", @@ -468,6 +463,20 @@ private void runNpmInstall() throws ExecutionFailedException { } + private File getPackageJsonForModule(String module) { + if (module.trim().equals("")) { + return null; + } + if (module.equals(".")) { + // The location of the project package.json + return new File(packageUpdater.npmFolder, "package.json"); + } + + return new File(new File(packageUpdater.nodeModulesFolder, module), + "package.json"); + + } + private boolean containsPostinstallScript(JsonObject packageJson) { return packageJson != null && packageJson.hasKey("scripts") && packageJson.getObject("scripts").hasKey("postinstall"); diff --git a/flow-tests/test-frontend/vite-basics/package.json b/flow-tests/test-frontend/vite-basics/package.json index f444e024b6e..70b6950e8f5 100644 --- a/flow-tests/test-frontend/vite-basics/package.json +++ b/flow-tests/test-frontend/vite-basics/package.json @@ -1,6 +1,9 @@ { "name": "no-name", "license": "UNLICENSED", + "scripts": { + "postinstall": "node -e \"fs.writeFileSync('target/classes/main.postinstall', 'hello')\"" + }, "dependencies": { "@polymer/polymer": "3.4.1", "@testscope/all": "../vite-test-assets/packages/@testscope/all", diff --git a/flow-tests/test-frontend/vite-basics/src/test/java/com/vaadin/viteapp/PostinstallIT.java b/flow-tests/test-frontend/vite-basics/src/test/java/com/vaadin/viteapp/PostinstallIT.java new file mode 100644 index 00000000000..bd3bd0d791c --- /dev/null +++ b/flow-tests/test-frontend/vite-basics/src/test/java/com/vaadin/viteapp/PostinstallIT.java @@ -0,0 +1,19 @@ +package com.vaadin.viteapp; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; + +import org.apache.commons.io.IOUtils; +import org.junit.Assert; +import org.junit.Test; + +public class PostinstallIT extends ViteDevModeIT { + + @Test + public void postinstallRanForProject() throws IOException { + waitForDevServer(); // This is what runs the postinstall script + Assert.assertEquals("hello", IOUtils.toString( + getClass().getClassLoader().getResource("main.postinstall"), + StandardCharsets.UTF_8)); + } +}