Skip to content

Add console log and outputs #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 23, 2022
Merged

Add console log and outputs #39

merged 9 commits into from
Mar 23, 2022

Conversation

mdbenjam
Copy link
Owner

This PR:

  1. Correctly imports the new name of the testing library
  2. Adds a new output pane that shows the most recent command's error or console output.
  3. Displays console outputs in both the command history view and the output pane.
ConsoleLog.mp4

Passes the console log output from the test to the frontend to be displayed for the user in a new
output pane.
When a single command results in multiple lines of output, we need to show all these lines to the
user.
Makes console log outputs underlined in yellow and errors underlined in red.
@@ -25,7 +25,9 @@ function refresh() {
let IDENTIFIER_MAP = {
highlight,
refresh,
console,
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add console as a default available command

throw Error(
`Error printed to console.error. This error occurred asynchronously, and may have happened before this line was executed.\n\n${consoleLog.arguments[0]}`
);
await delay(1);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a delay after every command to give the React code time to update and to send logs/errors.

Comment on lines 154 to 174
for (const consoleLog of consoleLogQueue) {
if (!consoleLog.seen) {
consoleLog.seen = true;

for (const arg of consoleLog.arguments) {
if (consoleLog.method === "error") {
consoleOutputs.push({
type: "error",
message: `Error printed to console.error. This error occurred asynchronously, and may have happened before this line was executed.\n\n${arg}`,
lineNumber,
});
} else {
consoleOutputs.push({
message: String(arg),
type: consoleLog.method,
lineNumber,
});
}
}
}
}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get every type of output, note the type and send it to the user.

@@ -61,6 +61,9 @@ export const debuggerSetup = async (fn) => {
consoleLogQueue.push({ method: "log", arguments: arguments });
return _log.apply(console, arguments);
};
console.log_without_reporting = function () {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Retain an alias for the original console.log that we can use to output lines only to the console, and not to the user in the UI.

@@ -175,11 +178,7 @@ fastify.post("/command", async (request, reply) => {
html: addStyleLinks(
replaceFilePaths(document.documentElement.innerHTML, manifest)
),
error: output.error && {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer report errors separately, just send consoleOutputs with their type.

}

.editor-div {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This setup allows each code mirror instance on the page to fill an equal amount of space vertically! Before this we were doing some less than idea layout management with things like calc(50vh - 100px)

@@ -21,12 +31,13 @@ function CommandInput({ setInnerHTML, availableCommands }) {
...existingHistory,
{
content: "",
errors: [],
consoleOutputs: [],
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just like on the backend, get rid of tracking errors independently and only track the console outputs. Within each output we have a type which can be error or log (or potentially info etc).

wasReset: false,
},
];
});
const [editorValue, setEditorValue] = useState("");
const [output, setOutput] = useState("");
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a new output state which is the most recent output from a command.

/>
</div>
<h1 className="code-window-titles">Output</h1>
<div className="editor-div">
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Show the most recent output

@@ -40,7 +40,7 @@ import {
const completePropertyAfter = ["PropertyName", ".", "?."];

const fixedHeightEditor = EditorView.theme({
"&": { height: "calc(50vh - 100px)" },
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we're properly using flexbox we can change this to 100%!


const errorEffect = StateEffect.define({});
const errorState = StateField.define({
const consoleEffect = StateEffect.define({});
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename a lot of things from error to console to make them more generic.

}),
sort: true,
});
if (consoleEffects.length > 0) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new check plus the line below (148 filter: () => false) means that we:

  1. Only update this when the effect update contains a consoleEffect
  2. Remove all of the existing consoleEffect, and adds them all back

Prior to this we had to make sure to only add the new consoleEffects or else we would double add them. This filter, then add approach is better for our case since we maintain the total list of consoleEffects outside of the editor and pass it in every time.

error: error.message,
});
})
.filter((error) => {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mdbenjam mdbenjam merged commit c59b1ad into main Mar 23, 2022
@mdbenjam mdbenjam mentioned this pull request Mar 23, 2022
@mdbenjam
Copy link
Owner Author

🎉 This PR is included in version 1.0.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant