Skip to content

Instantly share code, notes, and snippets.

@kitze
Created December 8, 2022 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kitze/f2aa75ced1622611e9efe6a6b5c839c9 to your computer and use it in GitHub Desktop.
Save kitze/f2aa75ced1622611e9efe6a6b5c839c9 to your computer and use it in GitHub Desktop.
if you want to switch to .page syntax for next.js, this script renames all the files in the /pages folder recursively to append .page before the file extension
const fs = require('fs');
const path = require('path');
// Function to rename files in the given directory
const renameFiles = (dir) => {
// Read the contents of the directory
fs.readdir(dir, (err, files) => {
if (err) {
// If there is an error, log it and return
console.error(err);
return;
}
// Loop through the files in the directory
files.forEach((file) => {
// Get the full path of the file
const filePath = path.join(dir, file);
// Check if the file is a directory
if (fs.statSync(filePath).isDirectory()) {
// If it is a directory, recursively call the function to rename the files in that directory
renameFiles(filePath);
} else {
// If it is a file, rename it by appending '.page' to the end of the file name (before the file extension)
// Use a regular expression to match the file extension
const extensionRegex = /\.[0-9a-z]+$/i;
const fileExt = file.match(extensionRegex)[0];
// Construct the new file name by appending '.page' to the end of the file name, before the file extension
const newFileName = `${file.replace(extensionRegex, '')}.page${fileExt}`;
fs.rename(filePath, path.join(dir, newFileName), (renameErr) => {
if (renameErr) {
// If there is an error, log it
console.error(renameErr);
}
});
}
});
});
};
// Call the function to rename the files in the './pages' directory
renameFiles('./pages');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment