diff --git a/lib/bundle.js b/lib/bundle.js index e6bb2d6..bfd84b8 100644 --- a/lib/bundle.js +++ b/lib/bundle.js @@ -9,7 +9,7 @@ function toArrayBuffer(buffer) { return buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength); } -async function findFontFiles(dir) { +async function findFontFiles(dir, baseDir = dir) { const fontFiles = []; const entries = await fs.readdir(dir, { withFileTypes: true }); @@ -17,12 +17,14 @@ async function findFontFiles(dir) { const fullPath = path.join(dir, entry.name); if (entry.isDirectory()) { - const subFiles = await findFontFiles(fullPath); + const subFiles = await findFontFiles(fullPath, baseDir); fontFiles.push(...subFiles); } else if (entry.isFile()) { const ext = path.extname(entry.name).toLowerCase(); if (FONT_EXTENSIONS.has(ext)) { - fontFiles.push({ path: fullPath, name: entry.name }); + // Store relative path from base input directory + const relativePath = path.relative(baseDir, fullPath); + fontFiles.push({ path: fullPath, relativePath }); } } } @@ -36,9 +38,9 @@ export async function createBundle(inputDir, outputPath, options = {}) { const fontFiles = await findFontFiles(inputDir); const availableFonts = []; - for (const { path: fontPath, name } of fontFiles) { + for (const { path: fontPath, relativePath } of fontFiles) { const fileBuffer = await fs.readFile(fontPath); - const result = await processFont(toArrayBuffer(fileBuffer), name); + const result = await processFont(toArrayBuffer(fileBuffer), relativePath); if (Array.isArray(result)) { availableFonts.push(...result); diff --git a/test/bundle.test.js b/test/bundle.test.js index 4830e75..c696f8c 100644 --- a/test/bundle.test.js +++ b/test/bundle.test.js @@ -74,4 +74,25 @@ describe('createBundle', () => { expect(result.fontsProcessed).toBe(1); }); + + it('should include subdirectory in filePath for nested fonts', async () => { + // Create nested structure with known subdirectory + const nestedDir = path.join(__dirname, 'output', 'nested-path-test'); + const subDir = path.join(nestedDir, 'my-subfolder'); + await fs.mkdir(subDir, { recursive: true }); + await fs.copyFile( + path.join(fixturesDir, 'Anton-Regular.ttf'), + path.join(subDir, 'Anton-Regular.ttf') + ); + + const nestedOutput = path.join(__dirname, 'output', 'nested-path-fonts.json'); + await createBundle(nestedDir, nestedOutput); + + const content = await fs.readFile(nestedOutput, 'utf-8'); + const json = JSON.parse(content); + + // filePath should include the subdirectory + const font = json.availableFonts[0]; + expect(font.filePath).toBe('my-subfolder/Anton-Regular.ttf'); + }); });