|
const fs = require("fs"); |
|
const https = require("https"); |
|
const path = require("path"); |
|
|
|
|
|
const basePath = path.resolve(__dirname); |
|
|
|
|
|
|
|
const FILES_TO_COPY = [ |
|
["node_modules/@fontsource-variable/inter/opsz.css", "static/fonts/common.css"], |
|
["node_modules/@fontsource-variable/inter/opsz-italic.css", "static/fonts/common.css"], |
|
["node_modules/@fontsource/bona-nova/700.css", "static/fonts/common.css"], |
|
]; |
|
|
|
|
|
const DIRECTORIES_TO_COPY = [ |
|
["node_modules/@fontsource-variable/inter/files", "static/fonts/files"], |
|
["node_modules/@fontsource/bona-nova/files", "static/fonts/files"], |
|
]; |
|
|
|
|
|
const processedDestinations = new Set(); |
|
const cssDestinations = new Set(); |
|
const allCopiedFiles = new Set(); |
|
|
|
|
|
FILES_TO_COPY.forEach(([source, dest]) => { |
|
|
|
const sourcePath = path.join(basePath, source); |
|
const destPath = path.join(basePath, dest); |
|
|
|
|
|
if (dest.endsWith(".css")) { |
|
cssDestinations.add(destPath); |
|
} |
|
|
|
|
|
const destDir = path.dirname(destPath); |
|
if (!fs.existsSync(destDir)) { |
|
fs.mkdirSync(destDir, { recursive: true }); |
|
console.log(`Created directory: ${destDir}`); |
|
} |
|
|
|
try { |
|
|
|
const content = fs.readFileSync(sourcePath, "utf8"); |
|
|
|
|
|
if (processedDestinations.has(destPath)) { |
|
|
|
fs.appendFileSync(destPath, "\n\n" + content); |
|
console.log(`Appended: ${sourcePath} β ${destPath}`); |
|
} else { |
|
|
|
fs.writeFileSync(destPath, content); |
|
processedDestinations.add(destPath); |
|
console.log(`Copied: ${sourcePath} β ${destPath}`); |
|
} |
|
|
|
|
|
let destFileContent = fs.readFileSync(destPath, "utf8"); |
|
destFileContent = destFileContent.replaceAll("./files/", "/fonts/files/"); |
|
fs.writeFileSync(destPath, destFileContent); |
|
} catch (error) { |
|
console.error(`Error processing ${sourcePath} to ${destPath}:`, error); |
|
process.exit(1); |
|
} |
|
}); |
|
|
|
|
|
function copyDirectoryRecursive(source, destination) { |
|
|
|
if (!fs.existsSync(destination)) { |
|
fs.mkdirSync(destination, { recursive: true }); |
|
console.log(`Created directory: ${destination}`); |
|
} |
|
|
|
|
|
const items = fs.readdirSync(source); |
|
|
|
|
|
items.forEach((item) => { |
|
const sourcePath = path.join(source, item); |
|
const destPath = path.join(destination, item); |
|
|
|
|
|
const stats = fs.statSync(sourcePath); |
|
if (stats.isDirectory()) { |
|
|
|
copyDirectoryRecursive(sourcePath, destPath); |
|
} else { |
|
|
|
fs.copyFileSync(sourcePath, destPath); |
|
allCopiedFiles.add(destPath); |
|
console.log(`Copied: ${sourcePath} β ${destPath}`); |
|
} |
|
}); |
|
} |
|
|
|
|
|
DIRECTORIES_TO_COPY.forEach(([source, dest]) => { |
|
|
|
const sourcePath = path.join(basePath, source); |
|
const destPath = path.join(basePath, dest); |
|
|
|
try { |
|
copyDirectoryRecursive(sourcePath, destPath); |
|
console.log(`Copied directory: ${sourcePath} β ${destPath}`); |
|
} catch (error) { |
|
console.error(`Error copying directory ${sourcePath} to ${destPath}:`, error); |
|
process.exit(1); |
|
} |
|
}); |
|
|
|
console.log("All files and directories copied successfully!"); |
|
|
|
|
|
console.log("\nChecking for unused font files..."); |
|
|
|
|
|
let allCssContent = ""; |
|
cssDestinations.forEach((cssPath) => { |
|
try { |
|
const content = fs.readFileSync(cssPath, "utf8"); |
|
allCssContent += content; |
|
} catch (error) { |
|
console.error(`Error reading CSS file ${cssPath}:`, error); |
|
} |
|
}); |
|
|
|
|
|
const unusedFiles = []; |
|
allCopiedFiles.forEach((filePath) => { |
|
const fileName = path.basename(filePath); |
|
|
|
|
|
if (!allCssContent.includes(fileName)) { |
|
unusedFiles.push(filePath); |
|
} |
|
}); |
|
|
|
|
|
if (unusedFiles.length > 0) { |
|
console.log(`Found ${unusedFiles.length} unused font files to delete:`); |
|
unusedFiles.forEach((filePath) => { |
|
try { |
|
fs.unlinkSync(filePath); |
|
console.log(`Deleted unused file: ${filePath}`); |
|
} catch (error) { |
|
console.error(`Error deleting file ${filePath}:`, error); |
|
} |
|
}); |
|
} else { |
|
console.log("No unused font files found."); |
|
} |
|
|
|
console.log("\nFont installation complete!"); |
|
|
|
|
|
const textBalancerUrl = "https://static.graphite.rs/text-balancer/text-balancer.js"; |
|
const textBalancerDest = path.join(basePath, "static", "text-balancer.js"); |
|
console.log("\nDownloading text-balancer.js..."); |
|
https |
|
.get(textBalancerUrl, (res) => { |
|
if (res.statusCode !== 200) { |
|
console.error(`Failed to download text-balancer.js. Status code: ${res.statusCode}`); |
|
res.resume(); |
|
return; |
|
} |
|
|
|
let data = ""; |
|
res.on("data", (chunk) => { |
|
data += chunk; |
|
}); |
|
|
|
res.on("end", () => { |
|
try { |
|
|
|
const destDir = path.dirname(textBalancerDest); |
|
if (!fs.existsSync(destDir)) { |
|
fs.mkdirSync(destDir, { recursive: true }); |
|
console.log(`Created directory: ${destDir}`); |
|
} |
|
fs.writeFileSync(textBalancerDest, data, "utf8"); |
|
console.log(`Downloaded and saved: ${textBalancerDest}`); |
|
} catch (error) { |
|
console.error(`Error saving text-balancer.js:`, error); |
|
} |
|
}); |
|
}) |
|
.on("error", (err) => { |
|
console.error(`Error downloading text-balancer.js:`, err); |
|
}); |
|
|