Skip to content

Commit a6e711d

Browse files
committed
Extract Runtime into its own thing, so that Prettier can roundtrip through serialization.
1 parent 4c23f57 commit a6e711d

File tree

2 files changed

+60
-50
lines changed

2 files changed

+60
-50
lines changed

lib/src/main/java/com/diffplug/spotless/npm/NpmFormatterStepStateBase.java

Lines changed: 59 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -47,75 +47,85 @@ abstract class NpmFormatterStepStateBase implements Serializable {
4747
private final NpmConfig npmConfig;
4848

4949
public final NpmFormatterStepLocations locations;
50-
protected final transient NodeServerLayout nodeServerLayout;
51-
private final transient NodeServeApp nodeServeApp;
5250

5351
protected NpmFormatterStepStateBase(String stepName, NpmConfig npmConfig, NpmFormatterStepLocations locations) throws IOException {
5452
this.stepName = requireNonNull(stepName);
5553
this.npmConfig = requireNonNull(npmConfig);
5654
this.locations = locations;
57-
this.nodeServerLayout = new NodeServerLayout(locations.buildDir(), npmConfig.getPackageJsonContent());
58-
this.nodeServeApp = new NodeServeApp(nodeServerLayout, npmConfig, locations);
5955
}
6056

61-
protected void prepareNodeServerLayout() throws IOException {
62-
nodeServeApp.prepareNodeAppLayout();
57+
public Runtime toRuntime() {
58+
return new Runtime(this);
6359
}
6460

65-
protected void prepareNodeServer() throws IOException {
66-
nodeServeApp.npmInstall();
67-
}
61+
public static class Runtime {
62+
private final NodeServerLayout nodeServerLayout;
63+
private final NodeServeApp nodeServeApp;
6864

69-
protected void assertNodeServerDirReady() throws IOException {
70-
if (needsPrepareNodeServerLayout()) {
71-
// reinstall if missing
72-
prepareNodeServerLayout();
65+
Runtime(NpmFormatterStepStateBase parent) {
66+
this.nodeServerLayout = new NodeServerLayout(parent.locations.buildDir(), parent.npmConfig.getPackageJsonContent());
67+
this.nodeServeApp = new NodeServeApp(nodeServerLayout, parent.npmConfig, parent.locations);
7368
}
74-
if (needsPrepareNodeServer()) {
75-
// run npm install if node_modules is missing
76-
prepareNodeServer();
69+
70+
protected void prepareNodeServerLayout() throws IOException {
71+
nodeServeApp.prepareNodeAppLayout();
7772
}
78-
}
7973

80-
protected boolean needsPrepareNodeServer() {
81-
return nodeServeApp.needsNpmInstall();
82-
}
74+
protected void prepareNodeServer() throws IOException {
75+
nodeServeApp.npmInstall();
76+
}
8377

84-
protected boolean needsPrepareNodeServerLayout() {
85-
return nodeServeApp.needsPrepareNodeAppLayout();
86-
}
78+
protected void assertNodeServerDirReady() throws IOException {
79+
if (needsPrepareNodeServerLayout()) {
80+
// reinstall if missing
81+
prepareNodeServerLayout();
82+
}
83+
if (needsPrepareNodeServer()) {
84+
// run npm install if node_modules is missing
85+
prepareNodeServer();
86+
}
87+
}
88+
89+
protected boolean needsPrepareNodeServer() {
90+
return nodeServeApp.needsNpmInstall();
91+
}
8792

88-
protected ServerProcessInfo npmRunServer() throws ServerStartException, IOException {
89-
assertNodeServerDirReady();
90-
LongRunningProcess server = null;
91-
try {
92-
// The npm process will output the randomly selected port of the http server process to 'server.port' file
93-
// so in order to be safe, remove such a file if it exists before starting.
94-
final File serverPortFile = new File(this.nodeServerLayout.nodeModulesDir(), "server.port");
95-
NpmResourceHelper.deleteFileIfExists(serverPortFile);
96-
// start the http server in node
97-
server = nodeServeApp.startNpmServeProcess();
98-
99-
// await the readiness of the http server - wait for at most 60 seconds
93+
protected boolean needsPrepareNodeServerLayout() {
94+
return nodeServeApp.needsPrepareNodeAppLayout();
95+
}
96+
97+
protected ServerProcessInfo npmRunServer() throws ServerStartException, IOException {
98+
assertNodeServerDirReady();
99+
LongRunningProcess server = null;
100100
try {
101-
NpmResourceHelper.awaitReadableFile(serverPortFile, Duration.ofSeconds(60));
102-
} catch (TimeoutException timeoutException) {
103-
// forcibly end the server process
101+
// The npm process will output the randomly selected port of the http server process to 'server.port' file
102+
// so in order to be safe, remove such a file if it exists before starting.
103+
final File serverPortFile = new File(this.nodeServerLayout.nodeModulesDir(), "server.port");
104+
NpmResourceHelper.deleteFileIfExists(serverPortFile);
105+
// start the http server in node
106+
server = nodeServeApp.startNpmServeProcess();
107+
108+
// await the readiness of the http server - wait for at most 60 seconds
104109
try {
105-
if (server.isAlive()) {
106-
server.destroyForcibly();
107-
server.waitFor();
110+
NpmResourceHelper.awaitReadableFile(serverPortFile, Duration.ofSeconds(60));
111+
} catch (TimeoutException timeoutException) {
112+
// forcibly end the server process
113+
try {
114+
if (server.isAlive()) {
115+
server.destroyForcibly();
116+
server.waitFor();
117+
}
118+
} catch (Throwable t) {
119+
// ignore
108120
}
109-
} catch (Throwable t) {
110-
// ignore
121+
throw timeoutException;
111122
}
112-
throw timeoutException;
123+
// read the server.port file for resulting port and remember the port for later formatting calls
124+
String serverPort = NpmResourceHelper.readUtf8StringFromFile(serverPortFile).trim();
125+
return new ServerProcessInfo(server, serverPort, serverPortFile);
126+
} catch (IOException | TimeoutException e) {
127+
throw new ServerStartException("Starting server failed." + (server != null ? "\n\nProcess result:\n" + ThrowingEx.get(server::result) : ""), e);
113128
}
114-
// read the server.port file for resulting port and remember the port for later formatting calls
115-
String serverPort = NpmResourceHelper.readUtf8StringFromFile(serverPortFile).trim();
116-
return new ServerProcessInfo(server, serverPort, serverPortFile);
117-
} catch (IOException | TimeoutException e) {
118-
throw new ServerStartException("Starting server failed." + (server != null ? "\n\nProcess result:\n" + ThrowingEx.get(server::result) : ""), e);
119129
}
120130
}
121131

lib/src/main/java/com/diffplug/spotless/npm/PrettierFormatterStep.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private static class State extends NpmFormatterStepStateBase implements Serializ
8888
public FormatterFunc createFormatterFunc() {
8989
try {
9090
logger.info("creating formatter function (starting server)");
91-
ServerProcessInfo prettierRestServer = npmRunServer();
91+
ServerProcessInfo prettierRestServer = toRuntime().npmRunServer();
9292
PrettierRestService restService = new PrettierRestService(prettierRestServer.getBaseUrl());
9393
String prettierConfigOptions = restService.resolveConfig(this.prettierConfig.getPrettierConfigPath(), this.prettierConfig.getOptions());
9494
return Closeable.ofDangerous(() -> endServer(restService, prettierRestServer), new PrettierFilePathPassingFormatterFunc(prettierConfigOptions, restService));

0 commit comments

Comments
 (0)