diff --git a/.gitignore b/.gitignore index a094fa9..cbb17a7 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,3 @@ dist node_modules test.json -test.ndjson diff --git a/README.md b/README.md index 6bb7a83..7830e2a 100644 --- a/README.md +++ b/README.md @@ -185,8 +185,6 @@ The JSON format lets you specify additional things apart from the command itself - killAllSequence: When you use “kill all” run-pty sends ctrl+c to all commands. However, not all commands exit when you do that. In such cases, you can use `killAllSequence` to specify what sequence of characters to send to the command to make it exit. -Instead of JSON, you can also use [NDJSON] – one JSON object per line (blank lines are OK, too). This is handy if you generate the file on the fly using some primitive scripting language. - ## Credits - [microsoft/node-pty] does all the heavy lifting of running the commands. @@ -211,5 +209,4 @@ There might still be occasional flicker. Hopefully the iTerm2 developers will im [graphic renditions]: https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters [iterm2]: https://www.iterm2.com/ [microsoft/node-pty]: https://github.com/microsoft/node-pty -[ndjson]: https://github.com/ndjson/ndjson-spec [tmux]: https://github.com/tmux/tmux diff --git a/run-pty.js b/run-pty.js index 949ae00..c836de0 100755 --- a/run-pty.js +++ b/run-pty.js @@ -244,7 +244,7 @@ Separate the commands with a character of choice: Note: All arguments are strings and passed as-is – no shell script execution. Use ${bold("sh -c '...'")} or similar if you need that. -Alternatively, specify the commands in a JSON (or NDJSON) file: +Alternatively, specify the commands in a JSON file: ${runPty} run-pty.json @@ -692,45 +692,12 @@ const parseArgs = (args) => { * @returns {Array} */ const parseInputFile = (string) => { - const first = string.trimStart().slice(0, 1); - switch (first) { - case "[": { - try { - return Decode.array(commandDescriptionDecoder)(JSON.parse(string)); - } catch (error) { - throw error instanceof Decode.DecoderError - ? new Error(error.format()) - : error; - } - } - - case "": // An empty file is empty NDJSON. - case "{": - return string.split("\n").flatMap((line, lineIndex) => { - const trimmed = line.trim(); - if (trimmed === "") { - return []; - } - - try { - return commandDescriptionDecoder(JSON.parse(trimmed)); - } catch (error) { - throw new Error( - `Line ${lineIndex + 1}: ${ - error instanceof Decode.DecoderError - ? error.format() - : error instanceof Error - ? error.message - : "Unknown parse error" - }` - ); - } - }); - - default: - throw new Error( - `Expected input to start with [ or { but got: ${first || "nothing"}` - ); + try { + return Decode.array(commandDescriptionDecoder)(JSON.parse(string)); + } catch (error) { + throw error instanceof Decode.DecoderError + ? new Error(error.format()) + : error; } }; diff --git a/test/fixtures/empty.ndjson b/test/fixtures/empty.json similarity index 100% rename from test/fixtures/empty.ndjson rename to test/fixtures/empty.json diff --git a/test/fixtures/invalid-ndjson-syntax.ndjson b/test/fixtures/invalid-ndjson-syntax.ndjson deleted file mode 100644 index 9a02bb5..0000000 --- a/test/fixtures/invalid-ndjson-syntax.ndjson +++ /dev/null @@ -1,2 +0,0 @@ -{ "command": ["npm", "run", "frontend"] } -{ "command": ["npm", "run", "backend"], } diff --git a/test/fixtures/kitchen-sink.ndjson b/test/fixtures/kitchen-sink.ndjson deleted file mode 100644 index 7f37ed9..0000000 --- a/test/fixtures/kitchen-sink.ndjson +++ /dev/null @@ -1,6 +0,0 @@ -{ "command": ["node"], "killAllSequence": "\u0003\u0003" } - - { "command": ["npm", "start"], "title": "Backend", "defaultStatus": null } - - -{ "command": ["npm", "run", "parcel"], "title": "Parcel", "cwd": "frontend", "status": { "🚨": ["🚨", "E"], "✨": null }, "defaultStatus": ["⏳", "S"] } diff --git a/test/run-pty.test.js b/test/run-pty.test.js index abb3e3b..5ed1833 100644 --- a/test/run-pty.test.js +++ b/test/run-pty.test.js @@ -62,7 +62,7 @@ describe("help", () => { Note: All arguments are strings and passed as-is – no shell script execution. Use ⧙sh -c '...'⧘ or similar if you need that. - Alternatively, specify the commands in a JSON (or NDJSON) file: + Alternatively, specify the commands in a JSON file: ⧙run-pty⧘ run-pty.json @@ -597,25 +597,26 @@ describe("parse json", () => { throw new Error("Expected Error!"); } - test("invalid json syntax", () => { - expect(testJsonError("invalid-json-syntax.json")).toMatchInlineSnapshot(` + test("empty file", () => { + expect(testJsonError("empty.json")).toMatchInlineSnapshot(` Failed to read command descriptions file as JSON: - Unexpected token ] in JSON at position 91 + Unexpected end of JSON input `); }); - test("invalid ndjson syntax", () => { - expect(testJsonError("invalid-ndjson-syntax.ndjson")) - .toMatchInlineSnapshot(` + test("invalid json syntax", () => { + expect(testJsonError("invalid-json-syntax.json")).toMatchInlineSnapshot(` Failed to read command descriptions file as JSON: - Line 2: Unexpected token } in JSON at position 40 + Unexpected token ] in JSON at position 91 `); }); test("bad json type", () => { expect(testJsonError("bad-json-type.json")).toMatchInlineSnapshot(` Failed to read command descriptions file as JSON: - Expected input to start with [ or { but got: n + At root: + Expected an array + Got: null `); }); @@ -623,10 +624,6 @@ describe("parse json", () => { expect(testJson("empty-array.json")).toStrictEqual({ tag: "NoCommands" }); }); - test("empty NDJSON", () => { - expect(testJson("empty.ndjson")).toStrictEqual({ tag: "NoCommands" }); - }); - test("empty command", () => { expect(testJsonError("empty-command.json")).toMatchInlineSnapshot(` Failed to read command descriptions file as JSON: @@ -672,9 +669,7 @@ describe("parse json", () => { }); test("kitchen sink", () => { - const parsed = testJson("kitchen-sink.json"); - - expect(parsed).toStrictEqual({ + expect(testJson("kitchen-sink.json")).toStrictEqual({ tag: "Parsed", commands: [ { @@ -706,7 +701,5 @@ describe("parse json", () => { }, ], }); - - expect(testJson("kitchen-sink.ndjson")).toStrictEqual(parsed); }); });