Skip to content

feat: try change output structure #455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions apps/blog/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@
"options": {
"command": "node apps/blog/scripts/build-routes.mjs"
}
},
"post-prerender": {
"executor": "nx:run-commands",
"options": {
"command": "node apps/blog/scripts/post-prerender.mjs"
}
}
}
}
48 changes: 48 additions & 0 deletions apps/blog/scripts/post-prerender.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { readdir, rename } from 'node:fs/promises';
import { join } from 'node:path';

const DIST_DIR = 'dist/apps/blog';
const SKIP_FOLDERS = ['assets', 'images', 'styles', 'scripts'];

async function renamePrerenderedFiles(dirPath = DIST_DIR) {
try {
const entries = await readdir(dirPath, { withFileTypes: true });

for (const entry of entries) {
if (entry.isDirectory()) {
const currentDirPath = join(dirPath, entry.name);

if (SKIP_FOLDERS.includes(entry.name)) {
continue;
}

const files = await readdir(currentDirPath);
if (files.length === 0) {
continue;
}

if (files.length > 1) {
console.error(`Multiple files in ${currentDirPath}:`, files);
await renamePrerenderedFiles(currentDirPath);
continue;
}
Comment on lines +24 to +28
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix inconsistent logic for directories with multiple files.

When multiple files are found in a directory, the script logs an error but then recurses into the same directory. This creates inconsistent behavior - if multiple files indicate an error condition, the directory should be skipped rather than processed recursively.

Consider this approach:

-        if (files.length > 1) {
-          console.error(`Multiple files in ${currentDirPath}:`, files);
-          await renamePrerenderedFiles(currentDirPath);
-          continue;
-        }
+        if (files.length > 1) {
+          console.warn(`Skipping ${currentDirPath} - contains multiple files:`, files);
+          // Only recurse if this directory might contain subdirectories to process
+          await renamePrerenderedFiles(currentDirPath);
+          continue;
+        }

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In apps/blog/scripts/post-prerender.mjs around lines 24 to 28, the code logs an
error when multiple files are found in a directory but then continues processing
that directory recursively, causing inconsistent behavior. Modify the logic so
that when multiple files are detected, the directory is skipped entirely instead
of recursing into it. Remove or adjust the call to renamePrerenderedFiles and
the continue statement to prevent further processing of that directory.


const indexPath = join(currentDirPath, 'index.html');

try {
await rename(indexPath, join(currentDirPath, `${entry.name}.html`));
console.log(`Renamed ${indexPath} to ${entry.name}.html`);
} catch (error) {
if (error.code !== 'ENOENT') {
console.error(`Error processing ${currentDirPath}:`, error);
}
}
Comment on lines +30 to +39
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Validate filename before renaming to prevent incorrect file operations.

The script assumes that if there's exactly one file, it must be index.html, but this isn't validated. This could lead to renaming wrong files.

Add filename validation:

-        const indexPath = join(currentDirPath, 'index.html');
-
-        try {
-          await rename(indexPath, join(currentDirPath, `${entry.name}.html`));
-          console.log(`Renamed ${indexPath} to ${entry.name}.html`);
-        } catch (error) {
-          if (error.code !== 'ENOENT') {
-            console.error(`Error processing ${currentDirPath}:`, error);
-          }
-        }
+        const singleFile = files[0];
+        if (singleFile !== 'index.html') {
+          console.warn(`Skipping ${currentDirPath} - single file is not index.html: ${singleFile}`);
+          continue;
+        }
+
+        const indexPath = join(currentDirPath, 'index.html');
+        const newPath = join(currentDirPath, `${entry.name}.html`);
+        
+        try {
+          await rename(indexPath, newPath);
+          console.log(`Renamed ${indexPath} to ${entry.name}.html`);
+        } catch (error) {
+          console.error(`Error renaming ${indexPath}:`, error);
+        }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const indexPath = join(currentDirPath, 'index.html');
try {
await rename(indexPath, join(currentDirPath, `${entry.name}.html`));
console.log(`Renamed ${indexPath} to ${entry.name}.html`);
} catch (error) {
if (error.code !== 'ENOENT') {
console.error(`Error processing ${currentDirPath}:`, error);
}
}
const singleFile = files[0];
if (singleFile !== 'index.html') {
console.warn(`Skipping ${currentDirPath} - single file is not index.html: ${singleFile}`);
continue;
}
const indexPath = join(currentDirPath, 'index.html');
const newPath = join(currentDirPath, `${entry.name}.html`);
try {
await rename(indexPath, newPath);
console.log(`Renamed ${indexPath} to ${entry.name}.html`);
} catch (error) {
console.error(`Error renaming ${indexPath}:`, error);
}
🤖 Prompt for AI Agents
In apps/blog/scripts/post-prerender.mjs around lines 30 to 39, the code renames
a file assuming it is named index.html without validating the filename. To fix
this, add a check to confirm that the file to be renamed is actually named
index.html before attempting the rename operation. If the filename does not
match, skip the rename or handle the case appropriately to avoid incorrect file
operations.

}
}
} catch (error) {
console.error('Error during post-prerender processing:', error);
process.exit(1);
}
}

renamePrerenderedFiles();
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"prepare": "husky install",
"hooks:debugger-checker": "node hooks/debugger-checker.js",
"lint-staged": "lint-staged",
"prerender": "nx run blog:build-routes && nx run blog:build",
"prerender": "nx run blog:build-routes && nx run blog:build && nx run blog:post-prerender",
"e2e": "npx nx run blog-e2e:e2e",
"e2e:ui": "npx nx run blog-e2e:e2e-ui"
},
Expand Down