Skip to content

Commit

Permalink
Remove NDJSON support
Browse files Browse the repository at this point in the history
  • Loading branch information
lydell committed Jul 5, 2022
1 parent bfa6eed commit a82abc0
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 70 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
dist
node_modules
test.json
test.ndjson
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <kbd>ctrl+c</kbd> 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.
Expand All @@ -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
47 changes: 7 additions & 40 deletions run-pty.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -692,45 +692,12 @@ const parseArgs = (args) => {
* @returns {Array<CommandDescription>}
*/
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;
}
};

Expand Down
File renamed without changes.
2 changes: 0 additions & 2 deletions test/fixtures/invalid-ndjson-syntax.ndjson

This file was deleted.

6 changes: 0 additions & 6 deletions test/fixtures/kitchen-sink.ndjson

This file was deleted.

29 changes: 11 additions & 18 deletions test/run-pty.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -597,36 +597,33 @@ 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
`);
});

test("empty list of commands", () => {
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:
Expand Down Expand Up @@ -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: [
{
Expand Down Expand Up @@ -706,7 +701,5 @@ describe("parse json", () => {
},
],
});

expect(testJson("kitchen-sink.ndjson")).toStrictEqual(parsed);
});
});

0 comments on commit a82abc0

Please sign in to comment.