Skip to content

Commit

Permalink
fix: Run postinstall for the project itself when there is a script de…
Browse files Browse the repository at this point in the history
…fined (#13917) (#13935)

Fixes #13537
  • Loading branch information
vaadin-bot authored Jun 9, 2022
1 parent 503f6d7 commit e8b1399
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -415,26 +415,21 @@ private void runNpmInstall() throws ExecutionFailedException {
}

List<String> 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",
Expand Down Expand Up @@ -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");
Expand Down
3 changes: 3 additions & 0 deletions flow-tests/test-frontend/vite-basics/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}

0 comments on commit e8b1399

Please sign in to comment.