Skip to content

Commit

Permalink
fix: add included read file sync (#509)
Browse files Browse the repository at this point in the history
  • Loading branch information
makamekm authored Sep 17, 2024
1 parent 0a7da00 commit 8534be2
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions src/transform/plugins/includes/collect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {relative} from 'path';
import {bold} from 'chalk';
import {readFileSync} from 'fs';

import {getRelativePath, isFileExists, resolveRelativePath} from '../../utilsFS';

Expand All @@ -23,30 +24,34 @@ function processRecursive(
};

try {
const content = copyFile(includePath, targetDestPath, includeOptions);
const contentProcessed = copyFile(includePath, targetDestPath, includeOptions);

// To reduce file reading we can include the file content into the generated content
if (included && content) {
const includedRelativePath = getRelativePath(includedParentPath, includePath);

// The appendix is the map that protects from multiple include files
if (!appendix.has(includedRelativePath)) {
// Recursive function to include the depth structure
const includeContent = collectRecursive(
content,
{
...options,
path: includePath,
includedParentPath,
},
appendix,
);

// Add to appendix set structure
appendix.set(
includedRelativePath,
`{% included (${includedRelativePath}) %}\n${includeContent}\n{% endincluded %}`,
);
if (included) {
const content = contentProcessed ?? readFileSync(targetDestPath, 'utf8');

if (content) {
const includedRelativePath = getRelativePath(includedParentPath, includePath);

// The appendix is the map that protects from multiple include files
if (!appendix.has(includedRelativePath)) {
// Recursive function to include the depth structure
const includeContent = collectRecursive(
content,
{
...options,
path: includePath,
includedParentPath,
},
appendix,
);

// Add to appendix set structure
appendix.set(
includedRelativePath,
`{% included (${includedRelativePath}) %}\n${includeContent}\n{% endincluded %}`,
);
}
}
}
} catch (e) {
Expand Down

0 comments on commit 8534be2

Please sign in to comment.