Skip to content

Commit a251b80

Browse files
claude: only copy files explicitly referenced in _brand.yml
`quarto use brand` now only copies files that are explicitly referenced in the brand YAML file, rather than copying all files in the source directory. Referenced files include: - The brand file itself (_brand.yml) - Logo paths: logo.small, logo.medium, logo.large (string or light/dark) - Named logo images: logo.images.<name> (string or {path, alt}) - Font files: typography.fonts[*].files[*] when source: file Updates test fixtures to use various logo/font reference patterns: - String form: logo.small: logo.png - Object with alt: logo.images.main: {path: ..., alt: ...} - Light/dark variants: logo.small: {light: ..., dark: ...} - Font string: typography.fonts[*].files: ["path"] - Font object: typography.fonts[*].files: [{path, weight, style}] Adds unreferenced files to fixtures to verify they are NOT copied: - README.md, unused-styles.css, notes.txt, template.html - fonts/unused-italic.woff2, images/extra-icon.png Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 601e3a8 commit a251b80

15 files changed

Lines changed: 256 additions & 11 deletions

File tree

‎src/command/use/commands/brand.ts‎

Lines changed: 127 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import { TempContext } from "../../../core/temp-types.ts";
1616
import { downloadWithProgress } from "../../../core/download.ts";
1717
import { withSpinner } from "../../../core/console.ts";
1818
import { unzip } from "../../../core/zip.ts";
19-
import { templateFiles } from "../../../extension/template.ts";
2019
import { Command } from "cliffy/command/mod.ts";
2120
import { initYamlIntelligenceResourcesFromFilesystem } from "../../../core/schema/utils.ts";
2221
import { createTempContext } from "../../../core/temp.ts";
@@ -110,6 +109,108 @@ function findBrandExtension(stagedDir: string): BrandExtensionInfo {
110109
return { isBrandExtension: false };
111110
}
112111

