Skip to content

Commit 79dff20

Browse files
feat(scripts): typecheck the scripts directory (#3149)
* feat(scripts): typecheck the scripts directory scripts/ was the one TypeScript surface nothing typechecked. Adding a project for it surfaced real errors rather than style: a preview generator passing string | undefined where a string was required, a readdir result indexed without a bound, and two non-null assertions standing in for a filter that could have narrowed the type instead. The two preview generators had also drifted into sharing a capture setup, down to the comment explaining why the capture is opaque. That lifts into scripts/preview-capture.ts, so the reason is written once and both callers own the handles they have to close. @hyperframes/core and @hyperframes/producer become dev dependencies because the scripts import them; without that the project resolves on a machine with a warm node_modules and fails in CI. * fix(scripts): use source imports consistently
1 parent db3de4c commit 79dff20

15 files changed

Lines changed: 139 additions & 98 deletions

‎.github/workflows/ci.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ jobs:
259259
- run: bash scripts/ci/install-workspace-dependencies.sh
260260
- run: bun run build
261261
- run: bun run --filter '*' typecheck
262+
- run: bun run typecheck:scripts
262263

263264
test:
264265
name: Test

‎lefthook.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pre-commit:
1919
run: bun packages/cli/scripts/gen-skills-manifest.ts && git add skills-manifest.json
2020
typecheck:
2121
glob: "*.{ts,tsx}"
22-
run: cd packages/core && bunx tsc --noEmit && cd ../studio && bunx tsc --noEmit
22+
run: cd packages/core && bunx tsc --noEmit && cd ../studio && bunx tsc --noEmit && cd ../.. && bunx tsc --noEmit -p scripts/tsconfig.json
2323
# Mirrors the CI gate (same `--base origin/main` so the local hook can't
2424
# pass on a branch CI would fail). Audits the working tree, which means
2525
# unstaged WIP in `packages/**` is part of the diff — stash before

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"format:check": "oxfmt --check .",
5050
"knip": "knip",
5151
"test:scripts": "node --import tsx --test scripts/check-tracked-artifacts.test.mjs scripts/check-docs-snippet-motion.test.mjs scripts/registry-target-paths.test.mjs scripts/check-workspace-contracts.test.mjs scripts/check-package-cycles.test.mjs scripts/check-cli-process-ownership.test.mjs scripts/package-subpaths.test.mjs scripts/validate-release-channel.test.mjs scripts/publish-workflow.test.mjs scripts/install-workspace-dependencies.test.mjs scripts/draft-changelog.test.ts scripts/set-version.test.ts scripts/release-prepare.test.ts scripts/cli-options.test.ts scripts/changelog-weekly.test.ts scripts/claude-plugin-compression.test.ts scripts/studio-runtime-smoke.test.mjs scripts/verify-packed-manifests.test.mjs scripts/lint-skills.test.mjs packages/gcp-cloud-run/check-dockerfile-workspaces.test.mjs",
52+
"typecheck:scripts": "tsc --noEmit -p scripts/tsconfig.json",
5253
"test:skills": "node --test 'skills/**/*.test.mjs'",
5354
"generate:previews": "tsx scripts/generate-template-previews.ts",
5455
"generate:catalog-previews": "tsx scripts/generate-catalog-previews.ts",

‎scripts/backfill-block-previews.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* npx tsx scripts/backfill-block-previews.ts --dry-run # preview changes only
1111
*/
1212

13-
import { readdirSync, readFileSync, writeFileSync } from "node:fs";
13+
import { readdirSync, readFileSync, writeFileSync, type Dirent } from "node:fs";
1414
import { join, resolve, dirname } from "node:path";
1515
import { fileURLToPath } from "node:url";
1616

@@ -44,7 +44,7 @@ function writeManifest(manifestPath: string, manifest: Record<string, unknown>):
4444

4545
function backfillBlocks() {
4646
const blocksDir = join(registryDir, "blocks");
47-
let entries: ReturnType<typeof readdirSync<string>>;
47+
let entries: Dirent[];
4848
try {
4949
entries = readdirSync(blocksDir, { withFileTypes: true });
5050
} catch {
@@ -81,7 +81,7 @@ function backfillBlocks() {
8181

8282
function normalizeComponents() {
8383
const componentsDir = join(registryDir, "components");
84-
let entries: ReturnType<typeof readdirSync<string>>;
84+
let entries: Dirent[];
8585
try {
8686
entries = readdirSync(componentsDir, { withFileTypes: true });
8787
} catch {

‎scripts/changelog-weekly.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ function capitalize(value: string) {
531531
if (!value) {
532532
return value;
533533
}
534-
return value[0].toUpperCase() + value.slice(1);
534+
return value.charAt(0).toUpperCase() + value.slice(1);
535535
}
536536

537537
function git(args: string[]) {

‎scripts/cli-options.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function parseMappedArgument<
2727
parsed: Parsed,
2828
config: ParserConfig<Parsed, ValueKey, BooleanKey>,
2929
) {
30-
const arg = args[index];
30+
const arg = args[index] ?? config.fail(`No argument at index ${index}`);
3131

3232
if (applyInlineValueOption(arg, parsed, config.inlineValueOptions)) {
3333
return index;

‎scripts/draft-changelog.ts‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -273,12 +273,8 @@ export function parseConventionalSubject(subject: string): ParsedSubject {
273273
};
274274
}
275275

276-
return {
277-
type: match[1],
278-
scope: match[2],
279-
summary: match[4],
280-
breaking: match[3] === "!",
281-
};
276+
const [, type = "other", scope, breaking, summary = subject] = match;
277+
return { type, scope, summary, breaking: breaking === "!" };
282278
}
283279

284280
function extractPrNumber(subject: string) {
@@ -444,7 +440,7 @@ function capitalize(value: string) {
444440
if (!value) {
445441
return value;
446442
}
447-
return value[0].toUpperCase() + value.slice(1);
443+
return value.charAt(0).toUpperCase() + value.slice(1);
448444
}
449445

450446
function renderTagsLiteral(tags: string[]) {

‎scripts/generate-catalog-pages.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ function textureGroupsFor(manifest: RegistryItem): TextureGroup[] {
234234
if (!group || typeof group !== "object") return false;
235235
if (!("title" in group) || typeof group.title !== "string") return false;
236236
if (!("items" in group) || !Array.isArray(group.items)) return false;
237-
return group.items.every((item) => typeof item === "string");
237+
return group.items.every((item: unknown) => typeof item === "string");
238238
});
239239
}
240240

@@ -403,16 +403,16 @@ function generateTextureAnimationExample(
403403
function generateTexturePreview(manifest: RegistryItem, textureGroups: TextureGroup[]): string[] {
404404
const sampleItems = textureGroups
405405
.map((group) => group.items[0])
406-
.filter(Boolean)
406+
.filter((item): item is string => Boolean(item))
407407
.slice(0, 6);
408408
const lines: string[] = ['<div className="hf-texture-preview-panel">'];
409409

410410
for (const item of sampleItems) {
411411
const maskPath = textureMaskUrlFor(manifest, item);
412412
lines.push(
413413
` <div className="hf-texture-preview-card" style={{ "--mask-url": "url('${maskPath}')" }}>`,
414-
` <div className="hf-texture-preview-label">${textureLabel(item!)}</div>`,
415-
` <div className="hf-texture-preview-shadow"><div className="hf-texture-preview-word">${textureSampleWord(item!)}</div></div>`,
414+
` <div className="hf-texture-preview-label">${textureLabel(item)}</div>`,
415+
` <div className="hf-texture-preview-shadow"><div className="hf-texture-preview-word">${textureSampleWord(item)}</div></div>`,
416416
" </div>",
417417
);
418418
}

‎scripts/generate-catalog-previews.ts‎

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,14 @@ import { tmpdir } from "node:os";
3535
import { fileURLToPath } from "node:url";
3636
// Import from source — bun workspace linking doesn't resolve for scripts outside packages/.
3737
import {
38-
createFileServer,
39-
createCaptureSession,
40-
initializeSession,
4138
captureFrame,
42-
getCompositionDuration,
4339
closeCaptureSession,
4440
createRenderJob,
4541
executeRenderJob,
4642
} from "../packages/producer/src/index.js";
4743
import { compileForRender } from "../packages/producer/src/services/htmlCompiler.js";
4844
import { resolveContainedCopies } from "./registry-target-paths.mjs";
45+
import { openOpaqueCapture } from "./preview-capture.js";
4946

5047
const scriptDir = dirname(fileURLToPath(import.meta.url));
5148
const repoRoot = resolve(scriptDir, "..");
@@ -277,52 +274,25 @@ async function prepareProjectDir(item: CatalogItem): Promise<string> {
277274
return tmpDir;
278275
}
279276

277+
/** Pull a `data-<attr>` pixel value out of the wrapper markup, or fall back. */
278+
function wrapperDimension(html: string, attr: "width" | "height", fallback: number): number {
279+
const match = html.match(new RegExp(`data-${attr}="(\\d+)"`))?.[1];
280+
return match ? parseInt(match, 10) : fallback;
281+
}
282+
280283
async function generateThumbnail(item: CatalogItem, projectDir: string): Promise<void> {
281284
const outDir = outputDir(item.kind);
282285
mkdirSync(outDir, { recursive: true });
283286

284287
// Read dimensions from the wrapper index.html (which may differ from native
285288
// dimensions for portrait overlays that are scaled to fit landscape).
286-
let width = 1920;
287-
let height = 1080;
288-
const wrapperPath = join(projectDir, "index.html");
289-
const wrapperHtml = readFileSync(wrapperPath, "utf-8");
290-
const wMatch = wrapperHtml.match(/data-width="(\d+)"/);
291-
const hMatch = wrapperHtml.match(/data-height="(\d+)"/);
292-
if (wMatch) width = parseInt(wMatch[1], 10);
293-
if (hMatch) height = parseInt(hMatch[1], 10);
289+
const wrapperHtml = readFileSync(join(projectDir, "index.html"), "utf-8");
290+
const width = wrapperDimension(wrapperHtml, "width", 1920);
291+
const height = wrapperDimension(wrapperHtml, "height", 1080);
294292

295293
const framesDir = join(projectDir, "_thumb_frames");
296-
mkdirSync(framesDir, { recursive: true });
297-
298-
const fileServer = await createFileServer({
299-
projectDir,
300-
port: 0,
301-
fps: { num: 30, den: 1 },
302-
});
294+
const { fileServer, session, duration } = await openOpaqueCapture({ projectDir, width, height });
303295
try {
304-
// `format: "png"` is the engine's TRANSPARENT capture mode: it forces
305-
// `background-image: none !important` on every `[data-composition-id]`, so
306-
// any block whose scene paints its own backdrop (the VS Code snippets sit
307-
// on a desktop wallpaper) loses it and the poster comes out empty. These
308-
// posters are opaque page images, never a compositing layer — capture
309-
// opaque and transcode to the .png the catalog pages reference.
310-
const session = await createCaptureSession(fileServer.url, framesDir, {
311-
width,
312-
height,
313-
fps: { num: 30, den: 1 },
314-
format: "jpeg",
315-
quality: 95,
316-
});
317-
await initializeSession(session);
318-
319-
let duration: number;
320-
try {
321-
duration = await getCompositionDuration(session);
322-
} catch {
323-
duration = 5;
324-
}
325-
326296
// Capture after the treatment appears, capped for long compositions.
327297
const captureTime = Math.min(3.0, duration * 0.6);
328298
const result = await captureFrame(session, 0, captureTime);

‎scripts/generate-registry-items.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ import {
2424
type FileType,
2525
type RegistryItem,
2626
type RegistryManifest,
27-
} from "@hyperframes/core";
27+
// Import from source, like every other script here: bun workspace linking
28+
// does not resolve for scripts outside packages/, so the package name
29+
// typechecks on a machine with a warm node_modules and fails in CI.
30+
} from "../packages/core/src/index.js";
2831

2932
const scriptDir = dirname(fileURLToPath(import.meta.url));
3033
const repoRoot = resolve(scriptDir, "..");

0 commit comments

Comments
 (0)