15
15
import de .php_perfect .intellij .ddev .dockerCompose .DockerComposeCredentialProvider ;
16
16
import org .jetbrains .annotations .NotNull ;
17
17
18
+ import java .util .Collection ;
18
19
import java .util .List ;
19
20
20
21
public final class NodeInterpreterProviderImpl implements NodeInterpreterProvider {
@@ -26,20 +27,66 @@ public NodeInterpreterProviderImpl(final @NotNull Project project) {
26
27
this .project = project ;
27
28
}
28
29
30
+ /**
31
+ * Configures a Node.js interpreter for the DDEV environment.
32
+ * If an interpreter with matching compose file already exists, it will be reused.
33
+ * Otherwise, a new interpreter will be created and configured.
34
+ *
35
+ * @param nodeInterpreterConfig Configuration for the Node.js interpreter
36
+ */
29
37
public void configureNodeInterpreter (final @ NotNull NodeInterpreterConfig nodeInterpreterConfig ) {
30
38
final NodeRemoteInterpreters nodeRemoteInterpreters = NodeRemoteInterpreters .getInstance ();
39
+ final Collection <NodeJSRemoteSdkAdditionalData > interpreters = nodeRemoteInterpreters .getInterpreters ();
40
+ final String normalizedTargetPath = normalizePath (nodeInterpreterConfig .composeFilePath ());
31
41
32
- if (!nodeRemoteInterpreters .getInterpreters ().isEmpty ()) {
42
+ // Check if we already have a matching remote interpreter set up
43
+ if (!interpreters .isEmpty () && isInterpreterAlreadyConfigured (interpreters , normalizedTargetPath )) {
44
+ LOG .debug ("Found existing nodejs interpreter" );
33
45
return ;
34
46
}
35
47
48
+ // Create and configure a new interpreter
36
49
LOG .debug ("Creating nodejs interpreter" );
37
50
38
- final DockerComposeCredentialsHolder credentials = DockerComposeCredentialProvider .getInstance ().getDdevDockerComposeCredentials (new DockerComposeConfig (List .of (nodeInterpreterConfig .composeFilePath ()), nodeInterpreterConfig .name ()));
39
- final NodeJSRemoteSdkAdditionalData sdkData = this .buildNodeJSRemoteSdkAdditionalData (credentials , nodeInterpreterConfig .binaryPath ());
51
+ // Create credentials for Docker Compose
52
+ final DockerComposeCredentialsHolder credentials = DockerComposeCredentialProvider .getInstance ()
53
+ .getDdevDockerComposeCredentials (
54
+ new DockerComposeConfig (List .of (nodeInterpreterConfig .composeFilePath ()), nodeInterpreterConfig .name ())
55
+ );
56
+
57
+ // Build and configure the SDK data
58
+ final NodeJSRemoteSdkAdditionalData sdkData = buildNodeJSRemoteSdkAdditionalData (
59
+ credentials , nodeInterpreterConfig .binaryPath ()
60
+ );
61
+
62
+ // Register the new interpreter
40
63
nodeRemoteInterpreters .add (sdkData );
64
+ NodeJsInterpreterManager .getInstance (this .project )
65
+ .setInterpreterRef (NodeJsInterpreterRef .create (sdkData .getSdkId ()));
66
+ }
67
+
68
+ /**
69
+ * @param interpreters Collection of existing interpreters
70
+ * @param normalizedTargetPath Normalized path to match against
71
+ * @return true if a matching interpreter exists, false otherwise
72
+ */
73
+ private boolean isInterpreterAlreadyConfigured (Collection <NodeJSRemoteSdkAdditionalData > interpreters , String normalizedTargetPath ) {
74
+ for (NodeJSRemoteSdkAdditionalData interpreter : interpreters ) {
75
+ Object credentialsObj = interpreter .connectionCredentials ().getCredentials ();
76
+
77
+ if (credentialsObj instanceof DockerComposeCredentialsHolder credentials &&
78
+ credentials .getComposeFilePaths () != null ) {
41
79
42
- NodeJsInterpreterManager .getInstance (this .project ).setInterpreterRef (NodeJsInterpreterRef .create (sdkData .getSdkId ()));
80
+ for (String composeFilePath : credentials .getComposeFilePaths ()) {
81
+ String normalizedExistingPath = normalizePath (composeFilePath );
82
+ if (normalizedExistingPath .contains (normalizedTargetPath )) {
83
+ return true ;
84
+ }
85
+ }
86
+ }
87
+ }
88
+
89
+ return false ;
43
90
}
44
91
45
92
private @ NotNull NodeJSRemoteSdkAdditionalData buildNodeJSRemoteSdkAdditionalData (DockerComposeCredentialsHolder credentials , @ NotNull String binaryPath ) {
@@ -60,4 +107,19 @@ private PathMappingSettings loadPathMappings(NodeJSRemoteSdkAdditionalData sdkDa
60
107
return null ;
61
108
}
62
109
}
110
+
111
+ /**
112
+ * Normalizes a file path by replacing backslashes with forward slashes
113
+ * and ensuring consistent path separators for comparison.
114
+ *
115
+ * @param path The path to normalize
116
+ * @return The normalized path
117
+ */
118
+ private String normalizePath (String path ) {
119
+ if (path == null ) {
120
+ return "" ;
121
+ }
122
+ // Replace backslashes with forward slashes for consistent comparison
123
+ return path .replace ('\\' , '/' );
124
+ }
63
125
}
0 commit comments