Skip to content

Commit

Permalink
fix: better exception for failing removal (#12659) (#12668)
Browse files Browse the repository at this point in the history
Update exception for failing to
remove node_modules folder.

Closes #12655

Co-authored-by: caalador <[email protected]>
  • Loading branch information
vaadin-bot and caalador authored Jan 3, 2022
1 parent ea9ca18 commit f182faa
Showing 1 changed file with 20 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.stream.Collectors;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;

import com.vaadin.flow.server.Constants;
import com.vaadin.flow.server.ExecutionFailedException;
Expand Down Expand Up @@ -97,8 +98,6 @@ public class TaskRunNpmInstall implements FallibleCommand {
/**
* Create an instance of the command.
*
* @param classFinder
* a reusable class finder
* @param packageUpdater
* package-updater instance used for checking if previous
* execution modified the package.json file
Expand Down Expand Up @@ -298,13 +297,7 @@ private boolean isVaadinHashUpdated() {
*/
private void runNpmInstall() throws ExecutionFailedException {
// Do possible cleaning before generating any new files.
try {
cleanUp();
} catch (IOException exception) {
throw new ExecutionFailedException("Couldn't remove "
+ packageUpdater.nodeModulesFolder + " directory",
exception);
}
cleanUp();

if (enablePnpm) {
try {
Expand Down Expand Up @@ -459,27 +452,42 @@ private List<String> modifyPnpmFile(File generatedFile, String versionsPath)
return lines;
}

private void cleanUp() throws IOException {
private void cleanUp() throws ExecutionFailedException {
if (!packageUpdater.nodeModulesFolder.exists()) {
return;
}
File modulesYaml = new File(packageUpdater.nodeModulesFolder,
MODULES_YAML);
boolean hasModulesYaml = modulesYaml.exists() && modulesYaml.isFile();
if (!enablePnpm && hasModulesYaml) {
FileUtils.forceDelete(packageUpdater.nodeModulesFolder);
deleteNodeModules(packageUpdater.nodeModulesFolder);
} else if (enablePnpm && !hasModulesYaml) {
// presence of .staging dir with a "pnpm-*" folder means that pnpm
// download is in progress, don't remove anything in this case
File staging = new File(packageUpdater.nodeModulesFolder,
".staging");
if (!staging.isDirectory() || staging.listFiles(
(dir, name) -> name.startsWith("pnpm-")).length == 0) {
FileUtils.forceDelete(packageUpdater.nodeModulesFolder);
deleteNodeModules(packageUpdater.nodeModulesFolder);
}
}
}

private void deleteNodeModules(File nodeModulesFolder)
throws ExecutionFailedException {
try {
FileUtils.forceDelete(nodeModulesFolder);
} catch (IOException exception) {
Logger log = packageUpdater.log();
log.debug("Exception removing node_modules", exception);
log.error("Failed to remove '"
+ packageUpdater.nodeModulesFolder.getAbsolutePath()
+ "'. Please remove it manually.");
throw new ExecutionFailedException(
"Exception removing node_modules. Please remove it manually.");
}
}

private void validateInstalledNpm(FrontendTools tools)
throws IllegalStateException {
File npmCacheDir = null;
Expand Down

0 comments on commit f182faa

Please sign in to comment.