forked from jhipster/jhipster-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: lazily run npm install when applying 'prettier' and client modules
If a package-lock.json is detected, and npm command is present, run npm install. Fixes jhipster#11106
- Loading branch information
Showing
18 changed files
with
215 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/tech/jhipster/lite/module/domain/npm/NpmLazyInstaller.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package tech.jhipster.lite.module.domain.npm; | ||
|
||
import tech.jhipster.lite.module.domain.properties.JHipsterProjectFolder; | ||
|
||
/** | ||
* Run npm install if a previous npm install has already been done. | ||
*/ | ||
public interface NpmLazyInstaller { | ||
void runInstallIn(JHipsterProjectFolder folder); | ||
} |
92 changes: 92 additions & 0 deletions
92
...va/tech/jhipster/lite/module/infrastructure/secondary/npm/FileSystemNpmLazyInstaller.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package tech.jhipster.lite.module.infrastructure.secondary.npm; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.concurrent.TimeUnit; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Service; | ||
import tech.jhipster.lite.module.domain.npm.NpmLazyInstaller; | ||
import tech.jhipster.lite.module.domain.properties.JHipsterProjectFolder; | ||
import tech.jhipster.lite.shared.error.domain.Assert; | ||
import tech.jhipster.lite.shared.npmdetector.infrastructure.secondary.NpmInstallationReader; | ||
import tech.jhipster.lite.shared.npmdetector.infrastructure.secondary.NpmInstallationType; | ||
|
||
/** | ||
* Launches npm install if the npm command is detected and if an existing package-lock.json is present. | ||
*/ | ||
@Service | ||
class FileSystemNpmLazyInstaller implements NpmLazyInstaller { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(FileSystemNpmLazyInstaller.class); | ||
private final NpmInstallationReader npmInstallationReader = new NpmInstallationReader(); | ||
|
||
public void runInstallIn(JHipsterProjectFolder folder) { | ||
Assert.notNull("folder", folder); | ||
|
||
if (!folder.fileExists("package-lock.json")) { | ||
log.info("No package-lock.json found, npm install skipped"); | ||
return; | ||
} | ||
|
||
NpmInstallationType npmInstallationType = npmInstallationReader.get(); | ||
switch (npmInstallationType) { | ||
case UNIX: | ||
execute(folder, "npm", "install"); | ||
break; | ||
case WINDOWS: | ||
execute(folder, "npm.cmd", "install"); | ||
break; | ||
case NONE: | ||
log.info("No npm installed, can't install project"); | ||
break; | ||
} | ||
} | ||
|
||
private void execute(JHipsterProjectFolder path, String... commands) { | ||
try { | ||
Process process = new ProcessBuilder(commands).directory(folderFile(path)).start(); | ||
|
||
if (failedExecution(process)) { | ||
throw new NpmInstallException("Error during formatting, process failed"); | ||
} | ||
|
||
traceProcess(String.join(" ", commands), process); | ||
} catch (IOException e) { | ||
throw new NpmInstallException("Error during formatting: " + e.getMessage(), e); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
|
||
throw new NpmInstallException("Error during formatting: " + e.getMessage(), e); | ||
} | ||
} | ||
|
||
private File folderFile(JHipsterProjectFolder path) { | ||
return new File(path.get()); | ||
} | ||
|
||
private boolean failedExecution(Process process) throws InterruptedException { | ||
return !process.waitFor(1, TimeUnit.MINUTES); | ||
} | ||
|
||
private void traceProcess(String command, Process process) throws IOException { | ||
if (log.isTraceEnabled()) { | ||
log.trace("{}: {}", command, read(process.getInputStream())); | ||
} | ||
|
||
if (log.isErrorEnabled()) { | ||
String errors = read(process.getErrorStream()); | ||
|
||
if (StringUtils.isNotBlank(errors)) { | ||
log.error("Error during {}: {}", command, errors); | ||
} | ||
} | ||
} | ||
|
||
private String read(InputStream stream) throws IOException { | ||
return new String(stream.readAllBytes(), StandardCharsets.UTF_8).intern(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/tech/jhipster/lite/module/infrastructure/secondary/npm/NpmErrorKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package tech.jhipster.lite.module.infrastructure.secondary.npm; | ||
|
||
import tech.jhipster.lite.shared.error.domain.ErrorKey; | ||
|
||
enum NpmErrorKey implements ErrorKey { | ||
INSTALL_ERROR("install-error"); | ||
|
||
private final String key; | ||
|
||
NpmErrorKey(String key) { | ||
this.key = key; | ||
} | ||
|
||
@Override | ||
public String get() { | ||
return key; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...main/java/tech/jhipster/lite/module/infrastructure/secondary/npm/NpmInstallException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package tech.jhipster.lite.module.infrastructure.secondary.npm; | ||
|
||
import tech.jhipster.lite.shared.error.domain.GeneratorException; | ||
import tech.jhipster.lite.shared.generation.domain.ExcludeFromGeneratedCodeCoverage; | ||
|
||
@ExcludeFromGeneratedCodeCoverage | ||
class NpmInstallException extends GeneratorException { | ||
|
||
public NpmInstallException(String message) { | ||
super(internalServerError(NpmErrorKey.INSTALL_ERROR).message(message)); | ||
} | ||
|
||
public NpmInstallException(String message, Throwable cause) { | ||
super(internalServerError(NpmErrorKey.INSTALL_ERROR).message(message).cause(cause)); | ||
} | ||
} |
7 changes: 0 additions & 7 deletions
7
src/main/java/tech/jhipster/lite/project/infrastructure/secondary/NpmInstallationType.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...a/tech/jhipster/lite/shared/npmdetector/infrastructure/secondary/NpmInstallationType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package tech.jhipster.lite.shared.npmdetector.infrastructure.secondary; | ||
|
||
public enum NpmInstallationType { | ||
NONE, | ||
UNIX, | ||
WINDOWS, | ||
} |
2 changes: 2 additions & 0 deletions
2
src/main/java/tech/jhipster/lite/shared/npmdetector/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@tech.jhipster.lite.SharedKernel | ||
package tech.jhipster.lite.shared.npmdetector; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.