Skip to content

Commit

Permalink
Merge pull request #31 from Roaders/codeExamples
Browse files Browse the repository at this point in the history
Add support for insertCode
  • Loading branch information
Roaders authored May 9, 2022
2 parents 5087421 + 45f0da3 commit c97d8a0
Show file tree
Hide file tree
Showing 27 changed files with 10,337 additions and 555 deletions.
4 changes: 4 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,10 @@ Markers in the document describe where the content should be inserted, existing
| **markdownPath** | **m** | string | The file to write to. Without replacement markers the whole file content will be replaced. Path can be absolute or relative. |
| **replaceBelow** | | string | A marker in the file to replace text below. |
| **replaceAbove** | | string | A marker in the file to replace text above. |
| **insertCodeBelow** | | string | A marker in the file to insert code below. File path to insert must be added at the end of the line and optionally codeComment flag: 'insertToken file="path/toFile.md" codeComment="ts"' |
| **insertCodeAbove** | | string | A marker in the file to insert code above. |
| **copyCodeBelow** | | string | A marker in the file being inserted to say only copy code below this line |
| **copyCodeAbove** | | string | A marker in the file being inserted to say only copy code above this line |
| **jsFile** | **j** | string[] | jsFile to 'require' that has an export with the 'UsageGuideConfig' export. Multiple file can be specified. |
| **configImportName** | **c** | string[] | Export name of the 'UsageGuideConfig' object. Defaults to 'usageGuideInfo'. Multiple exports can be specified. |
| **verify** | **v** | boolean | Verify the markdown file. Does not update the file but returns a non zero exit code if the markdown file is not correct. Useful for a pre-publish script. |
Expand Down
10,237 changes: 9,714 additions & 523 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-command-line-args",
"version": "2.2.2",
"version": "2.3.0",
"description": "A Typescript wrapper around command-line-args with additional support for markdown usage guide generation",
"bin": {
"write-markdown": "dist/write-markdown.js"
Expand All @@ -22,14 +22,16 @@
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"clean": "rimraf dist",
"clean": "rimraf dist coverage",
"build": "tsc -p tsconfig.build.json",
"build:example": "tsc -p src/example",
"watch-build:example": "tsc -p src/example --watch",
"watch-build": "tsc --watch -p tsconfig.build.json",
"lint": "eslint . --ext .ts",
"test": "jest --ci --coverage && tsc --noemit",
"watch-test": "jest --watch",
"prebuild-release": "npm run clean",
"build-release": "concurrently --kill-others-on-fail npm:test npm:lint npm:build npm:verify-markdown && codecov",
"build-release": "concurrently --kill-others-on-fail npm:test npm:lint npm:build npm:build:example npm:verify-markdown && codecov",
"prewrite-markdown": "npm run build",
"preverify-markdown": "npm run build",
"write-markdown": "node dist/write-markdown -f package.json -p markdownConfig",
Expand All @@ -53,7 +55,7 @@
"string-format": "^2.0.0"
},
"devDependencies": {
"@morgan-stanley/ts-mocking-bird": "^0.2.2",
"@morgan-stanley/ts-mocking-bird": "^0.2.3",
"@types/chalk": "^2.2.0",
"@types/command-line-args": "^5.0.0",
"@types/command-line-usage": "^5.0.1",
Expand Down
17 changes: 16 additions & 1 deletion src/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,21 @@ export interface Content extends SectionHeader {
includeIn?: 'markdown' | 'cli' | 'both';
}

export interface IInsertCodeTokens {
insertCodeBelow?: string;
insertCodeAbove?: string;
copyCodeBelow?: string;
copyCodeAbove?: string;
removeDoubleBlankLines: boolean;
}

export interface IInsertCodeOptions extends IInsertCodeTokens {
// This is used as the base path to resolve relative paths from.
// This is not used for absolute paths
// if not provided process.cwd() is used for resolution
dirname?: string;
}

export interface IReplaceOptions {
replaceBelow?: string;
replaceAbove?: string;
Expand All @@ -297,7 +312,7 @@ export interface IReplaceOptions {

export type JsImport = { jsFile: string; importName: string };

export interface IWriteMarkDown extends IReplaceOptions {
export interface IWriteMarkDown extends IReplaceOptions, IInsertCodeTokens {
markdownPath: string;
jsFile: string[];
configImportName: string[];
Expand Down
2 changes: 1 addition & 1 deletion src/example/additionalModifiers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parse } from '../parse';
import { parse } from '../';
import { additionalModifiers } from './configs';

const args = parse(additionalModifiers.arguments, additionalModifiers.parseOptions);
Expand Down
2 changes: 1 addition & 1 deletion src/example/configs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-useless-escape */
import { ArgumentConfig, UsageGuideConfig } from '../contracts';
import { ArgumentConfig, UsageGuideConfig } from '../';

/**
* we do not have any side effects in this file as this file is required by write-markdown
Expand Down
2 changes: 1 addition & 1 deletion src/example/exampleConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parse } from '../parse';
import { parse } from '../';
import { argumentConfig } from './configs';

const args = parse(argumentConfig);
Expand Down
3 changes: 1 addition & 2 deletions src/example/exampleConfigHelpWithMissingArgs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { CommandLineOption } from '../contracts';
import { parse } from '../parse';
import { parse, CommandLineOption } from '../';
import { argumentConfig } from './configs';

const args = parse(argumentConfig, {
Expand Down
2 changes: 1 addition & 1 deletion src/example/exampleConfigUsingPrintHelp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parse } from '../parse';
import { parse } from '../';
import { argumentConfig } from './configs';

const args = parse(argumentConfig, { hideMissingArgMessages: true }, false, true);
Expand Down
2 changes: 1 addition & 1 deletion src/example/exampleConfigWithHelp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parse } from '../parse';
import { parse } from '../';

interface ICopyFilesArguments {
sourcePath?: string;
Expand Down
2 changes: 1 addition & 1 deletion src/example/exitProcessExample.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parse } from '../parse';
import { parse } from '../';
import { argumentConfig } from './configs';

/**
Expand Down
21 changes: 21 additions & 0 deletions src/example/insert-code-sample.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Code inserted from file:

[//]: # (ts-command-line-args_write-markdown_insertCodeBelow file="./insert-code.example.ts" codeComment="ts")
```ts
async function insertSampleCode() {
const fileContent = readFileSync('src/example/insert-code-sample.md').toString();

const newContent = await insertCode(fileContent, {
insertCodeBelow: insertCodeBelowDefault,
insertCodeAbove: insertCodeAboveDefault,
copyCodeBelow: copyCodeBelowDefault,
copyCodeAbove: copyCodeAboveDefault,
dirname: dirname(`src/example/insert-code-sample.md`),
});

writeFileSync('src/example/insert-code-sample.md', newContent);
}
```
[//]: # (ts-command-line-args_write-markdown_insertCodeAbove)

The above code was inserted using `ts-command-line-args`
27 changes: 27 additions & 0 deletions src/example/insert-code.example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { insertCode } from '../';
import { readFileSync, writeFileSync } from 'fs';
import { dirname } from 'path';
import {
copyCodeAboveDefault,
copyCodeBelowDefault,
insertCodeAboveDefault,
insertCodeBelowDefault,
} from '../write-markdown.constants';

// ts-command-line-args_write-markdown_copyCodeBelow
async function insertSampleCode() {
const fileContent = readFileSync('src/example/insert-code-sample.md').toString();

const newContent = await insertCode(fileContent, {
insertCodeBelow: insertCodeBelowDefault,
insertCodeAbove: insertCodeAboveDefault,
copyCodeBelow: copyCodeBelowDefault,
copyCodeAbove: copyCodeAboveDefault,
dirname: dirname(`src/example/insert-code-sample.md`),
});

writeFileSync('src/example/insert-code-sample.md', newContent);
}
// ts-command-line-args_write-markdown_copyCodeAbove

insertSampleCode();
2 changes: 1 addition & 1 deletion src/example/noExitProcessExample.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { argumentConfig } from './configs';
import { parse } from '../parse';
import { parse } from '../';

/**
* will ignore missing errors and not exit
Expand Down
6 changes: 6 additions & 0 deletions src/example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"declaration": false,
}
}
2 changes: 1 addition & 1 deletion src/example/typicalAppWithGroups.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parse } from '../parse';
import { parse } from '../';
import { typicalAppWithGroupsInfo } from './configs';

const args = parse(typicalAppWithGroupsInfo.arguments, typicalAppWithGroupsInfo.parseOptions);
Expand Down
2 changes: 1 addition & 1 deletion src/example/unknownArgumentsExample.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parse } from '../parse';
import { parse } from '../';
import { argumentConfig } from './configs';

/**
Expand Down
2 changes: 1 addition & 1 deletion src/example/usageGuideWithExamples.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parse } from '../parse';
import { parse } from '../';
import { exampleSections } from './configs';

const args = parse(exampleSections.arguments, exampleSections.parseOptions);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { addContent } from './content.helper';
import { addContent } from './add-content.helper';
import { IReplaceOptions } from '../contracts';

describe('conent.helper', () => {
describe('content.helper', () => {
describe('addContent', () => {
const newContent = `new content line one
new content line two`;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IReplaceOptions } from '../contracts';
import { footerReplaceBelowMarker } from '../write-markdown.constants';
import { splitContent, findEscapeSequence } from './line-ending.helper';
import { splitContent, findEscapeSequence, filterDoubleBlankLines } from './line-ending.helper';

/**
* Adds or replaces content between 2 markers within a text string
Expand Down Expand Up @@ -32,12 +32,12 @@ export function addContent(inputString: string, content: string | string[], opti
const linesBefore = lines.slice(0, replaceBelowIndex + 1);
const linesAfter = replaceAboveIndex >= 0 ? lines.slice(replaceAboveIndex) : [];

const constantLines = content.reduce(
const contentLines = content.reduce(
(lines, currentContent) => [...lines, ...splitContent(currentContent)],
new Array<string>(),
);

let allLines = [...linesBefore, ...constantLines, ...linesAfter];
let allLines = [...linesBefore, ...contentLines, ...linesAfter];

if (options.removeDoubleBlankLines) {
allLines = allLines.filter((line, index, lines) => filterDoubleBlankLines(line, index, lines));
Expand All @@ -60,11 +60,3 @@ ${footerReplaceBelowMarker}`;
removeDoubleBlankLines: false,
});
}

const nonWhitespaceRegExp = /[^ \t]/;

function filterDoubleBlankLines(line: string, index: number, lines: string[]): boolean {
const previousLine = index > 0 ? lines[index - 1] : undefined;

return nonWhitespaceRegExp.test(line) || previousLine == null || nonWhitespaceRegExp.test(previousLine);
}
3 changes: 2 additions & 1 deletion src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export * from './command-line.helper';
export * from './content.helper';
export * from './add-content.helper';
export * from './markdown.helper';
export * from './visitor';
export * from './line-ending.helper';
export * from './options.helper';
export * from './string.helper';
export * from './insert-code.helper';
Loading

0 comments on commit c97d8a0

Please sign in to comment.