-
Notifications
You must be signed in to change notification settings - Fork 8
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Validate filename before renaming to prevent incorrect file operations. The script assumes that if there's exactly one file, it must be 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
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} catch (error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
console.error('Error during post-prerender processing:', error); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
process.exit(1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
renamePrerenderedFiles(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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:
🤖 Prompt for AI Agents