31
31
import org .slf4j .Logger ;
32
32
import org .slf4j .LoggerFactory ;
33
33
34
- import com .diffplug .spotless .FileSignature ;
35
34
import com .diffplug .spotless .FormatterFunc ;
35
+ import com .diffplug .spotless .ProcessRunner .LongRunningProcess ;
36
+ import com .diffplug .spotless .ThrowingEx ;
36
37
37
38
import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
38
39
@@ -42,11 +43,8 @@ abstract class NpmFormatterStepStateBase implements Serializable {
42
43
43
44
private static final long serialVersionUID = 1460749955865959948L ;
44
45
45
- @ SuppressWarnings ("unused" )
46
- private final FileSignature packageJsonSignature ;
47
-
48
46
@ SuppressFBWarnings ("SE_TRANSIENT_FIELD_NOT_RESTORED" )
49
- public final transient File nodeModulesDir ;
47
+ protected final transient NodeServerLayout nodeServerLayout ;
50
48
51
49
public final NpmFormatterStepLocations locations ;
52
50
@@ -58,45 +56,61 @@ protected NpmFormatterStepStateBase(String stepName, NpmConfig npmConfig, NpmFor
58
56
this .stepName = requireNonNull (stepName );
59
57
this .npmConfig = requireNonNull (npmConfig );
60
58
this .locations = locations ;
61
- NodeServerLayout layout = prepareNodeServer (locations .buildDir ());
62
- this .nodeModulesDir = layout .nodeModulesDir ();
63
- this .packageJsonSignature = FileSignature .signAsList (layout .packageJsonFile ());
59
+ this .nodeServerLayout = new NodeServerLayout (locations .buildDir (), stepName );
64
60
}
65
61
66
- private NodeServerLayout prepareNodeServer (File buildDir ) throws IOException {
67
- NodeServerLayout layout = new NodeServerLayout (buildDir , stepName );
68
- NpmResourceHelper .assertDirectoryExists (layout .nodeModulesDir ());
69
- NpmResourceHelper .writeUtf8StringToFile (layout .packageJsonFile (),
62
+ protected void prepareNodeServerLayout () throws IOException {
63
+ NpmResourceHelper .assertDirectoryExists (nodeServerLayout .nodeModulesDir ());
64
+ NpmResourceHelper .writeUtf8StringToFile (nodeServerLayout .packageJsonFile (),
70
65
this .npmConfig .getPackageJsonContent ());
71
66
NpmResourceHelper
72
- .writeUtf8StringToFile (layout .serveJsFile (), this .npmConfig .getServeScriptContent ());
67
+ .writeUtf8StringToFile (nodeServerLayout .serveJsFile (), this .npmConfig .getServeScriptContent ());
73
68
if (this .npmConfig .getNpmrcContent () != null ) {
74
- NpmResourceHelper .writeUtf8StringToFile (layout .npmrcFile (), this .npmConfig .getNpmrcContent ());
69
+ NpmResourceHelper .writeUtf8StringToFile (nodeServerLayout .npmrcFile (), this .npmConfig .getNpmrcContent ());
75
70
} else {
76
- NpmResourceHelper .deleteFileIfExists (layout .npmrcFile ());
71
+ NpmResourceHelper .deleteFileIfExists (nodeServerLayout .npmrcFile ());
77
72
}
73
+ }
74
+
75
+ protected void prepareNodeServer () throws IOException {
78
76
FormattedPrinter .SYSOUT .print ("running npm install" );
79
- runNpmInstall (layout .nodeModulesDir ());
77
+ runNpmInstall (nodeServerLayout .nodeModulesDir ());
80
78
FormattedPrinter .SYSOUT .print ("npm install finished" );
81
- return layout ;
82
79
}
83
80
84
81
private void runNpmInstall (File npmProjectDir ) throws IOException {
85
82
new NpmProcess (npmProjectDir , this .locations .npmExecutable (), this .locations .nodeExecutable ()).install ();
86
83
}
87
84
88
- protected ServerProcessInfo npmRunServer () throws ServerStartException , IOException {
89
- if (!this .nodeModulesDir .exists ()) {
90
- prepareNodeServer (NodeServerLayout .getBuildDirFromNodeModulesDir (this .nodeModulesDir ));
85
+ protected void assertNodeServerDirReady () throws IOException {
86
+ if (needsPrepareNodeServerLayout ()) {
87
+ // reinstall if missing
88
+ prepareNodeServerLayout ();
89
+ }
90
+ if (needsPrepareNodeServer ()) {
91
+ // run npm install if node_modules is missing
92
+ prepareNodeServer ();
91
93
}
94
+ }
92
95
96
+ protected boolean needsPrepareNodeServer () {
97
+ return !this .nodeServerLayout .isNodeModulesPrepared ();
98
+ }
99
+
100
+ protected boolean needsPrepareNodeServerLayout () {
101
+ return !this .nodeServerLayout .isLayoutPrepared ();
102
+ }
103
+
104
+ protected ServerProcessInfo npmRunServer () throws ServerStartException , IOException {
105
+ assertNodeServerDirReady ();
106
+ LongRunningProcess server = null ;
93
107
try {
94
108
// The npm process will output the randomly selected port of the http server process to 'server.port' file
95
109
// so in order to be safe, remove such a file if it exists before starting.
96
- final File serverPortFile = new File (this .nodeModulesDir , "server.port" );
110
+ final File serverPortFile = new File (this .nodeServerLayout . nodeModulesDir () , "server.port" );
97
111
NpmResourceHelper .deleteFileIfExists (serverPortFile );
98
112
// start the http server in node
99
- Process server = new NpmProcess (this .nodeModulesDir , this .locations .npmExecutable (), this .locations .nodeExecutable ()).start ();
113
+ server = new NpmProcess (this .nodeServerLayout . nodeModulesDir () , this .locations .npmExecutable (), this .locations .nodeExecutable ()).start ();
100
114
101
115
// await the readiness of the http server - wait for at most 60 seconds
102
116
try {
@@ -117,7 +131,7 @@ protected ServerProcessInfo npmRunServer() throws ServerStartException, IOExcept
117
131
String serverPort = NpmResourceHelper .readUtf8StringFromFile (serverPortFile ).trim ();
118
132
return new ServerProcessInfo (server , serverPort , serverPortFile );
119
133
} catch (IOException | TimeoutException e ) {
120
- throw new ServerStartException (e );
134
+ throw new ServerStartException ("Starting server failed." + ( server != null ? " \n \n Process result: \n " + ThrowingEx . get ( server :: result ) : "" ), e );
121
135
}
122
136
}
123
137
@@ -186,7 +200,7 @@ public void close() throws Exception {
186
200
protected static class ServerStartException extends RuntimeException {
187
201
private static final long serialVersionUID = -8803977379866483002L ;
188
202
189
- public ServerStartException (Throwable cause ) {
203
+ public ServerStartException (String message , Throwable cause ) {
190
204
super (cause );
191
205
}
192
206
}
0 commit comments