Skip to content

1480 speedup npm install step for npm based formatters #1590

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

Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
1480: add trace logger
  • Loading branch information
simschla committed Feb 26, 2023
commit 4b663d48639633cc64bef0e9cb6292dab6d30e94
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@
import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.ProcessRunner.LongRunningProcess;
import com.diffplug.spotless.ThrowingEx;
import com.diffplug.spotless.TimedLogger;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

abstract class NpmFormatterStepStateBase implements Serializable {

private static final Logger logger = LoggerFactory.getLogger(NpmFormatterStepStateBase.class);

private static final TimedLogger timedLogger = TimedLogger.forLogger(logger);

private static final long serialVersionUID = 1460749955865959948L;

@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
Expand All @@ -61,26 +64,24 @@ protected NpmFormatterStepStateBase(String stepName, NpmConfig npmConfig, NpmFor

protected void prepareNodeServerLayout() throws IOException {
final long started = System.currentTimeMillis();
// maybe introduce trace logger?
logger.info("Preparing {} for npm step {}.", this.nodeServerLayout, getClass().getName());
NpmResourceHelper.assertDirectoryExists(nodeServerLayout.nodeModulesDir());
NpmResourceHelper.writeUtf8StringToFile(nodeServerLayout.packageJsonFile(),
this.npmConfig.getPackageJsonContent());
NpmResourceHelper
.writeUtf8StringToFile(nodeServerLayout.serveJsFile(), this.npmConfig.getServeScriptContent());
if (this.npmConfig.getNpmrcContent() != null) {
NpmResourceHelper.writeUtf8StringToFile(nodeServerLayout.npmrcFile(), this.npmConfig.getNpmrcContent());
} else {
NpmResourceHelper.deleteFileIfExists(nodeServerLayout.npmrcFile());
}
logger.info("Prepared {} for npm step {} in {} ms.", this.nodeServerLayout, getClass().getName(), System.currentTimeMillis() - started);

timedLogger.withInfo("Preparing {} for npm step {}.", this.nodeServerLayout, getClass().getName()).run(() -> {
NpmResourceHelper.assertDirectoryExists(nodeServerLayout.nodeModulesDir());
NpmResourceHelper.writeUtf8StringToFile(nodeServerLayout.packageJsonFile(),
this.npmConfig.getPackageJsonContent());
NpmResourceHelper
.writeUtf8StringToFile(nodeServerLayout.serveJsFile(), this.npmConfig.getServeScriptContent());
if (this.npmConfig.getNpmrcContent() != null) {
NpmResourceHelper.writeUtf8StringToFile(nodeServerLayout.npmrcFile(), this.npmConfig.getNpmrcContent());
} else {
NpmResourceHelper.deleteFileIfExists(nodeServerLayout.npmrcFile());
}
});
}

protected void prepareNodeServer() throws IOException {
final long started = System.currentTimeMillis();
logger.info("running npm install in {} for npm step {}", this.nodeServerLayout.nodeModulesDir(), getClass().getName());
runNpmInstall(nodeServerLayout.nodeModulesDir());
logger.info("npm install finished in {} ms in {} for npm step {}", System.currentTimeMillis() - started, this.nodeServerLayout.nodeModulesDir(), getClass().getName());
timedLogger.withInfo("Running npm install in {} for npm step {}.", this.nodeServerLayout.nodeModulesDir(), getClass().getName())
.run(() -> runNpmInstall(nodeServerLayout.nodeModulesDir()));
}

private void runNpmInstall(File npmProjectDir) throws IOException {
Expand Down