next-tribune-blog
Version:
Automatic blog generator for Next.js from Tribune.sh blockchain articles
198 lines (182 loc) ⢠9.22 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
async function setup() {
console.log('š Setting up next-tribune-blog...');
// Get the actual project root (where npm install was run from)
// When run as postinstall, we need to go up from node_modules/package-name
let projectRoot = process.cwd();
// Check if we're running from within node_modules
if (projectRoot.includes('node_modules')) {
// Go up to the actual project root (2 levels: package-name -> node_modules -> project)
projectRoot = path_1.default.resolve(projectRoot, '..', '..');
}
console.log('š Project root:', projectRoot);
// Check for all possible config files
const nextConfigPath = path_1.default.join(projectRoot, 'next.config.js');
const nextConfigMjsPath = path_1.default.join(projectRoot, 'next.config.mjs');
const nextConfigTsPath = path_1.default.join(projectRoot, 'next.config.ts');
console.log('š Checking for config files...');
console.log(' - next.config.ts exists?', await fs_extra_1.default.pathExists(nextConfigTsPath));
console.log(' - next.config.js exists?', await fs_extra_1.default.pathExists(nextConfigPath));
console.log(' - next.config.mjs exists?', await fs_extra_1.default.pathExists(nextConfigMjsPath));
let configPath = '';
let isTypeScript = false;
// Priority: .ts > .js > .mjs
if (await fs_extra_1.default.pathExists(nextConfigTsPath)) {
configPath = nextConfigTsPath;
isTypeScript = true;
console.log('ā
Found next.config.ts');
}
else if (await fs_extra_1.default.pathExists(nextConfigPath)) {
configPath = nextConfigPath;
console.log('ā
Found next.config.js');
}
else if (await fs_extra_1.default.pathExists(nextConfigMjsPath)) {
configPath = nextConfigMjsPath;
console.log('ā
Found next.config.mjs');
}
if (configPath) {
console.log('š Reading config file...');
const configContent = await fs_extra_1.default.readFile(configPath, 'utf-8');
console.log('š Config content length:', configContent.length);
// Check if already configured
if (configContent.includes('NextTribuneBlogPlugin')) {
console.log('ā ļø Config already contains NextTribuneBlogPlugin, skipping...');
return;
}
console.log('š§ Modifying config file...');
const isESM = configPath.endsWith('.mjs') || isTypeScript;
// Import statement
const importStatement = isTypeScript || isESM
? `import { NextTribuneBlogPlugin } from 'next-tribune-blog';\n`
: `const { NextTribuneBlogPlugin } = require('next-tribune-blog');\n`;
// Plugin configuration
const pluginSetup = `
// Tribune Blog Configuration
const tribuneBlogPlugin = new NextTribuneBlogPlugin({
// Add your wallet address to show only your articles
walletAddress: '0xYourWalletAddress', // <-- CHANGE THIS TO YOUR WALLET ADDRESS
// OPTIONAL: Change the output directory if needed (default: 'src/app/blog')
// outputDir: 'src/app/blog',
});
`;
let updatedConfig = '';
if (isTypeScript) {
console.log('š§ Processing TypeScript config...');
// For TypeScript files, we need to:
// 1. Add the import at the top
// 2. Add the plugin setup after imports
// 3. Replace the export statement
// Find where to insert the import (after existing imports or at the top)
const importMatch = configContent.match(/^(import[\s\S]*?from\s+['"][^'"]+['"];?\s*\n)+/m);
if (importMatch) {
// Add import after existing imports
updatedConfig = configContent.slice(0, importMatch.index + importMatch[0].length) +
importStatement +
pluginSetup +
configContent.slice(importMatch.index + importMatch[0].length);
}
else {
// Add import at the very beginning
updatedConfig = importStatement + pluginSetup + configContent;
}
// Now replace the export statement
// Handle different export patterns
if (updatedConfig.match(/export\s+default\s+nextConfig\s*;?/)) {
// Pattern: export default nextConfig;
updatedConfig = updatedConfig.replace(/export\s+default\s+nextConfig\s*;?/, 'export default tribuneBlogPlugin.apply(nextConfig);');
}
else if (updatedConfig.match(/export\s+default\s+{/)) {
// Pattern: export default { ... }
updatedConfig = updatedConfig.replace(/export\s+default\s+({[\s\S]*?});?/, 'const nextConfig: NextConfig = $1;\n\nexport default tribuneBlogPlugin.apply(nextConfig);');
}
}
else if (isESM) {
console.log('š§ Processing ESM config...');
// Similar to TypeScript but without type annotations
const importMatch = configContent.match(/^(import[\s\S]*?from\s+['"][^'"]+['"];?\s*\n)+/m);
if (importMatch) {
updatedConfig = configContent.slice(0, importMatch.index + importMatch[0].length) +
importStatement +
pluginSetup +
configContent.slice(importMatch.index + importMatch[0].length);
}
else {
updatedConfig = importStatement + pluginSetup + configContent;
}
updatedConfig = updatedConfig.replace(/export\s+default\s+/, 'const nextConfig = ') + '\n\nexport default tribuneBlogPlugin.apply(nextConfig);';
}
else {
console.log('š§ Processing CommonJS config...');
// CommonJS
updatedConfig = importStatement + pluginSetup + configContent.replace(/module\.exports\s*=\s*/, 'const nextConfig = ') + '\n\nmodule.exports = tribuneBlogPlugin.apply(nextConfig);';
}
console.log('š¾ Writing updated config...');
await fs_extra_1.default.writeFile(configPath, updatedConfig);
console.log('ā
Config file updated successfully!');
}
else {
console.log('ā ļø No existing config found, creating new next.config.ts...');
// Create new next.config.ts
const newConfigTs = `import type { NextConfig } from "next";
import { NextTribuneBlogPlugin } from 'next-tribune-blog';
// Tribune Blog Configuration
const tribuneBlogPlugin = new NextTribuneBlogPlugin({
// Add your wallet address to show only your articles
walletAddress: '0xYourWalletAddress', // <-- CHANGE THIS TO YOUR WALLET ADDRESS
// OPTIONAL: Change the output directory if needed (default: 'src/app/blog')
// outputDir: 'src/app/blog',
});
const nextConfig: NextConfig = {};
export default tribuneBlogPlugin.apply(nextConfig);`;
await fs_extra_1.default.writeFile(nextConfigTsPath, newConfigTs);
console.log('ā
next.config.ts created with Tribune blog plugin');
}
// Create README
const readmePath = path_1.default.join(projectRoot, 'TRIBUNE_BLOG_README.md');
const readmeContent = `
Your Next.js project is now configured to generate blog pages from Tribune.sh articles!
Edit your \`next.config.ts\` file and replace \`0xYourWalletAddress\` with your actual wallet address:
\`\`\`typescript
const tribuneBlogPlugin = new NextTribuneBlogPlugin({
// Add your wallet address to show only your articles
walletAddress: '0xYourWalletAddress', // <-- CHANGE THIS TO YOUR WALLET ADDRESS
});
\`\`\`
1. Set your wallet address in \`next.config.ts\`
2. Run \`npm run build\` or \`bun run build\`
3. Run \`npm run dev\` or \`bun run dev\`
4. Visit http://localhost:3000/blog
- Automatically fetches YOUR articles from Tribune.sh blockchain
- Generates static blog pages at build time
- Beautiful, responsive design with dark mode support
- Links to Tribune.sh and blockchain explorer
- SEO-friendly with metadata support
- Articles are fetched at build time for optimal performance
- To update articles, run the build command again
- Each article includes links to view on Tribune.sh and Abstract Scan
- The Tribune contract and RPC URL are pre-configured
`;
await fs_extra_1.default.writeFile(readmePath, readmeContent);
console.log('\nš Installation complete!');
console.log('\nNext steps:');
console.log('1. Configure your Tribune contract address in next.config.ts');
console.log('2. Run "npm run build" or "bun run build"');
console.log('3. Run "npm run dev" or "bun run dev"');
console.log('4. Visit http://localhost:3000/blog');
console.log('\nFor more information, see TRIBUNE_BLOG_README.md');
}
setup().catch((error) => {
console.error('ā Setup failed:', error);
process.exit(1);
});
//# sourceMappingURL=setup.js.map