Skip to content

Commit

Permalink
CLI combine command - add -e, --exts option
Browse files Browse the repository at this point in the history
  • Loading branch information
pileks committed Dec 1, 2022
1 parent d092fca commit 969322e
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 20 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ yarn add -D doc-snippets
- To inject a snippet:
- `"$snippet: snippet-name"` - This gets replaced by the snippet with the given name.

Currently, `doc-snippets` extracts snippets from the following file types:
`.ts`, `.json`, `.yaml`, `.txt`, `.md`, `.graphql`, and `.cue`.

Snippets are injected only into `.md` files.

### Running `doc-snippets`
Expand All @@ -45,6 +42,10 @@ doc-snippets combine ./snippets ./src/docs ./docs
#### Options

- `-i, --ignore <paths...>` - Ignore listed paths. Paths should be formatted according to the [gitignore spec 2.22.1](https://git-scm.com/docs/gitignore/2.22.1)
- By default, only `node_modules` is ignored.

- `-e, --exts <exts...>` - Parse files with listed extensions
- By default, files with the following extensions are parsed: `.ts`, `.json`, `.yaml`, `.txt`, `.md`, `.graphql`, and `.cue`

### In your own code

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "doc-snippets",
"version": "0.2.0",
"version": "0.3.0",
"description": "Extract and inject snippets from code into markdown files",
"author": {
"name": "Pileks",
Expand Down
11 changes: 9 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,21 @@ export const run = async (argv: string[]): Promise<void> => {
.argument("<docsDir>", "The documentation directory")
.argument("<outputDir>", "The output directory")
.option("-i, --ignore <paths...>", "Ignore specified paths")
.option("-e, --exts <exts...>", "Extensions to parse")
.action(
async (
snippetsDir: string,
docsDir: string,
outputDir: string,
{ ignore }
{ ignore, exts }
) => {
await combineDocsAndSnippets(snippetsDir, docsDir, outputDir, ignore);
await combineDocsAndSnippets(
snippetsDir,
docsDir,
outputDir,
exts,
ignore
);
}
);

Expand Down
6 changes: 5 additions & 1 deletion src/lib/combine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export async function combineDocsAndSnippets(
snippetsDir: string,
docsDir: string,
outputDir: string,
parseExts?: string[],
ignorePaths?: string[]
): Promise<void> {
console.log(`- Copy ${docsDir} to ${outputDir}`);
Expand All @@ -18,7 +19,10 @@ export async function combineDocsAndSnippets(
fse.copySync(docsDir, outputDir, { overwrite: true });

console.log("- Extract Snippets");
const snippets = await extractSnippets(snippetsDir, ignorePaths);
const snippets = await extractSnippets(snippetsDir, {
parseExts,
ignorePaths,
});

console.log("- Inject Snippets");
await injectSnippets(snippets, outputDir);
Expand Down
1 change: 1 addition & 0 deletions src/lib/defaults/defaultIgnorePaths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const defaultIgnorePaths = ["node_modules"];
9 changes: 9 additions & 0 deletions src/lib/defaults/defaultParseExts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const defaultParseExts = [
".ts",
".json",
".yaml",
".txt",
".md",
".graphql",
".cue",
];
2 changes: 2 additions & 0 deletions src/lib/defaults/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./defaultIgnorePaths";
export * from "./defaultParseExts";
40 changes: 27 additions & 13 deletions src/lib/extract.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,55 @@
import { defaultParseExts, defaultIgnorePaths } from "./defaults";

import path from "path";
import fs from "fs";
import ignore, { Ignore } from "ignore";

/**
* @typedef ExtractSnippetsOptions
* @type {object}
* @property {string[]} [parseExts] A list of file extensions which will be parsed for Snippets
* @property {string[]} [ignorePaths] A list of paths in `gitignore` format which will be ignored when parsing
*/
type ExtractSnippetsOptions = {
parseExts?: string[];
ignorePaths?: string[];
};

/**
* Extract snippets from all files within a directory
* @param {string} dir - The directory to search and extract snippets from
* @returns
* @param {string} dir The directory to search and extract snippets from
* @param {ExtractSnippetsOptions} options Extraction options
* @returns {Record<string, string>} A Record containing all extracted Snippets
*/
export async function extractSnippets(
dir: string,
ignorePaths?: string[]
options?: ExtractSnippetsOptions
): Promise<Record<string, string>> {
const snippets: Record<string, string> = {};

const ignoreInstance = ignore();

if (ignorePaths) {
ignoreInstance.add(ignorePaths);
if (options?.ignorePaths) {
ignoreInstance.add(options.ignorePaths);
} else {
ignoreInstance.add(defaultIgnorePaths);
}

await searchAndExtractSnippetsFromDir(snippets, dir, ignoreInstance);
const exts = options?.parseExts ?? defaultParseExts;

await searchAndExtractSnippetsFromDir(snippets, dir, exts, ignoreInstance);

return snippets;
}

async function searchAndExtractSnippetsFromDir(
snippets: Record<string, string>,
dir: string,
exts: string[],
ignoreInstance: Ignore
) {
const dirents = fs.readdirSync(dir, { withFileTypes: true });

// Only search specific types of files
const exts = [".ts", ".json", ".yaml", ".txt", ".md", ".graphql", ".cue"];

// Ignore specific directories
const filter = ["node_modules"];

const match = (str: string, tests: string[]) => {
for (const test of tests) {
if (str.indexOf(test) > -1) {
Expand All @@ -55,10 +68,11 @@ async function searchAndExtractSnippetsFromDir(

if (dirent.isFile() && match(dirent.name, exts)) {
await extractSnippetsFromFile(snippets, direntPath);
} else if (dirent.isDirectory() && !match(dirent.name, filter)) {
} else if (dirent.isDirectory()) {
await searchAndExtractSnippetsFromDir(
snippets,
direntPath,
exts,
ignoreInstance
);
}
Expand Down

0 comments on commit 969322e

Please sign in to comment.