zaptst / zap

A streamable structured interface for real time reporting of developer tools.
MIT License
120 stars 1 forks source link

Merging streams #8

Open jamiebuilds opened 6 years ago

jamiebuilds commented 6 years ago

Because of the "id" format, it is easy to merge together multiple ZAP streams into a single stream simply by prepending all the "id"'s in each stream with another number for the stream and some sort of label.

Stream 1:

{ ... "id": "0" ... }
{ ... "id": "0.0" ... }
{ ... "id": "0.1" ... }
{ ... "id": "0" ... }
{ ... "id": "1" ... }
{ ... "id": "1.0" ... }
{ ... "id": "1.1" ... }
{ ... "id": "1.2" ... }
{ ... "id": "1" ... }

Stream 2:

{ ... "id": "0" ... }
{ ... "id": "0.0" ... }
{ ... "id": "0" ... }
{ ... "id": "1" ... }
{ ... "id": "1.0" ... }
{ ... "id": "1.1" ... }
{ ... "id": "1" ... }

Merged Streams:

{ ... "id": "0" ... } // 1
{ ... "id": "1" ... } // 2
{ ... "id": "0.0" ... } // 1
{ ... "id": "1.0" ... } // 2
{ ... "id": "0.0.0" ... } // 1
{ ... "id": "1.0.0" ... } // 2
{ ... "id": "1.0" ... } // 2
{ ... "id": "0.0.1" ... } // 1
{ ... "id": "0.0" ... } // 1
{ ... "id": "0.1" ... } // 1
{ ... "id": "1.1" ... } // 2
{ ... "id": "0.1.0" ... } // 1
{ ... "id": "0.1.1" ... } // 1
{ ... "id": "1.1.0" ... } // 2
{ ... "id": "1.1.1" ... } // 2
{ ... "id": "0.1.2" ... } // 1
{ ... "id": "0.1" ... } // 1
{ ... "id": "0" ... } // 1
{ ... "id": "1.1" ... } // 2
{ ... "id": "1" ... } // 2

This allows ZAP reporters to report the output from multiple tools without any additional changes.

There are no required changes to ZAP to support this behavior, but it seems worth documenting and maybe writing a library to do automatically.

sindresorhus commented 6 years ago

This is very cool. We could then potentially run linting and tests in parallel.

jamiebuilds commented 6 years ago

I think I'm gonna do it like this:

(./run-tests.sh | zap-wrap 0 "Tests" & ./run-lints.sh | zap-wrap 1 "Lints")

Where ... | zap-wrap <id> <message> accepts stdin and outputs:

// prepends a group "started" event
{"kind":"group","event":"started","id":"<id>","timestamp":"...","content":[{"message":"<message>"}]}
// modifies previous contents as stream:
{ ... "id":"<id>.<previous id>" ... }
// appends a group completion event that "passed" or "failed" based on the process exit code
{"kind":"group","event":"<passed|failed>","id":"<id>","timestamp":"...","content":[{"message":"<message>"}]}