Skip to content

Commit 86d0ae6

Browse files
author
Alessio Michelini
committed
[BREAKING] Changed the default behaviour to update the file
Upgraded to TS 5 Updated dependencies Updated tests to match new default behaviour Code cleanups
1 parent 8061c4d commit 86d0ae6

File tree

6 files changed

+27
-16
lines changed

6 files changed

+27
-16
lines changed

README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ $ npm install -g md-index-generator
1717
$ md-index-generator COMMAND
1818
running command...
1919
$ md-index-generator (-v|--version|version)
20-
md-index-generator/0.10.0 darwin-x64 node-v14.15.3
20+
1.0.0
2121

22-
// Display the output on the shell
22+
// Replace the passed markdown file
2323
$ md-index-generator <MarkDown.md>
2424

25+
// Display the output on the shell
26+
$ md-index-generator <MarkDown.md> --dry-run
27+
2528
// Store the output in a file
2629
$ md-index-generator <MARKDOWNFILE.md> --output <Index.md>
2730

@@ -30,11 +33,12 @@ $ md-index-generator <MarkDown.md> --depth 3
3033

3134
// Set the title of the index, output will start with "## Docs index"
3235
$ md-index-generator <MarkDown.md> --title "Docs index"
33-
34-
// Replace the passed markdown file
35-
$ md-index-generator <MarkDown.md> --replace
3636
```
3737

38+
## Breaking Changes
39+
40+
From version `1.x` onward, the logic of the CLI has changed, where by default it replace the index of the source file, while before you had to explicitly use the `--replace` flag to do so. If you want to see the index generated on screen, you must pass the `--dry-run` flag instead.
41+
3842
### Optional flags
3943

4044
`-o, --output <VALUE>` This takes an output file, where the parsed index will be stored instead of being shown in the shell
@@ -43,7 +47,7 @@ $ md-index-generator <MarkDown.md> --replace
4347

4448
`-t, --title <VALUE>` This set the title for the index list, by default is set to `Index`
4549

46-
`-r, --replace` This flag will set to add the index in the source file
50+
`-r, --dry-run` This flag will show the output on screen, but it will not update the source file
4751

4852
#### Add the index in the source file
4953

@@ -62,4 +66,4 @@ The index will go here!
6266
Hello
6367
```
6468

65-
> If you don't add the index tags, the script will automatically put the index before the first Heading 2 in the document. It is recommended to add the tags to properly control where the index will be added.
69+
> If you don't add the index tags, the script will automatically put the index before the first Heading 2 in the document. It is recommended to add the tags to properly control where the index will be added.

bin/run

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ program
1010
.option('-o, --output <outputFile>', 'Output will be saved in the defined file')
1111
.option('-d, --depth <depth>', 'Depth of the headings to parse, 4 means \'till h4\'', 4)
1212
.option('-t, --title <title>', 'Title to add on top of the index, by default is \'## Index\'')
13-
.option('-r, --replace', 'Add the index to the source file', false)
13+
.option('-r, --dry-run', 'Show the result on screen, but doesn\'t modify the file', false)
1414
.action(MarkdownIndexGenerator);
1515

1616
program.parse();

src/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import { Flags } from './interfaces/types';
33
export const INDEX_TAG = 'index';
44
export const VALID_FORMATS = ['md', 'mdx'];
55
export const DEFAULT_VALUES: Flags = {
6-
replace: false,
6+
dryRun: false,
77
depth: 4,
88
}

src/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,20 @@ const MarkdownIndexGenerator = async(file: string, flags?: Flags) => {
2626
if (flags?.output) {
2727
await parser.toFile(flags.output);
2828
console.log(`File ${flags.output} saved!`);
29-
} else if (flags?.replace) {
29+
return process.exit(0);
30+
}
31+
32+
if (!flags?.dryRun) {
3033
await parser.replaceOriginal();
3134
console.log(`File ${file} updated!`);
32-
} else {
35+
return process.exit(0);
36+
}
37+
38+
if (flags?.dryRun) {
3339
console.log('--- Begin MarkDown ---');
3440
console.log(parser.toView());
3541
console.log('--- End Markdown ---');
42+
return process.exit(0);
3643
}
3744
}
3845

src/interfaces/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
export type HeadingsRange = 2|3|4|5;
22

33
export interface Flags {
4-
depth: number;
5-
replace: boolean;
4+
depth?: number;
5+
dryRun?: boolean;
66
title?: string;
77
output?: string;
88
}

test/index.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('MarkdownIndexGenerator', () => {
1010
});
1111

1212
it('should show the generated menu', async () => {
13-
await MarkdownIndexGenerator('./test/__mocks__/test.md');
13+
await MarkdownIndexGenerator('./test/__mocks__/test.md', { dryRun: true });
1414
expect(console.log).toHaveBeenCalledWith('--- Begin MarkDown ---');
1515
expect(console.log).toHaveBeenCalledWith(expect.stringContaining('Index'));
1616
expect(console.log).toHaveBeenCalledWith(expect.stringContaining('[Heading 2](#heading-2)'));
@@ -19,15 +19,15 @@ describe('MarkdownIndexGenerator', () => {
1919
});
2020

2121
it('should show the generated menu, but only at depth 2', async () => {
22-
await MarkdownIndexGenerator('./test/__mocks__/test.md', { depth: 2, replace: false});
22+
await MarkdownIndexGenerator('./test/__mocks__/test.md', { depth: 2, dryRun: true});
2323
expect(console.log).toHaveBeenCalledWith('--- Begin MarkDown ---');
2424
expect(console.log).toHaveBeenCalledWith(expect.stringContaining('[Heading 2](#heading-2)'));
2525
expect(console.log).toHaveBeenCalledWith(expect.not.stringContaining('- [Sub sub heading 4](#sub-sub-heading-4)'));
2626
expect(console.log).toHaveBeenCalledWith('--- End Markdown ---');
2727
});
2828

2929
it('should show the generated menu, and contain a custom title', async () => {
30-
await MarkdownIndexGenerator('./test/__mocks__/test.md', { title: 'test', depth: 2, replace: false });
30+
await MarkdownIndexGenerator('./test/__mocks__/test.md', { title: 'test', depth: 2, dryRun: true });
3131
expect(console.log).toHaveBeenCalledWith('--- Begin MarkDown ---');
3232
expect(console.log).toHaveBeenCalledWith(expect.not.stringContaining('Index'));
3333
expect(console.log).toHaveBeenCalledWith(expect.stringContaining('[Heading 2](#heading-2)'));

0 commit comments

Comments
 (0)