From 28f261db605502b1a178b0045559be7bfbe53436 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Mon, 5 Feb 2024 23:47:07 +0000 Subject: [PATCH] file filter for checkdocs --- cli/cli.ts | 60 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/cli/cli.ts b/cli/cli.ts index c779ed702c5d..2a94177f6bd1 100644 --- a/cli/cli.ts +++ b/cli/cli.ts @@ -1914,7 +1914,7 @@ ${gcards.map(gcard => `[${gcard.name}](${gcard.url})`).join(',\n')} const kioskGames = targetConfig?.kiosk?.games; for (const game of (kioskGames ?? [])) { if (game.name) targetStrings[`{id:game-name}${game.name}`] = game.name; - if (game.description) targetStrings[`{id:game-description}${game.description}`] = game.description; + if (game.description) targetStrings[`{id:game-description}${game.description}`] = game.description; } const approvedRepoLib = targetConfig?.packages?.approvedRepoLib; @@ -5729,7 +5729,8 @@ function checkDocsAsync(parsed?: commandParser.ParsedCommand): Promise { true, parsed.flags["re"] as string, !!parsed.flags["fix"], - !!parsed.flags["pycheck"] + !!parsed.flags["pycheck"], + parsed.flags["file"] as string ) } @@ -5750,9 +5751,10 @@ function checkFileSize(files: string[]): number { return maxSize; } -function internalCheckDocsAsync(compileSnippets?: boolean, re?: string, fix?: boolean, pycheck?: boolean): Promise { +function internalCheckDocsAsync(compileSnippets?: boolean, re?: string, fix?: boolean, pycheck?: boolean, fileFilter?: string): Promise { if (!nodeutil.existsDirSync("docs")) return Promise.resolve(); + const imageChecks = !fileFilter const docsRoot = nodeutil.targetDir; const docsTemplate = server.expandDocFileTemplate("docs.html") pxt.log(`checking docs`); @@ -5772,41 +5774,44 @@ function internalCheckDocsAsync(compileSnippets?: boolean, re?: string, fix?: bo const existingSnippets: pxt.Map = {}; let snippets: CodeSnippet[] = []; - const maxFileSize = checkFileSize(nodeutil.allFiles("docs", { maxDepth: 10, allowMissing: true, includeDirs: true, ignoredFileMarker: ".ignorelargefiles" })); + const maxFileSize = imageChecks ? checkFileSize(nodeutil.allFiles("docs", { maxDepth: 10, allowMissing: true, includeDirs: true, ignoredFileMarker: ".ignorelargefiles" })) : 0; if (!pxt.appTarget.ignoreDocsErrors && maxFileSize > (pxt.appTarget.cloud.maxFileSize || (5000000))) U.userError(`files too big in docs folder`); // scan and fix image links - nodeutil.allFiles("docs", { ignoredFileMarker: ignoredFoldersKey }) - .filter(f => /\.md/.test(f)) - .forEach(f => { - let md = fs.readFileSync(f, { encoding: "utf8" }); - let newmd = md.replace(/]\((\/static\/[^)]+?)\.(png|jpg)(\s+"[^"]+")?\)/g, (m: string, p: string, ext: string, comment: string) => { - let fn = path.join(docsRoot, "docs", `${p}.${ext}`); - if (fs.existsSync(fn)) + if (imageChecks) + nodeutil.allFiles("docs", { ignoredFileMarker: ignoredFoldersKey }) + .filter(f => /\.md/.test(f)) + .forEach(f => { + let md = fs.readFileSync(f, { encoding: "utf8" }); + let newmd = md.replace(/]\((\/static\/[^)]+?)\.(png|jpg)(\s+"[^"]+")?\)/g, (m: string, p: string, ext: string, comment: string) => { + let fn = path.join(docsRoot, "docs", `${p}.${ext}`); + if (fs.existsSync(fn)) + return m; + // try finding other file + let next = ext == "png" ? "jpg" : "png"; + const exists = fs.existsSync(path.join(docsRoot, "docs", `${p}.${next}`)); + if (exists && fix) + return `](${p}.${next}${comment ? " " : ""}${comment || ""})`; + + // broken image or resources + broken++; + pxt.log(`missing file ${p}.${ext}`) return m; - // try finding other file - let next = ext == "png" ? "jpg" : "png"; - const exists = fs.existsSync(path.join(docsRoot, "docs", `${p}.${next}`)); - if (exists && fix) - return `](${p}.${next}${comment ? " " : ""}${comment || ""})`; - - // broken image or resources - broken++; - pxt.log(`missing file ${p}.${ext}`) - return m; + }); + if (fix && md != newmd) { + pxt.log(`patching ${f}`) + nodeutil.writeFileSync(f, newmd, { encoding: "utf8" }) + } }); - if (fix && md != newmd) { - pxt.log(`patching ${f}`) - nodeutil.writeFileSync(f, newmd, { encoding: "utf8" }) - } - }); function addSnippet(snippet: CodeSnippet, entryPath: string, snipIndex: number, src: string) { const key = `${src}${snipIndex}`; if (existingSnippets[key]) return; + if (fileFilter && !snippet.file?.includes(fileFilter)) + return; snippets.push(snippet); const dir = path.join("temp/snippets", snippet.type); const fn = `${dir}/${entryPath.replace(/^\//, '').replace(/[\/\s]/g, '-').replace(/\.\w+$/, '')}-${snipIndex}.${snippet.ext}`; @@ -7047,6 +7052,9 @@ ${pxt.crowdin.KEY_VARIABLE} - crowdin key description: "Check code snippets by round-tripping to .py and comparing the " + "original and result .ts. This will generate lots of false positives but can " + "still be useful for searching for semantic issues." + }, + file: { + description: "partial file name matching, also disables image checking" } } }, checkDocsAsync);