diff --git a/bun.lock b/bun.lock index 46a60d321e..e6fc7bca61 100644 --- a/bun.lock +++ b/bun.lock @@ -234,6 +234,7 @@ "acorn": "^8.17.0", "acorn-walk": "^8.3.5", "htmlparser2": "^10.1.0", + "ignore": "^5.3.2", "linkedom": "^0.18.12", "postcss": "^8.5.8", "postcss-selector-parser": "^7.1.4", diff --git a/docs/packages/cli.mdx b/docs/packages/cli.mdx index 212d0c4959..3f72ed9947 100644 --- a/docs/packages/cli.mdx +++ b/docs/packages/cli.mdx @@ -2545,7 +2545,8 @@ video to disk. The zip excludes root `renders` and `snapshots`, `.git`, `node_modules`, `dist`, `.next`, `coverage`, dotfiles, and anything your `.hyperframesignore` rules -match. Those rules use gitignore syntax and apply to `hyperframes publish` too. +match. Those rules use gitignore syntax and apply to `hyperframes publish` too, and +the project lint behind `lint`, `check`, `snapshot` and `render` does not read compositions they match. Keep them narrow — a dynamically selected asset may have no static reference to protect it. diff --git a/packages/cli/src/utils/publishProject.ts b/packages/cli/src/utils/publishProject.ts index 9e0b2576ab..0719428df1 100644 --- a/packages/cli/src/utils/publishProject.ts +++ b/packages/cli/src/utils/publishProject.ts @@ -12,7 +12,8 @@ import { } from "node:fs"; import { parseHTML } from "linkedom"; import AdmZip from "adm-zip"; -import ignore, { type Ignore } from "ignore"; +import type { Ignore } from "ignore"; +import { projectIgnore } from "@hyperframes/lint"; import { CSS_URL_RE, isNonRelativeUrl, isPathInside, rewriteSrcsetUrls } from "@hyperframes/core"; import { decodeUrlPathVariants, encodeUrlPath } from "@hyperframes/parsers"; import { buildAuthHeaders } from "../auth/client.js"; @@ -24,12 +25,6 @@ import { writeProjectLink } from "./projectLink.js"; const IGNORED_DIRS = new Set([".git", "node_modules", "dist", ".next", "coverage"]); const IGNORED_FILES = new Set([".DS_Store", "Thumbs.db"]); -const HYPERFRAMES_IGNORE_FILE = ".hyperframesignore"; -// Unanchored (no leading/mid slash) so it matches a dot-prefixed name at any -// depth, same as the segments it replaces below — but as a matcher pattern -// instead of a hard skip, a project's own `.hyperframesignore` negation can -// still override it. -const DEFAULT_PROJECT_IGNORE = ["/renders/", "/snapshots/", ".*"]; const PUBLISH_CONTENT_TYPE = "application/zip"; const PUBLISH_METADATA_TIMEOUT_MS = 30_000; const PUBLISH_UPLOAD_MIN_TIMEOUT_MS = 120_000; @@ -301,21 +296,12 @@ export function uploadTimeoutMs(byteLength: number): number { // Absolute, non-negotiable exclusions only — anything a `.hyperframesignore` // negation rule should be able to override (including dot-prefixed paths; -// see DEFAULT_PROJECT_IGNORE) must go through the ignore matcher instead of +// see projectIgnore) must go through the ignore matcher instead of // short-circuiting here. function shouldIgnoreSegment(segment: string): boolean { return IGNORED_DIRS.has(segment) || IGNORED_FILES.has(segment); } -function createProjectIgnore(rootDir: string): Ignore { - const matcher = ignore().add(DEFAULT_PROJECT_IGNORE); - const ignorePath = join(rootDir, HYPERFRAMES_IGNORE_FILE); - if (existsSync(ignorePath)) { - matcher.add(readFileSync(ignorePath, "utf-8")); - } - return matcher; -} - function collectProjectFiles( rootDir: string, currentDir: string, @@ -636,7 +622,7 @@ export function stagePublishFiles( ): Map { const absProjectDir = resolve(projectDir); const filePaths: string[] = []; - collectProjectFiles(absProjectDir, absProjectDir, filePaths, createProjectIgnore(absProjectDir)); + collectProjectFiles(absProjectDir, absProjectDir, filePaths, projectIgnore(absProjectDir)); if (!filePaths.includes("index.html")) { throw new Error( "Project archive must include index.html at the root. Check that .hyperframesignore does not exclude it.", diff --git a/packages/lint/package.json b/packages/lint/package.json index 6e096a112e..a4e1bc46ed 100644 --- a/packages/lint/package.json +++ b/packages/lint/package.json @@ -57,6 +57,7 @@ "acorn": "^8.17.0", "acorn-walk": "^8.3.5", "htmlparser2": "^10.1.0", + "ignore": "^5.3.2", "linkedom": "^0.18.12", "postcss": "^8.5.8", "postcss-selector-parser": "^7.1.4" diff --git a/packages/lint/src/index.ts b/packages/lint/src/index.ts index 14633b0819..a862a6e8ee 100644 --- a/packages/lint/src/index.ts +++ b/packages/lint/src/index.ts @@ -13,4 +13,5 @@ export { } from "./hyperframeLinter.js"; export { collectExternalStyles, lintProject, shouldBlockRender } from "./project.js"; export type { ProjectLintResult } from "./project.js"; +export { projectIgnore } from "./projectIgnore.js"; export { audioAttributeShapeError, type AudioAttribute } from "./rules/audioAttributes.js"; diff --git a/packages/lint/src/project.test.ts b/packages/lint/src/project.test.ts index e45b1fdb83..b30fd41d0e 100644 --- a/packages/lint/src/project.test.ts +++ b/packages/lint/src/project.test.ts @@ -34,6 +34,7 @@ function makeProject(indexHtml: string, subComps?: Record): stri const compsDir = join(dir, "compositions"); mkdirSync(compsDir, { recursive: true }); for (const [name, html] of Object.entries(subComps)) { + mkdirSync(dirname(join(compsDir, name)), { recursive: true }); writeFileSync(join(compsDir, name), html); } } @@ -114,6 +115,24 @@ describe("blank_root_with_standalone_composition", () => { expect(finding?.suggestedComposition).toBe("compositions/index.html"); }); + it("does not count a composition in a folder the project's .hyperframesignore leaves out", async () => { + const project = makeProject(validHtml(), { + "archive/index.html": ` +
+
Old cut
+
+`, + }); + writeFileSync(join(project, ".hyperframesignore"), "compositions/archive/\n"); + + const { results } = await lintProject(project); + + expect(results.map((entry) => entry.file)).toEqual(["index.html"]); + expect( + results.flatMap((entry) => entry.result.findings).map((finding) => finding.code), + ).not.toContain("blank_root_with_standalone_composition"); + }); + it("treats non-rendering script, style, link, meta, and template children as blank", async () => { const shellOnlyRoot = validHtml().replace( "", diff --git a/packages/lint/src/project.ts b/packages/lint/src/project.ts index 908f68f92a..efa00370b2 100644 --- a/packages/lint/src/project.ts +++ b/packages/lint/src/project.ts @@ -1,6 +1,7 @@ export { shouldBlockRender } from "./shouldBlockRender.js"; import { createHash } from "node:crypto"; import { existsSync, readdirSync } from "node:fs"; +import { projectIgnore } from "./projectIgnore.js"; import { dirname, extname, join, relative, resolve } from "node:path"; import { rewriteAssetPath } from "@hyperframes/parsers/asset-paths"; import { spansOverlap } from "@hyperframes/parsers/time-spans"; @@ -315,10 +316,12 @@ export async function lintProject( const allHtmlSources: HtmlSource[] = [{ html: rootHtml, compSrcPath: rootCompSrcPath }]; const compositionsDir = resolve(projectDir, "compositions"); if (!entryFile && existsSync(compositionsDir)) { + const ignored = projectIgnore(projectDir); const collectHtmlFiles = (dir: string, rel: string): string[] => { const out: string[] = []; for (const entry of readdirSync(dir, { withFileTypes: true })) { const relPath = rel ? `${rel}/${entry.name}` : entry.name; + if (ignored.ignores(`compositions/${relPath}${entry.isDirectory() ? "/" : ""}`)) continue; if (entry.isDirectory()) { // Registry components are source snippets, not independently mounted // sub-compositions. Linting every installed template here makes an diff --git a/packages/lint/src/projectIgnore.ts b/packages/lint/src/projectIgnore.ts new file mode 100644 index 0000000000..d5978170cd --- /dev/null +++ b/packages/lint/src/projectIgnore.ts @@ -0,0 +1,13 @@ +import { existsSync, readFileSync } from "node:fs"; +import { join } from "node:path"; +import ignore, { type Ignore } from "ignore"; + +const DEFAULT_PROJECT_IGNORE = ["/renders/", "/snapshots/", ".*"]; + +/** What the project leaves out; ask with a relative `/` path, a folder with a trailing `/`. */ +export function projectIgnore(projectDir: string): Ignore { + const matcher = ignore().add(DEFAULT_PROJECT_IGNORE); + const ignorePath = join(projectDir, ".hyperframesignore"); + if (existsSync(ignorePath)) matcher.add(readFileSync(ignorePath, "utf-8")); + return matcher; +}