112+
// Extract a path string from various formats:
113+
// - string: "path/to/file"
114+
// - object with path: { path: "path/to/file", alt: "..." }
115+
function extractPath(value: unknown): string | undefined {
116+
if (typeof value === "string") {
117+
return value;
118+
}
119+
if (value && typeof value === "object" && "path" in value) {
120+
const pathValue = (value as Record<string, unknown>).path;
121+
if (typeof pathValue === "string") {
122+
return pathValue;
123+
}
124+
}
125+
return undefined;
126+
}
127+
128+
// Check if a path is a local file (not a URL)
129+
function isLocalPath(path: string): boolean {
130+
return !path.startsWith("http://") && !path.startsWith("https://");
131+
}
132+
133+
// Extract all referenced file paths from a brand YAML file
134+
function extractBrandFilePaths(brandYamlPath: string): string[] {
135+
const paths: string[] = [];
136+
137+
try {
138+
const yaml = readYaml(brandYamlPath) as Metadata;
139+
if (!yaml) return paths;
140+
141+
// Extract logo paths
142+
const logo = yaml.logo as Metadata | undefined;
143+
if (logo) {
144+
// Handle logo.images (named resources)
145+
// Format: logo.images.<name> can be string or { path, alt }
146+
const images = logo.images as Metadata | undefined;
147+
if (images && typeof images === "object") {
148+
for (const value of Object.values(images)) {
149+
const path = extractPath(value);
150+
if (path && isLocalPath(path)) {
151+
paths.push(path);
152+
}
153+
}
154+
}
155+
156+
// Handle logo.small, logo.medium, logo.large
157+
// Format: string or { light: string, dark: string }
158+
for (const size of ["small", "medium", "large"]) {
159+
const sizeValue = logo[size];
160+
if (!sizeValue) continue;
161+
162+
if (typeof sizeValue === "string") {
163+
if (isLocalPath(sizeValue)) {
164+
paths.push(sizeValue);
165+
}
166+
} else if (typeof sizeValue === "object") {
167+
// Handle { light: "...", dark: "..." }
168+
const lightDark = sizeValue as Record<string, unknown>;
169+
if (
170+
typeof lightDark.light === "string" && isLocalPath(lightDark.light)
171+
) {
172+
paths.push(lightDark.light);
173+
}
174+
if (
175+
typeof lightDark.dark === "string" && isLocalPath(lightDark.dark)
176+
) {
177+
paths.push(lightDark.dark);
178+
}
179+
}
180+
}
181+
}
182+
183+
// Extract typography font file paths
184+
const typography = yaml.typography as Metadata | undefined;
185+
if (typography) {
186+
const fonts = typography.fonts as unknown[] | undefined;
187+
if (Array.isArray(fonts)) {
188+
for (const font of fonts) {
189+
if (!font || typeof font !== "object") continue;
190+
const fontObj = font as Record<string, unknown>;
191+
192+
// Only process fonts with source: "file"
193+
if (fontObj.source !== "file") continue;
194+
195+
const files = fontObj.files as unknown[] | undefined;
196+
if (Array.isArray(files)) {
197+
for (const file of files) {
198+
const path = extractPath(file);
199+
if (path && isLocalPath(path)) {
200+
paths.push(path);
201+
}
202+
}
203+
}
204+
}
205+
}
206+
}
207+
} catch {
208+
// If we can't read/parse the brand file, return empty list
209+
}
210+
211+
return paths;
212+
}
213+
113214
export const useBrandCommand = new Command()
114215
.name("brand")
115216
.arguments("<target:string>")
@@ -191,15 +292,32 @@ async function useBrand(
191292
? brandExtInfo.extensionDir!
192293
: stagedDir;
193294

194-
// Filter the list to template files
195-
let filesToCopy = templateFiles(sourceDir);
295+
// Find the brand file
296+
const brandFileName = brandExtInfo.isBrandExtension
297+
? brandExtInfo.brandFileName!
298+
: existsSync(join(sourceDir, "_brand.yml"))
299+
? "_brand.yml"
300+
: existsSync(join(sourceDir, "_brand.yaml"))
301+
? "_brand.yaml"
302+
: undefined;
303+
304+
if (!brandFileName) {
305+
info("No brand file (_brand.yml or _brand.yaml) found in source");
306+
return;
307+
}
196308

197-
// For brand extensions, exclude _extension.yml/_extension.yaml
198-
if (brandExtInfo.isBrandExtension) {
199-
filesToCopy = filesToCopy.filter((f) => {
200-
const name = basename(f);
201-
return name !== "_extension.yml" && name !== "_extension.yaml";
202-
});
309+
const brandFilePath = join(sourceDir, brandFileName);
310+
311+
// Extract referenced file paths from the brand YAML
312+
const referencedPaths = extractBrandFilePaths(brandFilePath);
313+
314+
// Build list of files to copy: brand file + referenced files
315+
const filesToCopy: string[] = [brandFilePath];
316+
for (const refPath of referencedPaths) {
317+
const fullPath = join(sourceDir, refPath);
318+
if (existsSync(fullPath)) {
319+
filesToCopy.push(fullPath);
320+
}
203321
}
204322

205323
// Confirm changes to brand directory (skip for dry-run or force)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Basic Brand
2+
3+
This README should not be copied.

‎tests/smoke/use-brand/basic-brand/_brand.yml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,11 @@ meta:
22
name: Basic Test Brand
33
color:
44
primary: "#007bff"
5+
logo:
6+
small: logo.png
7+
typography:
8+
fonts:
9+
- source: file
10+
family: "Custom Font"
11+
files:
12+
- fonts/custom-font.woff2
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DUMMY WOFF2 FONT

‎tests/smoke/use-brand/brand-extension/_extensions/test-org/test-brand/brand.yml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@ meta:
33
color:
44
primary: "#007bff"
55
secondary: "#6c757d"
6+
logo:
7+
images:
8+
brand:
9+
path: logo.png
10+
alt: "Brand extension logo"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Template file that should not be copied

‎tests/smoke/use-brand/multi-file-brand/_brand.yml‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,22 @@ meta:
22
name: Multi-file Test Brand
33
color:
44
primary: "#28a745"
5+
logo:
6+
images:
7+
main:
8+
path: logo.png
9+
alt: "Main logo"
10+
favicon:
11+
path: favicon.png
12+
alt: "Favicon"
13+
typography:
14+
fonts:
15+
- source: file
16+
family: "Brand Font"
17+
files:
18+
- path: fonts/brand-regular.woff2
19+
weight: 400
20+
style: normal
21+
- path: fonts/brand-bold.woff2
22+
weight: 700
23+
style: normal
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DUMMY BOLD FONT
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DUMMY REGULAR FONT
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DUMMY UNUSED FONT

0 commit comments

Comments
 (0)