1515import de .php_perfect .intellij .ddev .dockerCompose .DockerComposeCredentialProvider ;
1616import org .jetbrains .annotations .NotNull ;
1717
18+ import java .util .Collection ;
1819import java .util .List ;
1920
2021public final class NodeInterpreterProviderImpl implements NodeInterpreterProvider {
@@ -26,20 +27,66 @@ public NodeInterpreterProviderImpl(final @NotNull Project project) {
2627 this .project = project ;
2728 }
2829
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+ */
2937 public void configureNodeInterpreter (final @ NotNull NodeInterpreterConfig nodeInterpreterConfig ) {
3038 final NodeRemoteInterpreters nodeRemoteInterpreters = NodeRemoteInterpreters .getInstance ();
39+ final Collection <NodeJSRemoteSdkAdditionalData > interpreters = nodeRemoteInterpreters .getInterpreters ();
40+ final String normalizedTargetPath = normalizePath (nodeInterpreterConfig .composeFilePath ());
3141
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" );
3345 return ;
3446 }
3547
48+ // Create and configure a new interpreter
3649 LOG .debug ("Creating nodejs interpreter" );
3750
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
4063 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 ) {
4179
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 ;
4390 }
4491
4592 private @ NotNull NodeJSRemoteSdkAdditionalData buildNodeJSRemoteSdkAdditionalData (DockerComposeCredentialsHolder credentials , @ NotNull String binaryPath ) {
@@ -60,4 +107,19 @@ private PathMappingSettings loadPathMappings(NodeJSRemoteSdkAdditionalData sdkDa
60107 return null ;
61108 }
62109 }
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+ }
63125}
0 commit comments