Skip to content

Commit

Permalink
feat: generate .npmrc with default pnpm option (#9956)
Browse files Browse the repository at this point in the history
Add an .npmrc file so that when installing packages with pnpm, 
it is no longer necessary to pass the --shamefully-hoist flag.
  • Loading branch information
Johannes Eriksson committed Feb 3, 2021
1 parent 367c5f0 commit b871875
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,13 @@ private void runNpmInstall() throws ExecutionFailedException {
+ "with npm by setting system variable -Dvaadin.pnpm.enable=false",
exception);
}
try {
createNpmRcFile();
} catch (IOException exception) {
packageUpdater.log().warn(".npmrc generation failed; pnpm "
+ "package installation may require manaually passing "
+ "the --shamefully-hoist flag", exception);
}
}

List<String> executable;
Expand Down Expand Up @@ -482,6 +489,47 @@ private void createPnpmFile(String versionsPath) throws IOException {
}
}

/*
* Create an .npmrc file the project directory if there is none.
*/
private void createNpmRcFile() throws IOException {
File npmrcFile = new File(packageUpdater.npmFolder.getAbsolutePath(),
".npmrc");
boolean shouldWrite;
if (npmrcFile.exists()) {
List<String> lines = FileUtils.readLines(npmrcFile,
StandardCharsets.UTF_8);
if (lines.stream().anyMatch(line -> line
.contains("NOTICE: this is an auto-generated file"))) {
shouldWrite = true;
} else {
// Looks like this file was not generated by Vaadin
if (lines.stream()
.noneMatch(line -> line.contains("shamefully-hoist"))) {
String message = "Custom .npmrc file ({}) found in "
+ "project; pnpm package installation may "
+ "require passing the --shamefully-hoist flag";
packageUpdater.log().info(message, npmrcFile);
}
shouldWrite = false;
}
} else {
shouldWrite = true;
}
if (shouldWrite) {
try (InputStream content = TaskRunNpmInstall.class
.getResourceAsStream("/npmrc")) {
if (content == null) {
throw new IOException(
"Couldn't find template npmrc in the classpath");
}
FileUtils.copyInputStreamToFile(content, npmrcFile);
packageUpdater.log()
.info("Generated pnpm configuration: '{}'", npmrcFile);
}
}
}

private List<String> modifyPnpmFile(File generatedFile, String versionsPath)
throws IOException {
List<String> lines = FileUtils.readLines(generatedFile,
Expand Down
6 changes: 6 additions & 0 deletions flow-server/src/main/resources/npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#
# NOTICE: this is an auto-generated file
#
# This file sets the default parameters for manual `pnpm install`.
#
shamefully-hoist=true
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import org.apache.commons.io.FileUtils;
import org.hamcrest.CoreMatchers;
Expand Down Expand Up @@ -502,6 +503,63 @@ public void generateVersionsJson_versionsGeneratedFromPackageJson_containsBothDe
versionsJson.toJson());
}

@Test
public void runPnpmInstall_npmRcFileNotFound_newNpmRcFileIsGenerated()
throws IOException, ExecutionFailedException {
TaskRunNpmInstall task = createTask();
task.execute();

File npmRcFile = new File(getNodeUpdater().npmFolder, ".npmrc");
Assert.assertTrue(npmRcFile.exists());
String content = FileUtils.readFileToString(npmRcFile,
StandardCharsets.UTF_8);
Assert.assertTrue(content.contains("shamefully-hoist"));
}

@Test
public void runPnpmInstall_npmRcFileGeneratedByVaadinFound_npmRcFileIsGenerated()
throws IOException, ExecutionFailedException {
File oldNpmRcFile = new File(getNodeUpdater().npmFolder, ".npmrc");
// @formatter:off
String originalContent = "# NOTICE: this is an auto-generated file\n"
+ "shamefully-hoist=true\n"
+ "symlink=true\n";
// @formatter:on
FileUtils.writeStringToFile(oldNpmRcFile, originalContent,
StandardCharsets.UTF_8);

TaskRunNpmInstall task = createTask();
task.execute();

File newNpmRcFile = new File(getNodeUpdater().npmFolder, ".npmrc");
Assert.assertTrue(newNpmRcFile.exists());
String content = FileUtils.readFileToString(newNpmRcFile,
StandardCharsets.UTF_8);
Assert.assertTrue(content.contains("shamefully-hoist"));
Assert.assertFalse(content.contains("symlink=true"));
}

@Test
public void runPnpmInstall_customNpmRcFileFound_npmRcFileIsNotGenerated()
throws IOException, ExecutionFailedException {
File oldNpmRcFile = new File(getNodeUpdater().npmFolder, ".npmrc");
// @formatter:off
String originalContent = "# A custom npmrc file for my project\n"
+ "symlink=true\n";
// @formatter:on
FileUtils.writeStringToFile(oldNpmRcFile, originalContent,
StandardCharsets.UTF_8);

TaskRunNpmInstall task = createTask();
task.execute();

File newNpmRcFile = new File(getNodeUpdater().npmFolder, ".npmrc");
Assert.assertTrue(newNpmRcFile.exists());
String content = FileUtils.readFileToString(newNpmRcFile,
StandardCharsets.UTF_8);
Assert.assertEquals(originalContent, content);
}

@Override
protected String getToolName() {
return "pnpm";
Expand Down

0 comments on commit b871875

Please sign in to comment.