chore: formatting fixes

This commit is contained in:
haritabh-z01 2025-06-28 11:02:07 +05:30
parent dd6af5e879
commit 119a46c339
167 changed files with 4507 additions and 3248 deletions

View file

@ -1,43 +1,49 @@
#!/usr/bin/env node
// Load environment variables from .env files
import * as dotenv from 'dotenv';
import path from 'path';
import { S3Client, ListObjectsV2Command, GetObjectCommand } from '@aws-sdk/client-s3';
import { createWriteStream, promises as fs } from 'fs';
import { pipeline } from 'stream/promises';
import * as dotenv from "dotenv";
import path from "path";
import {
S3Client,
ListObjectsV2Command,
GetObjectCommand,
} from "@aws-sdk/client-s3";
import { createWriteStream, promises as fs } from "fs";
import { pipeline } from "stream/promises";
// Configure dotenv to load environment variables
dotenv.config();
// Parse command line arguments
const args = process.argv.slice(2);
const DRY_RUN = args.includes('--dry-run');
const SKIP_ERRORS = args.includes('--skip-errors');
const DRY_RUN = args.includes("--dry-run");
const SKIP_ERRORS = args.includes("--skip-errors");
// Log environment variables for debugging
console.log('AWS Credentials:', {
accessKeyId: process.env.AWS_ACCESS_KEY_ID ? '****' : undefined,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY ? '****' : undefined
console.log("AWS Credentials:", {
accessKeyId: process.env.AWS_ACCESS_KEY_ID ? "****" : undefined,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY ? "****" : undefined,
});
// Configuration
const BUCKET_NAME = process.env.S3_BUCKET_NAME || 'amical-www';
const ENDPOINT = process.env.S3_ENDPOINT || 'https://s3.wasabisys.com';
const BLOG_PREFIX = process.env.BLOG_PREFIX || 'blog/';
const BLOG_IMAGES_PREFIX = process.env.BLOG_IMAGES_PREFIX || 'blog-images/';
const CONTENT_DIR = path.join(process.cwd(), 'content');
const PUBLIC_DIR = path.join(process.cwd(), 'public');
const LOCAL_BLOG_DIR = path.join(CONTENT_DIR, 'blogs');
const LOCAL_BLOG_IMAGES_DIR = path.join(PUBLIC_DIR, 'blog');
const BUCKET_NAME = process.env.S3_BUCKET_NAME || "amical-www";
const ENDPOINT = process.env.S3_ENDPOINT || "https://s3.wasabisys.com";
const BLOG_PREFIX = process.env.BLOG_PREFIX || "blog/";
const BLOG_IMAGES_PREFIX = process.env.BLOG_IMAGES_PREFIX || "blog-images/";
const CONTENT_DIR = path.join(process.cwd(), "content");
const PUBLIC_DIR = path.join(process.cwd(), "public");
const LOCAL_BLOG_DIR = path.join(CONTENT_DIR, "blogs");
const LOCAL_BLOG_IMAGES_DIR = path.join(PUBLIC_DIR, "blog");
// Check if we have AWS credentials
const HAS_AWS_CREDENTIALS = !!(process.env.AWS_ACCESS_KEY_ID && process.env.AWS_SECRET_ACCESS_KEY);
const HAS_AWS_CREDENTIALS = !!(
process.env.AWS_ACCESS_KEY_ID && process.env.AWS_SECRET_ACCESS_KEY
);
// S3 client configuration
const s3ClientConfig: any = {
endpoint: ENDPOINT,
region: process.env.AWS_REGION || 'us-east-1', // Required but may not matter for Wasabi
region: process.env.AWS_REGION || "us-east-1", // Required but may not matter for Wasabi
forcePathStyle: true, // Required for Wasabi
};
@ -48,16 +54,23 @@ if (HAS_AWS_CREDENTIALS) {
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
};
} else if (!DRY_RUN) {
console.warn('Warning: AWS credentials not found. S3 operations will be skipped.');
console.warn(
"Warning: AWS credentials not found. S3 operations will be skipped.",
);
if (SKIP_ERRORS) {
console.warn('Running with --skip-errors flag. Will continue without fetching content.');
console.warn(
"Running with --skip-errors flag. Will continue without fetching content.",
);
} else {
console.error('AWS credentials are required for content fetch. Set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables.');
console.error(
"AWS credentials are required for content fetch. Set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables.",
);
}
}
// Initialize S3 client only if we have credentials or in dry run mode
const s3Client = (HAS_AWS_CREDENTIALS || DRY_RUN) ? new S3Client(s3ClientConfig) : null;
const s3Client =
HAS_AWS_CREDENTIALS || DRY_RUN ? new S3Client(s3ClientConfig) : null;
/**
* Ensures that a directory exists, creating it and all parent directories if necessary
@ -99,7 +112,9 @@ async function listObjects(prefix: string): Promise<string[]> {
// Skip S3 operations if we don't have credentials
if (!s3Client) {
console.log(`Skipping S3 operations for prefix: ${prefix} (no credentials)`);
console.log(
`Skipping S3 operations for prefix: ${prefix} (no credentials)`,
);
return [];
}
@ -134,7 +149,10 @@ async function listObjects(prefix: string): Promise<string[]> {
}
}
async function downloadFile(key: string, destinationPath: string): Promise<void> {
async function downloadFile(
key: string,
destinationPath: string,
): Promise<void> {
if (DRY_RUN) {
console.log(`[DRY RUN] Would download: ${key} to ${destinationPath}`);
return;
@ -171,11 +189,11 @@ async function downloadFile(key: string, destinationPath: string): Promise<void>
}
async function fetchBlogContent(): Promise<void> {
console.log('Starting content fetch from S3...');
console.log("Starting content fetch from S3...");
if (DRY_RUN) {
console.log('*** DRY RUN MODE - No files will be downloaded ***');
console.log("*** DRY RUN MODE - No files will be downloaded ***");
} else if (!HAS_AWS_CREDENTIALS) {
console.log('*** NO CREDENTIALS MODE - S3 operations will be skipped ***');
console.log("*** NO CREDENTIALS MODE - S3 operations will be skipped ***");
}
console.log(`Bucket: ${BUCKET_NAME}`);
console.log(`Endpoint: ${ENDPOINT}`);
@ -191,12 +209,16 @@ async function fetchBlogContent(): Promise<void> {
// Skip S3 operations if we don't have credentials
if (!HAS_AWS_CREDENTIALS && !DRY_RUN) {
console.log('Skipping S3 operations due to missing credentials.');
console.log("Skipping S3 operations due to missing credentials.");
if (SKIP_ERRORS) {
console.log('Build process will continue without fetching content due to --skip-errors flag.');
console.log(
"Build process will continue without fetching content due to --skip-errors flag.",
);
return;
} else {
throw new Error('AWS credentials are required for content fetch. Set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables.');
throw new Error(
"AWS credentials are required for content fetch. Set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables.",
);
}
}
@ -205,7 +227,7 @@ async function fetchBlogContent(): Promise<void> {
const blogFiles = await listObjects(BLOG_PREFIX);
if (blogFiles.length === 0) {
console.log('No blog files found.');
console.log("No blog files found.");
} else {
console.log(`Found ${blogFiles.length} blog files.`);
@ -221,11 +243,15 @@ async function fetchBlogContent(): Promise<void> {
await downloadFile(key, destinationPath);
downloadedCount++;
} catch (error) {
console.error(`Failed to download ${key}. Continuing with next file.`);
console.error(
`Failed to download ${key}. Continuing with next file.`,
);
}
}
console.log(`Successfully downloaded ${downloadedCount} of ${blogFiles.length} blog files.`);
console.log(
`Successfully downloaded ${downloadedCount} of ${blogFiles.length} blog files.`,
);
}
// Fetch blog images
@ -233,7 +259,7 @@ async function fetchBlogContent(): Promise<void> {
const imageFiles = await listObjects(BLOG_IMAGES_PREFIX);
if (imageFiles.length === 0) {
console.log('No image files found.');
console.log("No image files found.");
} else {
console.log(`Found ${imageFiles.length} image files.`);
@ -249,18 +275,24 @@ async function fetchBlogContent(): Promise<void> {
await downloadFile(key, destinationPath);
downloadedCount++;
} catch (error) {
console.error(`Failed to download ${key}. Continuing with next file.`);
console.error(
`Failed to download ${key}. Continuing with next file.`,
);
}
}
console.log(`Successfully downloaded ${downloadedCount} of ${imageFiles.length} image files.`);
console.log(
`Successfully downloaded ${downloadedCount} of ${imageFiles.length} image files.`,
);
}
console.log('\nContent fetch completed successfully!');
console.log("\nContent fetch completed successfully!");
} catch (error) {
console.error('Error fetching content:', error);
console.error("Error fetching content:", error);
if (SKIP_ERRORS) {
console.warn('Continuing build process despite errors due to --skip-errors flag.');
console.warn(
"Continuing build process despite errors due to --skip-errors flag.",
);
} else {
process.exit(1);
}