From 554c451d04f3d6b1f2fa2f4732507d5e75655de7 Mon Sep 17 00:00:00 2001 From: weareoutman Date: Sat, 24 Oct 2020 12:17:16 +0800 Subject: [PATCH] fix: refine debug --- README.md | 9 +++++++++ src/server/utils/debug.ts | 6 ++++++ src/server/utils/parsePage.ts | 9 +++------ src/server/utils/postBuildFactory.ts | 13 ++++++------- src/server/utils/scanDocuments.ts | 11 ++++++++--- 5 files changed, 32 insertions(+), 16 deletions(-) create mode 100644 src/server/utils/debug.ts diff --git a/README.md b/README.md index 8a20dfb1..36d3eb4c 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/src/server/utils/debug.ts b/src/server/utils/debug.ts new file mode 100644 index 00000000..6c65d0ba --- /dev/null +++ b/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"); diff --git a/src/server/utils/parsePage.ts b/src/server/utils/parsePage.ts index 96e11844..e2c88f2b 100644 --- a/src/server/utils/parsePage.ts +++ b/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"); @@ -17,8 +14,8 @@ export function parsePage($: cheerio.Root, url: string): ParsedDocument { const $main = $("main"); if ($main.length === 0) { - debug( - "Page has no
, therefore no content was indexed for this page '%s'.", + debugWarn( + "page has no
, therefore no content was indexed for this page %o", url ); } diff --git a/src/server/utils/postBuildFactory.ts b/src/server/utils/postBuildFactory.ts index 336b37f5..deaf60f7 100644 --- a/src/server/utils/postBuildFactory.ts +++ b/src/server/utils/postBuildFactory.ts @@ -1,31 +1,30 @@ 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 { - 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"), @@ -33,6 +32,6 @@ export function postBuildFactory(config: ProcessedPluginOptions) { { encoding: "utf8" } ); - debug("Index written to disk, success!"); + debugInfo("index written to disk successfully!"); }; } diff --git a/src/server/utils/scanDocuments.ts b/src/server/utils/scanDocuments.ts index 010f3777..601042b4 100644 --- a/src/server/utils/scanDocuments.ts +++ b/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 = () => { @@ -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);