Skip to content

Commit

Permalink
fix(docs): remove optional from --allow-run flag when using --upgrade…
Browse files Browse the repository at this point in the history
… option

feat(http): add new commands to http file, including a command that logs to stderr and a command that doesn't output anything
fix(http): fix typo in command id
fix(http): remove unused code
feat(command.ts): add support for displaying command output in the console
fix(logger.ts): fix typo in comment
feat(logger.ts): add support for displaying command spinner in the console
fix(print.ts): change spinner symbol from dot to dash
fix(runner.ts): clear spinner before executing command and start it again after the command is executed
  • Loading branch information
jupegarnica committed May 24, 2023
1 parent 08ac491 commit 65e70b9
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 46 deletions.
46 changes: 46 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/started.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ deno run --allow-read --allow-env --allow-net --allow-run https://tepi.deno.dev/
* `--allow-read` Needed to read files from the file system.
* `--allow-net` Needed to make HTTP requests.
* `--allow-env` (optional) Needed to load and read environment variables. Required if you use the --env-file option.
* `--allow-run` (optional) Needed to run the upgrade command. Required if you use the --upgrade option.
* `--allow-run` (optional) Required if you use the --upgrade option.
31 changes: 21 additions & 10 deletions http/command.http
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
---
host: <%= Deno.env.get('HOST') || 'https://faker.deno.dev' %>

---

###
---
id: run command with request
command: echo "with request"
command: echo "with request" && sleep 2 && echo "2s"
---
GET /
GET /?delay=1000
x-quiet: true

###

---
id: run command with no request
command: echo "with NO request" && sleep 1 && echo "1s" && sleep 1 && echo "2s"
id: run command without request
command: echo "without request"
---
###
---
id: fail command
command: echo "fail command" && exit 133
---

###


---
id: run command with timeout
timeout: 1000
command: echo "with timeout && sleep 2"
id: log to stderr
command: sh toStderr.sh
ignore: true
---

###
###
---
id: run command without stdout
timeout: 2000
command: sleep 5
ignore: true
---
13 changes: 4 additions & 9 deletions http/debug.http
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@

POST https://faker.deno.dev/pong
quiet: true
content-type: application/json


{
"ping": "pong long long very long text pong long long very long text pong long long very long text pong long long very long text pong long long very long text pong long long very long text pong long long very long text pong long long very long text pong long long very long text pong long long very long text pong long long very long text pong long long very long text pong long long very long text pong long long very long text pong long long very long text pong long long very long text pong long long very long text pong long long very long text pong long long very long text pong long long very long text pong long long very long text pong long long very long text pong long long very long text pong long long very long text pong long long very long text pong long long very long text pong long long very long text pong long long very long text pong long long very long text pong long long very long text pong long long very long text pong long long very long text pong long long very long text pong long long very long text "
}
---
id: fail command
command: echo "fail command" && exit 133
---
102 changes: 79 additions & 23 deletions src/command.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,95 @@
import { Block } from "./types.ts";
import $ from "https://deno.land/x/dax/mod.ts";
import $ from "https://deno.land/x/dax@0.31.1/mod.ts";
import * as fmt from "https://deno.land/std@0.178.0/fmt/colors.ts";
import { DISPLAY_INDEX_MINIMAL, getDisplayIndex } from "./print.ts";
import { getCmdSpinner } from "./logger.ts";
import { deferred } from "https://deno.land/std@0.188.0/async/deferred.ts";

function mergeReadableStreams<T>(
...streams: ReadableStream<T>[]
): ReadableStream<T> {
const resolvePromises = streams.map(() => deferred<void>());

return new ReadableStream<T>({
start(controller) {
Promise.all(resolvePromises).then(() => {
controller.close();
});
for (const [key, stream] of Object.entries(streams)) {
(async () => {
try {
for await (const data of stream) {
controller.enqueue(data);
}
resolvePromises[+key].resolve();

} catch (error) {
controller.error(error);
}
})();
}
},
});
}



