Fix server.go

server.go processed Typesript output incorrectly when multiple
lines are concatenated together.

Change-Id: Id2af52df8edf702214aeeab1bae0242d3077c616
This commit is contained in:
Dmitrii Filippov
2020-10-05 16:30:32 +02:00
parent fa5a425991
commit 5a2334ed69

View File

@@ -527,6 +527,7 @@ var (
) )
type typescriptLogWriter struct { type typescriptLogWriter struct {
unfinishedLine string
logger *log.Logger logger *log.Logger
// when WaitGroup counter is 0 the compilation is complete // when WaitGroup counter is 0 the compilation is complete
compilationDoneWaiter *sync.WaitGroup compilationDoneWaiter *sync.WaitGroup
@@ -534,13 +535,30 @@ type typescriptLogWriter struct {
func newTypescriptLogWriter(compilationCompleteWaiter *sync.WaitGroup) *typescriptLogWriter { func newTypescriptLogWriter(compilationCompleteWaiter *sync.WaitGroup) *typescriptLogWriter {
return &typescriptLogWriter{ return &typescriptLogWriter{
unfinishedLine: "",
logger: log.New(log.Writer(), "TSC - ", log.Flags()), logger: log.New(log.Writer(), "TSC - ", log.Flags()),
compilationDoneWaiter: compilationCompleteWaiter, compilationDoneWaiter: compilationCompleteWaiter,
} }
} }
func (lw typescriptLogWriter) Write(p []byte) (n int, err error) { func (lw typescriptLogWriter) Write(p []byte) (n int, err error) {
text := strings.TrimSpace(string(p)) // The input p can contain several lines and/or the partial line
// Code splits the input by EOL marker (\n) and stores the unfinished line
// for the next call to Write.
partialText := lw.unfinishedLine + string(p)
lines := strings.Split(partialText, "\n")
fullLines := lines
if strings.HasSuffix(partialText, "\n") {
lw.unfinishedLine = ""
} else {
fullLines = lines[:len(lines)-1]
lw.unfinishedLine = lines[len(lines)-1]
}
for _, fullLine := range fullLines {
text := strings.TrimSpace(fullLine)
if text == "" {
continue
}
if strings.HasSuffix(text, tsFileChangeDetectedMsg) || if strings.HasSuffix(text, tsFileChangeDetectedMsg) ||
strings.HasSuffix(text, tsStartingCompilation) { strings.HasSuffix(text, tsStartingCompilation) {
lw.compilationDoneWaiter.Add(1) lw.compilationDoneWaiter.Add(1)
@@ -556,9 +574,9 @@ func (lw typescriptLogWriter) Write(p []byte) (n int, err error) {
time.Sleep(waitForNextChangeInterval) time.Sleep(waitForNextChangeInterval)
lw.compilationDoneWaiter.Add(-1) lw.compilationDoneWaiter.Add(-1)
}() }()
} }
lw.logger.Print(text) lw.logger.Print(text)
}
return len(p), nil return len(p), nil
} }