-
-
Notifications
You must be signed in to change notification settings - Fork 210
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: lazily run npm install when applying 'prettier' and client modules #11113
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
} |
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"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe it should be |
||||||
break; | ||||||
case WINDOWS: | ||||||
execute(folder, "npm.cmd", "install"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe it should be |
||||||
break; | ||||||
case NONE: | ||||||
log.info("No npm installed, can't install project"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it could be used for another command, so it should not be related to formatting |
||||||
} | ||||||
|
||||||
traceProcess(String.join(" ", commands), process); | ||||||
} catch (IOException e) { | ||||||
throw new NpmInstallException("Error during formatting: " + e.getMessage(), e); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it could be used for another command, so it should not be related to formatting |
||||||
} catch (InterruptedException e) { | ||||||
Thread.currentThread().interrupt(); | ||||||
|
||||||
throw new NpmInstallException("Error during formatting: " + e.getMessage(), e); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it could be used for another command, so it should not be related to formatting |
||||||
} | ||||||
} | ||||||
|
||||||
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(); | ||||||
} | ||||||
} |
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; | ||
} | ||
} |
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)); | ||
} | ||
} |
This file was deleted.
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, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@tech.jhipster.lite.SharedKernel | ||
package tech.jhipster.lite.shared.npmdetector; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.