@turingnova/robots
Version:
Next.js robots.tsx generator - Automatically create and serve robots.txt for Next.js applications
114 lines âĸ 5.37 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const commander_1 = require("commander");
const index_1 = require("./index");
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const program = new commander_1.Command();
program
.name('init')
.description('Initialize robots.tsx in your Next.js app')
.version('1.0.21');
program
.option('-d, --dir <directory>', 'Target directory: app or pages', 'auto')
.action((options) => {
try {
const cwd = process.cwd();
const packageJsonPath = path_1.default.join(cwd, 'package.json');
// Check if this is a Next.js project
if (!fs_1.default.existsSync(packageJsonPath)) {
console.log('â No package.json found. This package is for Next.js projects only.');
return;
}
const packageJson = JSON.parse(fs_1.default.readFileSync(packageJsonPath, 'utf8'));
if (!packageJson.dependencies?.next && !packageJson.devDependencies?.next) {
console.log('â This package is designed for Next.js projects only.');
console.log('đĄ Install Next.js first: npm install next');
return;
}
let targetDir = options.dir;
// Auto-detect directory
if (targetDir === 'auto') {
const appDir = path_1.default.join(cwd, 'app');
const pagesDir = path_1.default.join(cwd, 'pages');
const srcAppDir = path_1.default.join(cwd, 'src', 'app');
const srcPagesDir = path_1.default.join(cwd, 'src', 'pages');
if (fs_1.default.existsSync(appDir)) {
targetDir = 'app';
console.log('đ Found existing app/ directory');
}
else if (fs_1.default.existsSync(srcAppDir)) {
targetDir = 'src/app';
console.log('đ Found existing src/app/ directory');
}
else if (fs_1.default.existsSync(pagesDir)) {
targetDir = 'pages';
console.log('đ Found existing pages/ directory');
}
else if (fs_1.default.existsSync(srcPagesDir)) {
targetDir = 'src/pages';
console.log('đ Found existing src/pages/ directory');
}
else {
// Default to app directory (Next.js 13+ App Router)
targetDir = 'app';
console.log('đ No app/, src/app/, pages/, or src/pages/ directory found. Creating app/ directory...');
}
}
console.log(`đ¤ Creating robots.tsx in ${targetDir}/ directory...`);
integrateNextJs(targetDir);
}
catch (error) {
console.error('â Error initializing robots.tsx:', error.message);
}
});
program
.command('generate')
.description('Generate a robots file')
.option('-o, --output <path>', 'Output file path', './robots.tsx')
.option('-f, --format <format>', 'Output format (tsx or txt)', 'tsx')
.option('-u, --user-agent <agent>', 'User agent', '*')
.option('-a, --allow <paths>', 'Comma-separated list of allowed paths', '/')
.option('-d, --disallow <paths>', 'Comma-separated list of disallowed paths', '/admin/,/private/')
.option('-s, --sitemap <url>', 'Sitemap URL')
.option('-c, --crawl-delay <seconds>', 'Crawl delay in seconds')
.option('-h, --host <url>', 'Host URL')
.option('--custom-rules <rules>', 'Comma-separated list of custom rules')
.action((options) => {
const config = {
userAgent: options.userAgent,
allow: options.allow.split(',').map((s) => s.trim()).filter((s) => s),
disallow: options.disallow.split(',').map((s) => s.trim()).filter((s) => s),
sitemap: options.sitemap,
crawlDelay: options.crawlDelay ? parseInt(options.crawlDelay) : undefined,
host: options.host,
customRules: options.customRules ? options.customRules.split(',').map((s) => s.trim()).filter((s) => s) : undefined
};
(0, index_1.createRobotsFile)(config, options.output, options.format);
});
function integrateNextJs(targetDir) {
const cwd = process.cwd();
const targetPath = path_1.default.join(cwd, ...targetDir.split('/'));
// Only create directory if it doesn't exist (for cases where auto-detection said to create it)
if (!fs_1.default.existsSync(targetPath)) {
fs_1.default.mkdirSync(targetPath, { recursive: true });
console.log(`đ Created ${targetDir}/ directory`);
}
const robotsTsxPath = path_1.default.join(targetPath, 'robots.tsx');
if (!fs_1.default.existsSync(robotsTsxPath)) {
const robotsTsxContent = (0, index_1.generateRobotsTsx)();
fs_1.default.writeFileSync(robotsTsxPath, robotsTsxContent);
console.log(`â
robots.tsx created in ${targetDir}/ directory!`);
console.log('đ robots.txt will be served at your-domain.com/robots.txt');
console.log('đ Deploy your Next.js app and robots.txt will be live!');
}
else {
console.log(`âšī¸ robots.tsx already exists in ${targetDir}/ directory`);
}
}
program.parse();
//# sourceMappingURL=cli.js.map