Skip to content

Commit

Permalink
feat: add key support on output
Browse files Browse the repository at this point in the history
  • Loading branch information
adbayb committed Mar 28, 2022
1 parent 64936eb commit ab597f0
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 40 deletions.
90 changes: 64 additions & 26 deletions example/withOutput.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,70 @@
import { helpers, termost } from "../src";

const program = termost("Example to showcase the `message` API");
type ProgramContext = {
output: string;
};

program.output({
handler() {
const content =
"A content formatted thanks to the `print` helper presets.";
const program = termost<ProgramContext>(
"Example to showcase the `message` API"
);

helpers.print(content);
helpers.print(content, { type: "warning" });
helpers.print(content, { type: "error" });
helpers.print(content, { type: "success" });
helpers.print(content, {
type: "information",
label: "馃憢 You can also customize the label",
});
helpers.print(["I support also", "multilines", "with array input"], {
type: "information",
label: "馃憢 You can also customize the label",
});
console.log(
helpers.format(
"\nYou can also have a total control on the formatting through the `format` helper.",
program
.output({
handler() {
const content =
"A content formatted thanks to the `print` helper presets.";

helpers.print(content);
helpers.print(content, { type: "warning" });
helpers.print(content, { type: "error" });
helpers.print(content, { type: "success" });
helpers.print(content, {
type: "information",
label: "馃憢 You can also customize the label",
});
helpers.print(
["I support also", "multilines", "with array input"],
{
color: "white",
modifier: ["italic", "strikethrough", "bold"],
type: "information",
label: "馃憢 You can also customize the label",
}
)
);
},
});
);
console.log(
helpers.format(
"\nYou can also have a total control on the formatting through the `format` helper.",
{
color: "white",
modifier: ["italic", "strikethrough", "bold"],
}
)
);
},
})
.output({
key: "output",
handler() {
return "Hello from previous output";
},
})
.output({
handler(context) {
helpers.print(
`A shareable value can also be displayed: ${context.output}`
);
},
})
.output({
async handler() {
helpers.print(
"I can also run asynchronous operations... Operation is starting..."
);

await wait(2000);

helpers.print("Operation is finished");
},
});

const wait = (delay: number) => {
return new Promise((resolve) => setTimeout(resolve, delay));
};
35 changes: 24 additions & 11 deletions src/api/output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,39 @@ import {
ArgumentValues,
Context,
CreateInstruction,
InstructionKey,
InstructionParameters,
ObjectLikeConstraint,
} from "../../types";

export const createOutput: CreateInstruction<
OutputParameters<ObjectLikeConstraint>
OutputParameters<ObjectLikeConstraint, keyof ObjectLikeConstraint>
> = (parameters) => {
const { handler } = parameters;
const { handler, key } = parameters;

return async function execute(context, argv) {
handler(context, argv);
const value = handler(context, argv);

return null;
return { key, value };
};
};

export type OutputParameters<Values extends ObjectLikeConstraint> =
InstructionParameters<
Values,
{
handler: (context: Context<Values>, argv: ArgumentValues) => void;
}
>;
export type OutputParameters<
Values extends ObjectLikeConstraint,
Key
> = InstructionParameters<
Values,
Key extends keyof Values
? Partial<InstructionKey<Key>> & {
handler: (
context: Context<Values>,
argv: ArgumentValues
) => Promise<Values[Key]>;
}
: {
handler: (
context: Context<Values>,
argv: ArgumentValues
) => void;
}
>;
2 changes: 1 addition & 1 deletion src/termost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export type Termost<Values extends ObjectLikeConstraint = EmptyObject> = {
params: InputParameters<Values, Key>
): Termost<Values>;
option<Key>(params: OptionParameters<Values, Key>): Termost<Values>;
output(params: OutputParameters<Values>): Termost<Values>;
output<Key>(params: OutputParameters<Values, Key>): Termost<Values>;
task<Key>(params: TaskParameters<Values, Key>): Termost<Values>;
};

Expand Down
11 changes: 10 additions & 1 deletion test/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,16 @@ exports[`termost should handle \`message\` api 1`] = `
 multilines
 with array input

You can also have a total control on the formatting through the \`format\` helper."
You can also have a total control on the formatting through the \`format\` helper.

鈩癸笍 Information
 A shareable value can also be displayed: Hello from previous output

鈩癸笍 Information
 I can also run asynchronous operations... Operation is starting...

鈩癸笍 Information
 Operation is finished"
`;
exports[`termost should handle \`option\` api 1`] = `
Expand Down
2 changes: 1 addition & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe("termost", () => {

test("should handle `message` api", async () => {
const output = await exec(
"node -r esbuild-register example/withMessage.ts"
"node -r esbuild-register example/withOutput.ts"
);

expect(output).toMatchSnapshot();
Expand Down

0 comments on commit ab597f0

Please sign in to comment.