UNPKG

rp-markdown-docs

Version:

A modern, beautiful documentation generator that converts markdown files into interactive HTML documentation sites

269 lines (221 loc) 7.17 kB
import fs from 'fs-extra'; import path from 'path'; import chalk from 'chalk'; import ora from 'ora'; import inquirer from 'inquirer'; export async function initProject(directory = '.', options) { const spinner = ora('Initializing documentation project...').start(); try { const targetDir = path.resolve(directory); // Check if directory exists and is not empty if (await fs.pathExists(targetDir)) { const files = await fs.readdir(targetDir); if (files.length > 0) { spinner.stop(); const { overwrite } = await inquirer.prompt([ { type: 'confirm', name: 'overwrite', message: `Directory ${directory} is not empty. Continue anyway?`, default: false } ]); if (!overwrite) { console.log(chalk.yellow('Operation cancelled.')); return; } spinner.start(); } } await fs.ensureDir(targetDir); // Create directory structure const dirs = ['docs', 'docs/getting-started', 'docs/api', 'docs/guides']; for (const dir of dirs) { await fs.ensureDir(path.join(targetDir, dir)); } // Create configuration file const config = `module.exports = { title: 'My Documentation', description: 'Comprehensive documentation for my project', baseUrl: '/', theme: { primaryColor: '#3B82F6', accentColor: '#10B981', darkMode: true }, navigation: { logo: 'My Docs', links: [ { text: 'GitHub', url: 'https://github.com/yourusername/yourproject' }, { text: 'NPM', url: 'https://npmjs.com/package/yourpackage' } ] }, features: { search: true, tableOfContents: true, editOnGitHub: true, lastModified: true }, gitHub: { repo: 'yourusername/yourproject', branch: 'main', docsDir: 'docs' } };`; await fs.writeFile(path.join(targetDir, 'mdocs.config.js'), config); // Create sample markdown files const indexContent = `# Welcome to My Documentation This is the main page of your documentation. You can edit this file to customize your homepage. ## Quick Start 1. Edit the files in the \`docs/\` directory 2. Run \`mdocs generate\` to build your documentation 3. Run \`mdocs serve\` to preview your site locally ## Features - 🚀 Fast and modern interface - 🔍 Real-time search - 📱 Mobile responsive - 🌙 Dark mode support - 📝 Markdown support with syntax highlighting - 🎨 Customizable themes ## Getting Started Check out the [Getting Started Guide](./getting-started/installation.md) to learn more. `; const installationContent = `# Installation Learn how to install and set up the project. ## Prerequisites Before you begin, ensure you have the following installed: - Node.js (version 16 or higher) - npm or yarn package manager ## Installation Steps 1. Install the package: \`\`\`bash npm install your-package-name \`\`\` 2. Import and use: \`\`\`javascript import { YourPackage } from 'your-package-name'; const instance = new YourPackage(); \`\`\` ## Configuration Create a configuration file: \`\`\`json { "apiKey": "your-api-key", "environment": "production" } \`\`\` ## Next Steps - [Configuration Guide](../guides/configuration.md) - [API Reference](../api/overview.md) `; const apiContent = `# API Overview Complete API reference for the project. ## Authentication All API requests require authentication using an API key: \`\`\`http Authorization: Bearer YOUR_API_KEY \`\`\` ## Base URL \`\`\` https://api.example.com/v1 \`\`\` ## Endpoints ### GET /users Retrieve a list of users. **Parameters:** | Parameter | Type | Description | |-----------|------|-------------| | \`limit\` | number | Number of users to return | | \`offset\` | number | Number of users to skip | **Response:** \`\`\`json { "users": [ { "id": "123", "name": "John Doe", "email": "john@example.com" } ], "total": 100 } \`\`\` `; const configGuideContent = `# Configuration Guide Learn how to configure the project for your needs. ## Basic Configuration The configuration file supports the following options: \`\`\`javascript module.exports = { // Basic settings apiUrl: 'https://api.example.com', timeout: 5000, // Feature flags features: { analytics: true, debugging: false }, // Theme customization theme: { primaryColor: '#3B82F6', darkMode: true } }; \`\`\` ## Environment Variables Set these environment variables: - \`API_KEY\`: Your API key - \`NODE_ENV\`: Environment (development/production) - \`DEBUG\`: Enable debug mode (true/false) ## Advanced Configuration For advanced use cases, you can: 1. Custom middleware 2. Plugin system 3. Custom themes 4. Internationalization See the [Advanced Guide](./advanced.md) for more details. `; await fs.writeFile(path.join(targetDir, 'docs/index.md'), indexContent); await fs.writeFile(path.join(targetDir, 'docs/getting-started/installation.md'), installationContent); await fs.writeFile(path.join(targetDir, 'docs/api/overview.md'), apiContent); await fs.writeFile(path.join(targetDir, 'docs/guides/configuration.md'), configGuideContent); // Create package.json scripts const packageJsonPath = path.join(targetDir, 'package.json'); let packageJson = {}; if (await fs.pathExists(packageJsonPath)) { packageJson = await fs.readJson(packageJsonPath); } else { packageJson = { name: path.basename(targetDir), version: '1.0.0', description: 'Documentation project', scripts: {}, devDependencies: {} }; } packageJson.scripts = { ...packageJson.scripts, 'docs:dev': 'mdocs generate --watch', 'docs:build': 'mdocs build', 'docs:serve': 'mdocs serve' }; packageJson.devDependencies = { ...packageJson.devDependencies, 'markdown-docs-generator': '^1.0.0' }; await fs.writeJson(packageJsonPath, packageJson, { spaces: 2 }); spinner.succeed(chalk.green('Documentation project initialized successfully!')); console.log('\n' + chalk.blue('Next steps:')); console.log(chalk.gray('1.'), 'Edit your documentation files in the', chalk.cyan('docs/'), 'directory'); console.log(chalk.gray('2.'), 'Customize', chalk.cyan('mdocs.config.js'), 'to match your project'); console.log(chalk.gray('3.'), 'Run', chalk.cyan('mdocs generate --watch'), 'to start developing'); console.log(chalk.gray('4.'), 'Run', chalk.cyan('mdocs serve'), 'to preview your documentation'); } catch (error) { spinner.fail(chalk.red('Failed to initialize project')); console.error(error); process.exit(1); } } //# sourceMappingURL=init.js.map