Skip to content

Commit

Permalink
fix: refine debug
Browse files Browse the repository at this point in the history
  • Loading branch information
weareoutman committed Oct 24, 2020
1 parent 3b1470b commit 554c451
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -115,6 +115,15 @@ html[data-theme="dark"] {
}
```

## Trouble Shooting

When building your docs project, Set the env `DEBUG=search-local:*` to enable [debug](https://github.com/visionmedia/debug) logs.

```shell
# In your docs project:
DEBUG=search-local:* yarn build
```

## Contributing

See [contributing guide](CONTRIBUTING.md).
6 changes: 6 additions & 0 deletions src/server/utils/debug.ts
@@ -0,0 +1,6 @@
import createDebug from "debug";

const debug = createDebug("search-local");
export const debugVerbose = debug.extend("verbose");
export const debugInfo = debug.extend("info");
export const debugWarn = debug.extend("warn");
9 changes: 3 additions & 6 deletions src/server/utils/parsePage.ts
@@ -1,13 +1,10 @@
import { ParsedDocument } from "../../shared/interfaces";
import { debugWarn } from "./debug";
import { getCondensedText } from "./getCondensedText";
import _debug from "debug";

const debug = _debug("search-local");

export function parsePage($: cheerio.Root, url: string): ParsedDocument {
$("a[aria-hidden=true]").remove();

$("a[aria-hidden=true]").remove();
let $pageTitle = $("h1").first();
if ($pageTitle.length === 0) {
$pageTitle = $("title");
Expand All @@ -17,8 +14,8 @@ export function parsePage($: cheerio.Root, url: string): ParsedDocument {

const $main = $("main");
if ($main.length === 0) {
debug(
"Page has no <main>, therefore no content was indexed for this page '%s'.",
debugWarn(
"page has no <main>, therefore no content was indexed for this page %o",
url
);
}
Expand Down
13 changes: 6 additions & 7 deletions src/server/utils/postBuildFactory.ts
@@ -1,38 +1,37 @@
import fs from "fs";
import path from "path";
import util from "util";
import _debug from "debug";
import { ProcessedPluginOptions, PostBuildData } from "../../shared/interfaces";
import { buildIndex } from "./buildIndex";
import { debugInfo } from "./debug";
import { processDocInfos } from "./processDocInfos";
import { scanDocuments } from "./scanDocuments";

const debug = _debug("search-local");
const writeFileAsync = util.promisify(fs.writeFile);

export function postBuildFactory(config: ProcessedPluginOptions) {
return async function postBuild(buildData: PostBuildData): Promise<void> {
debug("Gathering documents");
debugInfo("gathering documents");

const data = processDocInfos(buildData, config);

debug("Parsing documents");
debugInfo("parsing documents");

// Give every index entry a unique id so that the index does not need to store long URLs.
const allDocuments = await scanDocuments(data);

debug("Building index");
debugInfo("building index");

const searchIndex = buildIndex(allDocuments, config);

debug("Writing index to disk");
debugInfo("writing index to disk");

await writeFileAsync(
path.join(buildData.outDir, "search-index.json"),
JSON.stringify(searchIndex),
{ encoding: "utf8" }
);

debug("Index written to disk, success!");
debugInfo("index written to disk successfully!");
};
}
11 changes: 8 additions & 3 deletions src/server/utils/scanDocuments.ts
@@ -1,11 +1,11 @@
import fs from "fs";
import path from "path";
import util from "util";
import _debug from "debug";
import { DocInfoWithFilePath, SearchDocument } from "../../shared/interfaces";
import { parse } from "./parse";
import { debugVerbose } from "./debug";

const readFileAsync = util.promisify(fs.readFile);
const debug = _debug("search-local");

let nextDocId = 0;
const getNextDocId = () => {
Expand All @@ -22,7 +22,12 @@ export async function scanDocuments(

await Promise.all(
DocInfoWithFilePathList.map(async ({ filePath, url, type }) => {
debug(`Parsing ${type} file ${filePath}`, { url });
debugVerbose(
`parsing %s file %o of %o`,
type,
path.relative(process.cwd(), filePath),
url
);

const html = await readFileAsync(filePath, { encoding: "utf8" });
const { pageTitle, sections } = parse(html, type, url);
Expand Down

0 comments on commit 554c451

Please sign in to comment.