Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions lib/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ 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 });

for (const entry of entries) {
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 });
}
}
}
Expand All @@ -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);
Expand Down
21 changes: 21 additions & 0 deletions test/bundle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});