@@ -24,9 +24,92 @@ import { InternalError } from "../../../core/lib/error.ts";
2424import { notebookContext } from "../../../render/notebook/notebook-context.ts" ;
2525import { projectContext } from "../../../project/project-context.ts" ;
2626import { afterConfirm } from "../../../tools/tools-console.ts" ;
27+ import { readYaml } from "../../../core/yaml.ts" ;
28+ import { Metadata } from "../../../config/types.ts" ;
2729
2830const kRootTemplateName = "template.qmd" ;
2931
32+ // Brand extension detection result
33+ interface BrandExtensionInfo {
34+ isBrandExtension : boolean ;
35+ extensionDir ?: string ; // Directory containing the brand extension
36+ brandFileName ?: string ; // The original brand file name (e.g., "brand.yml")
37+ }
38+
39+ // Check if a directory contains a brand extension
40+ function checkForBrandExtension ( dir : string ) : BrandExtensionInfo {
41+ const extensionFiles = [ "_extension.yml" , "_extension.yaml" ] ;
42+
43+ for ( const file of extensionFiles ) {
44+ const path = join ( dir , file ) ;
45+ if ( existsSync ( path ) ) {
46+ try {
47+ const yaml = readYaml ( path ) as Metadata ;
48+ // Check for contributes.metadata.project.brand
49+ const contributes = yaml ?. contributes as Metadata | undefined ;
50+ const metadata = contributes ?. metadata as Metadata | undefined ;
51+ const project = metadata ?. project as Metadata | undefined ;
52+ const brandFile = project ?. brand as string | undefined ;
53+
54+ if ( brandFile && typeof brandFile === "string" ) {
55+ return {
56+ isBrandExtension : true ,
57+ extensionDir : dir ,
58+ brandFileName : brandFile ,
59+ } ;
60+ }
61+ } catch {
62+ // If we can't read/parse the extension file, continue searching
63+ }
64+ }
65+ }
66+
67+ return { isBrandExtension : false } ;
68+ }
69+
70+ // Search for a brand extension in the staged directory
71+ // Searches: root, _extensions/*, _extensions/*/*
72+ function findBrandExtension ( stagedDir : string ) : BrandExtensionInfo {
73+ // First check the root directory
74+ const rootCheck = checkForBrandExtension ( stagedDir ) ;
75+ if ( rootCheck . isBrandExtension ) {
76+ return rootCheck ;
77+ }
78+
79+ // Check _extensions directory
80+ const extensionsDir = join ( stagedDir , "_extensions" ) ;
81+ if ( ! existsSync ( extensionsDir ) ) {
82+ return { isBrandExtension : false } ;
83+ }
84+
85+ try {
86+ // Check direct children: _extensions/extension-name/
87+ for ( const entry of Deno . readDirSync ( extensionsDir ) ) {
88+ if ( ! entry . isDirectory ) continue ;
89+
90+ const extPath = join ( extensionsDir , entry . name ) ;
91+ const check = checkForBrandExtension ( extPath ) ;
92+ if ( check . isBrandExtension ) {
93+ return check ;
94+ }
95+
96+ // Check nested: _extensions/org/extension-name/
97+ for ( const nested of Deno . readDirSync ( extPath ) ) {
98+ if ( ! nested . isDirectory ) continue ;
99+ const nestedPath = join ( extPath , nested . name ) ;
100+ const nestedCheck = checkForBrandExtension ( nestedPath ) ;
101+ if ( nestedCheck . isBrandExtension ) {
102+ return nestedCheck ;
103+ }
104+ }
105+ }
106+ } catch {
107+ // Directory read error, return not found
108+ }
109+
110+ return { isBrandExtension : false } ;
111+ }
112+
30113export const useBrandCommand = new Command ( )
31114 . name ( "brand" )
32115 . arguments ( "<target:string>" )
@@ -100,8 +183,24 @@ async function useBrand(
100183 // Extract and move the template into place
101184 const stagedDir = await stageBrand ( source , tempContext ) ;
102185
186+ // Check if this is a brand extension
187+ const brandExtInfo = findBrandExtension ( stagedDir ) ;
188+
189+ // Determine the actual source directory and file mapping
190+ const sourceDir = brandExtInfo . isBrandExtension
191+ ? brandExtInfo . extensionDir !
192+ : stagedDir ;
193+
103194 // Filter the list to template files
104- const filesToCopy = templateFiles ( stagedDir ) ;
195+ let filesToCopy = templateFiles ( sourceDir ) ;
196+
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+ } ) ;
203+ }
105204
106205 // Confirm changes to brand directory (skip for dry-run or force)
107206 if ( ! options . dryRun && ! options . force ) {
@@ -125,10 +224,20 @@ async function useBrand(
125224 }
126225
127226 // Build set of source file paths for comparison
227+ // For brand extensions, we need to account for the brand file rename
128228 const sourceFiles = new Set (
129229 filesToCopy
130230 . filter ( ( f ) => ! Deno . statSync ( f ) . isDirectory )
131- . map ( ( f ) => relative ( stagedDir , f ) ) ,
231+ . map ( ( f ) => {
232+ const rel = relative ( sourceDir , f ) ;
233+ // If this is a brand extension and this is the brand file, it will become _brand.yml
234+ if (
235+ brandExtInfo . isBrandExtension && rel === brandExtInfo . brandFileName
236+ ) {
237+ return "_brand.yml" ;
238+ }
239+ return rel ;
240+ } ) ,
132241 ) ;
133242
134243 // Find extra files in target that aren't in source
@@ -147,13 +256,20 @@ async function useBrand(
147256
148257 for ( const fileToCopy of filesToCopy ) {
149258 const isDir = Deno . statSync ( fileToCopy ) . isDirectory ;
150- const rel = relative ( stagedDir , fileToCopy ) ;
259+ const rel = relative ( sourceDir , fileToCopy ) ;
151260 if ( isDir ) {
152261 continue ;
153262 }
263+
264+ // For brand extensions, rename the brand file to _brand.yml
265+ let targetRel = rel ;
266+ if ( brandExtInfo . isBrandExtension && rel === brandExtInfo . brandFileName ) {
267+ targetRel = "_brand.yml" ;
268+ }
269+
154270 // Compute the paths
155- const targetPath = join ( brandDir , rel ) ;
156- const displayName = rel ;
271+ const targetPath = join ( brandDir , targetRel ) ;
272+ const displayName = targetRel ;
157273 const targetDir = dirname ( targetPath ) ;
158274 const copyAction = {
159275 file : displayName ,
0 commit comments