@@ -47,75 +47,85 @@ abstract class NpmFormatterStepStateBase implements Serializable {
47
47
private final NpmConfig npmConfig ;
48
48
49
49
public final NpmFormatterStepLocations locations ;
50
- protected final transient NodeServerLayout nodeServerLayout ;
51
- private final transient NodeServeApp nodeServeApp ;
52
50
53
51
protected NpmFormatterStepStateBase (String stepName , NpmConfig npmConfig , NpmFormatterStepLocations locations ) throws IOException {
54
52
this .stepName = requireNonNull (stepName );
55
53
this .npmConfig = requireNonNull (npmConfig );
56
54
this .locations = locations ;
57
- this .nodeServerLayout = new NodeServerLayout (locations .buildDir (), npmConfig .getPackageJsonContent ());
58
- this .nodeServeApp = new NodeServeApp (nodeServerLayout , npmConfig , locations );
59
55
}
60
56
61
- protected void prepareNodeServerLayout () throws IOException {
62
- nodeServeApp . prepareNodeAppLayout ( );
57
+ public Runtime toRuntime () {
58
+ return new Runtime ( this );
63
59
}
64
60
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 ;
68
64
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 );
73
68
}
74
- if ( needsPrepareNodeServer ()) {
75
- // run npm install if node_modules is missing
76
- prepareNodeServer ();
69
+
70
+ protected void prepareNodeServerLayout () throws IOException {
71
+ nodeServeApp . prepareNodeAppLayout ();
77
72
}
78
- }
79
73
80
- protected boolean needsPrepareNodeServer () {
81
- return nodeServeApp .needsNpmInstall ();
82
- }
74
+ protected void prepareNodeServer () throws IOException {
75
+ nodeServeApp .npmInstall ();
76
+ }
83
77
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
+ }
87
92
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 ;
100
100
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
104
109
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
108
120
}
109
- } catch (Throwable t ) {
110
- // ignore
121
+ throw timeoutException ;
111
122
}
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 \n Process result:\n " + ThrowingEx .get (server ::result ) : "" ), e );
113
128
}
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 \n Process result:\n " + ThrowingEx .get (server ::result ) : "" ), e );
119
129
}
120
130
}
121
131
0 commit comments