generated from linz/template-javascript-hello-world
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: annotate commands with examples (#1097)
#### Motivation Storing examples with the code makes it easier to update the examples at the same time the code is updated #### Modification Annotate commands with a examples in markdown #### Checklist _If not applicable, provide explanation of why._ - [ ] Tests updated - [ ] Docs updated - [ ] Issue linked in Title
- Loading branch information
Showing
19 changed files
with
94 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export const md = { | ||
/** | ||
* Create a markdown code block | ||
* | ||
* @param body body of the code block | ||
* @param lang language to use eg `bash` | ||
* @returns markdown formatted code block | ||
*/ | ||
code(lang: 'bash' | 'typescript', body: string): string { | ||
return ` | ||
\`\`\`${lang} | ||
${body} | ||
\`\`\` | ||
`; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
export const ExampleSymbol = Symbol('example-text'); | ||
|
||
export interface CommandWithExamples { | ||
/** | ||
* list of examples to be added to the command README.md | ||
* | ||
* @see {@link generateReadme} for README generation | ||
*/ | ||
[ExampleSymbol]: { title: string; text: string }[]; | ||
} | ||
|
||
/** | ||
* Annotate a command with examples | ||
* | ||
* @param cmd Command to annotate | ||
* @param exampleTitle Title for the example | ||
* @param exampleText body of the example, markdown is supported | ||
*/ | ||
export function annotateExample(cmd: unknown, exampleTitle: string, exampleText: string): void { | ||
if (cmd == null) throw new Error('Command is null'); | ||
const ce = cmd as CommandWithExamples; | ||
const examples = ce[ExampleSymbol] ?? []; | ||
|
||
examples.push({ title: exampleTitle, text: exampleText }); | ||
ce[ExampleSymbol] = examples; | ||
} | ||
|
||
/** | ||
* Does the command have examples annotated | ||
* | ||
* @see {@link annotateExample} | ||
* | ||
* @param cmd | ||
* @returns whether there are examples | ||
*/ | ||
export function commandHasExample<T>(cmd: T): cmd is T & CommandWithExamples { | ||
if (cmd == null) return false; | ||
return (cmd as unknown as CommandWithExamples)[ExampleSymbol] != null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters