-
Notifications
You must be signed in to change notification settings - Fork 14
/
NodeJSCommandLine.kt
169 lines (139 loc) · 4.35 KB
/
NodeJSCommandLine.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package io.plugin.tsnode.lib.cmd
import com.intellij.execution.configurations.GeneralCommandLine
import com.intellij.execution.configurations.PtyCommandLine
import com.intellij.execution.process.OSProcessHandler
import com.intellij.execution.process.ProcessTerminatedListener
import com.intellij.javascript.debugger.CommandLineDebugConfigurator
import com.intellij.javascript.nodejs.NodeCommandLineUtil
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.registry.Registry
import com.intellij.openapi.util.text.StringUtil
import com.intellij.util.ThrowableConsumer
import io.plugin.tsnode.execution.TsRunConfiguration
import io.plugin.tsnode.execution.TsRunSettings
import java.nio.charset.Charset
object MyNodeCommandLineUtil
{
fun createCommandLine(runConfig: TsRunConfiguration?, project: Project?): GeneralCommandLine
{
var usePtyWithTerminalConsole: Boolean? = null
if (runConfig != null)
{
/**
* allow set env var nodejs.console.use.terminal=true/false for enable or disable
*/
val v = runConfig.runSettings.envData.envs.get("nodejs.console.use.terminal")
if (!v.isNullOrEmpty() && !v.isBlank())
{
usePtyWithTerminalConsole = v.toBoolean()
}
}
if (usePtyWithTerminalConsole == null)
{
/**
* use idea Registry nodejs.console.use.terminal
*/
usePtyWithTerminalConsole = Registry.`is`("nodejs.console.use.terminal")
}
val commandLine = NodeCommandLineUtil.createCommandLine(usePtyWithTerminalConsole)
if (NodeCommandLineUtil.isTerminalCommandLine(commandLine))
{
// do something
(commandLine as PtyCommandLine)
}
else
{
// do something
}
return commandLine
}
fun createCommandLine(): GeneralCommandLine
{
return createCommandLine(null, null)
}
fun createCommandLine(runConfig: TsRunConfiguration?): GeneralCommandLine
{
return createCommandLine(runConfig, null)
}
fun createCommandLine(project: Project?): GeneralCommandLine
{
return createCommandLine(null, project)
}
fun configureDebugConfigurator(commandLine: GeneralCommandLine, debugConfigurator: CommandLineDebugConfigurator?)
{
if (debugConfigurator !== null) {
NodeCommandLineUtil.configureCommandLine(commandLine, debugConfigurator, ThrowableConsumer { })
}
}
fun configureWorkDirectory(commandLine: GeneralCommandLine, runSettings: TsRunSettings, project: Project)
{
if (StringUtil.isEmptyOrSpaces(runSettings.workingDirectory))
{
commandLine.withWorkDirectory(project.basePath)
}
else
{
commandLine.withWorkDirectory(runSettings.workingDirectory)
}
}
fun configureEnvironment(commandLine: GeneralCommandLine, envs: Map<String, String>?)
{
if (envs !== null) {
commandLine.withEnvironment(envs)
}
val env = commandLine.environment;
/**
* same as node.js run add FORCE_COLOR=true
*/
env.putIfAbsent("FORCE_COLOR", "3")
NodeCommandLineUtil.configureUsefulEnvironment(commandLine)
/*
env.putIfAbsent("COLORTERM", "true")
env.putIfAbsent("FORCE_COLOR", "true")
env.putIfAbsent("npm_config_color", "always")
env.putIfAbsent("MOCHA_COLORS", "1")
*/
}
fun configureCharset(commandLine: GeneralCommandLine)
{
if (commandLine.charset.toString() == "x-windows-950" || commandLine.charset.toString() == "x-windows-936")
{
commandLine.withCharset(Charset.forName("UTF-8"))
}
}
fun createProcessHandler(commandLine: GeneralCommandLine, project: Project?, debugConfigurator: CommandLineDebugConfigurator?): OSProcessHandler
{
val processHandler = if (NodeCommandLineUtil.isTerminalCommandLine(commandLine))
{
val p = NodeCommandLineUtil.createKillableColoredProcessHandler(commandLine, true)
p.setShouldKillProcessSoftlyWithWinP(true)
p
}
else
{
NodeCommandLineUtil.createProcessHandler(commandLine, true, debugConfigurator)
}
configureProcess(processHandler, project)
return processHandler
}
fun createProcessHandler(commandLine: GeneralCommandLine): OSProcessHandler
{
return createProcessHandler(commandLine, null, null)
}
fun configureProcess(processHandler: OSProcessHandler, project: Project?)
{
processHandler.setShouldDestroyProcessRecursively(true);
if (project != null)
{
ProcessTerminatedListener.attach(processHandler, project)
}
else
{
ProcessTerminatedListener.attach(processHandler)
}
}
fun configureProcess(processHandler: OSProcessHandler)
{
return configureProcess(processHandler, null)
}
}