Skip to content

Commit d79013e

Browse files
committed
chore(docs): updating docs
1 parent 449c064 commit d79013e

3 files changed

Lines changed: 76 additions & 32 deletions

File tree

docs/examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
### Features
1010

1111
- [Handling output](handling-output.md)
12+
- [Using the `.wrap()` method](using-wrap.md)

docs/examples/handling-output.md

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
```typescript
88
import type { ChalkInstance } from "chalk";
99
import type { Logger } from "ts-log";
10-
import { LogLevel, Output, Program } from "@libreworks/cli";
10+
import { Output, Program } from "@libreworks/cli";
1111

1212
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
1313

@@ -19,39 +19,37 @@ async function makeLogEntries(logger: Logger, colors: ChalkInstance) {
1919
}
2020
}
2121

22-
const output = Output.create({
23-
verbosity: LogLevel.TRACE,
24-
color: true,
25-
});
22+
const output = Output.create({});
2623

27-
const command = new Command("@libreworks/cli:example-handling-output")
24+
const program = new Program("@libreworks/cli:example-handling-output")
2825
.description("A test of @libreworks/cli")
29-
.version("0.0.0")
30-
.action(async function (options, cmd) {
31-
// Send data as-is to stdout.
32-
output.write("You called the handler!");
33-
34-
// Use the logger, which writes to stderr.
35-
output.log.info("Handler invoked", { options });
36-
output.log.info(`Testing ${output.colors.red("123")}`);
37-
38-
await Promise.all([
39-
// Send logs to stderr at regular intervals.
40-
makeLogEntries(output.logger, output.colors),
41-
42-
// Show a loading indicator for 5 seconds.
43-
output.spinDuring(sleep(5000), "Reticulating Splines"),
44-
]);
45-
46-
output.log.warn("This is a warning!");
47-
48-
try {
49-
throw new Error("A Big Problem");
50-
} catch (e) {
51-
// Stop the spinner and display the error.
52-
output.fail(e);
53-
}
54-
});
26+
.version("0.0.0");
27+
28+
program.action(async function (options, cmd) {
29+
// Send data as-is to stdout.
30+
output.write("You called the handler!");
31+
32+
// Use the logger, which writes to stderr.
33+
output.log.info("Handler invoked", { options });
34+
output.log.info(`Testing ${output.colors.red("123")}`);
35+
36+
await Promise.all([
37+
// Send logs to stderr at regular intervals.
38+
makeLogEntries(output.logger, output.colors),
39+
40+
// Show a loading indicator for 5 seconds.
41+
output.spinDuring(sleep(5000), "Reticulating Splines"),
42+
]);
43+
44+
output.log.warn("This is a warning!");
45+
46+
try {
47+
throw new Error("A Big Problem");
48+
} catch (e) {
49+
// Stop the spinner and display the error.
50+
output.fail(e);
51+
}
52+
});
5553

5654
await command.parseAsync();
5755
```

docs/examples/using-wrap.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# @libreworks/cli
2+
3+
## Examples
4+
5+
### Using the `.wrap()` Method
6+
7+
```typescript
8+
import type { ChalkInstance } from "chalk";
9+
import type { Logger } from "ts-log";
10+
import { Output, Program } from "@libreworks/cli";
11+
12+
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
13+
14+
async function makeLogEntries(logger: Logger, colors: ChalkInstance) {
15+
logger.info("Time to make the entries!");
16+
for (let i = 0; i < 10; i++) {
17+
logger.trace(`Testing ${colors.green(i)}`);
18+
await sleep(500);
19+
}
20+
}
21+
22+
const output = Output.create({});
23+
24+
const program = new Program(output, "@libreworks/cli:example-handling-output")
25+
.description("A test of @libreworks/cli")
26+
.version("0.0.0");
27+
28+
program.action(
29+
// The loading indicator will display during this async function.
30+
program.wrap(async function (options, cmd) {
31+
// Use the logger, which writes to stderr.
32+
output.log.info("Handler invoked", { options });
33+
output.log.info(`Testing ${output.colors.red("123")}`);
34+
35+
// Send logs to stderr at regular intervals.
36+
makeLogEntries(output.logger, output.colors);
37+
output.log.warn("This is a warning!");
38+
39+
// Send data as-is to stdout.
40+
return "You called the handler!";
41+
})
42+
);
43+
44+
await command.parseAsync();
45+
```

0 commit comments

Comments
 (0)