Skip to content

Commit

Permalink
fix(sources content): add source content after final formatting
Browse files Browse the repository at this point in the history
- Move source content reading to after all processing and formatting
- Use a Set to track processed files
- Adjust main execution flow to generate source map as final step
  • Loading branch information
phukon committed Sep 17, 2024
1 parent d4bedb5 commit f965012
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions main/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ class FileProcessor {
const map = new SourceMapGenerator({ file: readmeFilePath });
let line = 1;
let fileOffsets = {}; // to store offsets for each file
let processedFiles = new Set();

collectedComments.forEach((comment) => {
const location = commentLocations.find(
Expand Down Expand Up @@ -328,14 +329,17 @@ class FileProcessor {
});
line += lines.length + 1; // +1 for the blank line between comments

// Update the file-specific offset
fileOffsets[location.filePath] = offset;

const sourceContent = fs.readFileSync(location.filePath, 'utf-8');
map.setSourceContent(location.filePath, sourceContent);
processedFiles.add(location.filePath);
}
});

processedFiles.forEach((filePath) => {
const sourceContent = fs.readFileSync(filePath, 'utf-8');
map.setSourceContent(filePath, sourceContent);
});

fs.writeFileSync(sourceMapFilePath, map.toString());
console.log(`Source map written to ${sourceMapFilePath}`);
}
Expand All @@ -348,8 +352,9 @@ class FileProcessor {
const fileProcessor = new FileProcessor(sourceCodeDir, sourceCodeDir);
fileProcessor.traverseDirectory(sourceCodeDir);
fileProcessor.writeCollectedComments(readmeFilePath);
fileProcessor.generateSourceMap(sourceMapFilePath);

console.log('Running Prettier Formatter After Codemod...');
await formatDirectory(sourceCodeDir);

fileProcessor.generateSourceMap(sourceMapFilePath);
})();

0 comments on commit f965012

Please sign in to comment.