Create fix-metadata.js
This commit is contained in:
116
scripts/fix-metadata.js
Normal file
116
scripts/fix-metadata.js
Normal file
@@ -0,0 +1,116 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const glob = require('glob');
|
||||
|
||||
// Define default viewport and themeColor values
|
||||
const defaultViewport = {
|
||||
width: "device-width",
|
||||
initialScale: 1,
|
||||
maximumScale: 1,
|
||||
};
|
||||
|
||||
const defaultThemeColor = [
|
||||
{ media: "(prefers-color-scheme: dark)", color: "#000000" },
|
||||
{ media: "(prefers-color-scheme: light)", color: "#D53F8C" },
|
||||
];
|
||||
|
||||
// List of files with metadata issues based on build log warnings
|
||||
const affectedPaths = [
|
||||
'app/dashboard/admin/page.tsx',
|
||||
'app/dashboard/page.tsx',
|
||||
'app/dashboard/products/page.tsx',
|
||||
'app/dashboard/stock/page.tsx',
|
||||
'app/_not-found.tsx',
|
||||
'app/dashboard/orders/page.tsx',
|
||||
'app/dashboard/shipping/page.tsx',
|
||||
'app/dashboard/storefront/page.tsx',
|
||||
'app/dashboard/categories/page.tsx',
|
||||
'app/dashboard/chats/page.tsx',
|
||||
'app/dashboard/promotions/page.tsx',
|
||||
'app/page.tsx',
|
||||
'app/auth/login/page.tsx',
|
||||
'app/dashboard/storefront/customers/page.tsx',
|
||||
'app/dashboard/chats/new/page.tsx',
|
||||
'app/auth/register/page.tsx',
|
||||
];
|
||||
|
||||
// Function to check if a file exists
|
||||
function fileExists(filePath) {
|
||||
try {
|
||||
fs.accessSync(filePath, fs.constants.F_OK);
|
||||
return true;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Function to transform file content
|
||||
function fixMetadataFile(filePath) {
|
||||
if (!fileExists(filePath)) {
|
||||
console.log(`File not found: ${filePath}`);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
console.log(`Processing: ${filePath}`);
|
||||
let content = fs.readFileSync(filePath, 'utf8');
|
||||
|
||||
// Check if the file has metadata but no viewport
|
||||
const hasMetadata = content.includes('export const metadata');
|
||||
const hasViewport = content.includes('export const viewport');
|
||||
|
||||
if (hasMetadata && !hasViewport) {
|
||||
// Ensure we import Viewport type
|
||||
if (content.includes('import { Metadata }')) {
|
||||
content = content.replace('import { Metadata }', 'import { Metadata, Viewport }');
|
||||
} else if (!content.includes('import { Viewport }') && !content.includes('Viewport')) {
|
||||
// Add import if not already present
|
||||
if (content.includes('import ')) {
|
||||
// Find the last import statement
|
||||
const importRegex = /^import.*$/gm;
|
||||
const imports = [...content.matchAll(importRegex)];
|
||||
|
||||
if (imports.length > 0) {
|
||||
const lastImport = imports[imports.length - 1][0];
|
||||
content = content.replace(lastImport, lastImport + '\nimport { Viewport } from "next";');
|
||||
} else {
|
||||
content = 'import { Viewport } from "next";\n' + content;
|
||||
}
|
||||
} else {
|
||||
content = 'import { Viewport } from "next";\n' + content;
|
||||
}
|
||||
}
|
||||
|
||||
// Add viewport export
|
||||
const viewportExport = `
|
||||
export const viewport: Viewport = {
|
||||
width: "device-width",
|
||||
initialScale: 1,
|
||||
themeColor: [
|
||||
{ media: "(prefers-color-scheme: dark)", color: "#000000" },
|
||||
{ media: "(prefers-color-scheme: light)", color: "#D53F8C" },
|
||||
],
|
||||
};`;
|
||||
|
||||
// Insert viewport export after metadata
|
||||
const metadataRegex = /export const metadata[^;]*;/s;
|
||||
content = content.replace(metadataRegex, match => `${match}\n${viewportExport}`);
|
||||
|
||||
// Write the changes back to the file
|
||||
fs.writeFileSync(filePath, content, 'utf8');
|
||||
console.log(`Fixed: ${filePath}`);
|
||||
} else {
|
||||
console.log(`No changes needed or already has viewport: ${filePath}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error processing ${filePath}:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
// Process each affected file
|
||||
affectedPaths.forEach(relativePath => {
|
||||
const fullPath = path.join(process.cwd(), relativePath);
|
||||
fixMetadataFile(fullPath);
|
||||
});
|
||||
|
||||
console.log('Metadata fix script completed!');
|
||||
Reference in New Issue
Block a user