Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Run postinstall for the project itself when there is a script defined (#13917) (CP: 23.1) #13935

Merged
merged 1 commit into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}