export async function executeCommand(block: Block) {



const command = block.meta.command?.trim();
if (!command) {
return;
}
const timeout = Number(block.meta.timeout) || 0;

const displayIndex = getDisplayIndex(block.meta);
const cmd = $.raw`${command}`;
cmd.timeout(timeout);

if (displayIndex > DISPLAY_INDEX_MINIMAL) {
console.info(
fmt.dim(`------- command output ------- `)
);
cmd.printCommand();
cmd.stderr("inheritPiped");
cmd.stdout("inheritPiped");
} else {
cmd.stderr("null");
cmd.stdout("null");
}
await cmd
let spinner;
let output = "";

// TODO: timeout
// const timeout = Number(block.meta.timeout) || 0;

try {
const displayIndex = getDisplayIndex(block.meta);
if (displayIndex > DISPLAY_INDEX_MINIMAL) {
spinner = getCmdSpinner(command).start();
const cmd = $.raw`${command}`;

const child = cmd.stderr("piped").stdout('piped').spawn();
// const stream = child.stdout();
// TODO: merge stdout and stderr , and print them in order, now with mergeReadableStreams if the command fails it break execution
const stream = mergeReadableStreams(child.stdout(), child.stderr());
for await (const chunk of stream) {
const text = new TextDecoder().decode(chunk);
if (!output) {
console.group();
console.info(
fmt.dim(`------- output: ${command} ------- `)
);
}
output += text;
console.info(text.replace(/\n$/, ''));
}
} else {

await $.raw`${command}`.quiet()
// .timeout(timeout);
}

if (output) {
console.info(
fmt.dim(`------- output end: ${command} ------- `)
);
console.groupEnd();

}
spinner?.success(command);
} catch (error) {

spinner?.fail(command);
const err = new Error(`command failed: ${command} \n ${error}`);
err.cause = error;
throw err

if (displayIndex > DISPLAY_INDEX_MINIMAL) {
console.info(
fmt.dim(`------- command output end ------- `)
);
}


Expand Down
41 changes: 39 additions & 2 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// TODO: fecth from deno.land/x
// TODO: fetch from deno.land/x
import { wait } from "https://raw.githubusercontent.com/denosaurs/wait/main/mod.ts"
import * as fmt from "https://deno.land/std@0.178.0/fmt/colors.ts";

export const REFRESH_INTERVAL = 110;

Expand All @@ -10,14 +11,50 @@ export function getSpinner(text: string) {
prefix: "",
text,
spinner: "dots4",
color: "yellow",
color: "blue",
interval: REFRESH_INTERVAL,
discardStdin: true,
});

}


export function getCmdSpinner(text: string) {

const spinner = wait({

text: fmt.blue("$") + " " + text,
spinner: "clock",
color: "green",
interval: REFRESH_INTERVAL,
discardStdin: true,
});
const self = {
start: () => {
spinner.start();
return self;
},
success: (text: string) => {
spinner.stopAndPersist({
symbol: ' ' + fmt.green("$"),

text,
})
},
fail: (text: string) => {
spinner.stopAndPersist({
symbol: ' ' + fmt.red("$"),
text,

})
}
}

return self

}


export function log(text: string) {

return getSpinner(text).start();
Expand Down
3 changes: 2 additions & 1 deletion src/print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ export function createBlockSpinner(

const text = `${fmt.white(block.description)} ${" "} ${fmt.gray(`${ms(_elapsedTime)}`)
}${differentFile}`;
const symbol = fmt.yellow("·");
const symbol = fmt.yellow("-");
clearInterval(id);

if (globalMeta._noAnimation) {
Expand Down Expand Up @@ -499,6 +499,7 @@ export function createBlockSpinner(
text,
});
},

update,
};
}
3 changes: 3 additions & 0 deletions src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,10 @@ async function runBlock(
}

if (block.meta.command) {
spinner.clear();
await executeCommand(block);
spinner.start();

}

if (!block.request) {
Expand Down

0 comments on commit 65e70b9

Please sign in to comment.