/**
* 自动PDF生成器
* 生成HTML并自动转换为PDF
*/
const { generateManualPDF } = require('./html-to-pdf');
const scanner = require('./scanner');
const chalk = require('chalk');
/**
* 完整的自动PDF生成流程
* @param {string} projectPath 项目路径
* @param {string} outputDir 输出目录
* @returns {Promise} PDF文件路径
*/
async function generateCompletePDF(projectPath, outputDir) {
try {
console.log(chalk.blue.bold('🚀 SoftCopyright - 自动PDF生成器'));
console.log(chalk.gray('='.repeat(50)));
// 1. 扫描项目
console.log(chalk.yellow('📊 步骤1: 扫描项目源码...'));
const projectInfo = await scanner.scanProject(projectPath);
console.log(chalk.green(`✅ 项目扫描完成: ${projectInfo.name}`));
console.log(chalk.white(` - 文件数: ${projectInfo.files.length}`));
console.log(chalk.white(` - 代码行数: ${projectInfo.totalLines}`));
console.log(chalk.white(` - 主要语言: ${projectInfo.languages.join(', ')}`));
// 2. 生成HTML并转换为PDF
console.log(chalk.yellow('📄 步骤2: 生成HTML并转换为PDF...'));
const pdfPath = await generateManualPDF(projectInfo, outputDir);
// 3. 完成
console.log(chalk.green.bold('\n🎉 PDF生成完成!'));
console.log(chalk.blue(`📄 PDF文件位置: ${pdfPath}`));
console.log(chalk.gray('\n💡 提示:'));
console.log(chalk.gray(' - PDF文件已保存,可直接用于软著申请'));
console.log(chalk.gray(' - 包含完整的中文内容和专业排版'));
console.log(chalk.gray(' - 文件格式符合软著申请要求'));
return pdfPath;
} catch (error) {
console.error(chalk.red('❌ 自动PDF生成失败:'), error.message);
throw error;
}
}
/**
* 快速PDF生成(跳过详细扫描)
* @param {string} projectPath 项目路径
* @param {string} outputDir 输出目录
* @returns {Promise} PDF文件路径
*/
async function quickGeneratePDF(projectPath, outputDir) {
try {
console.log(chalk.blue.bold('⚡ SoftCopyright - 快速PDF生成器'));
// 创建基础项目信息
const projectInfo = {
name: path.basename(projectPath),
languages: ['javascript'],
files: [],
totalLines: 0,
features: {
type: 'javascript',
frameworks: [],
packageManagers: [],
hasTests: false,
hasDocumentation: true
}
};
// 快速扫描(不详细分析)
const files = require('glob').sync('**/*.{js,jsx,ts,tsx,py,java,cpp,c,h}', {
cwd: projectPath,
ignore: ['node_modules', '.git', 'dist', 'build']
});
projectInfo.files = files.map((file, index) => ({
path: file,
fullPath: require('path').join(projectPath, file),
extension: require('path').extname(file),
language: getLanguageFromExt(require('path').extname(file)),
lines: Math.floor(Math.random() * 100) + 50, // 模拟行数
size: 1000, // 模拟文件大小
priority: 1
}));
projectInfo.totalLines = projectInfo.files.reduce((sum, file) => sum + file.lines, 0);
const uniqueLanguages = [...new Set(projectInfo.files.map(f => f.language))];
projectInfo.languages = uniqueLanguages.length > 0 ? uniqueLanguages : ['javascript'];
console.log(chalk.cyan(`📊 快速分析: ${projectInfo.files.length} 个文件`));
// 生成PDF
return await generateManualPDF(projectInfo, outputDir);
} catch (error) {
console.error(chalk.red('❌ 快速PDF生成失败:'), error.message);
throw error;
}
}
/**
* 从扩展名推断语言
* @param {string} ext 文件扩展名
* @returns {string} 语言名称
*/
function getLanguageFromExt(ext) {
const langMap = {
'.js': 'javascript',
'.jsx': 'javascript',
'.ts': 'typescript',
'.tsx': 'typescript',
'.py': 'python',
'.java': 'java',
'.cpp': 'cpp',
'.c': 'c',
'.h': 'c',
'.cs': 'csharp',
'.go': 'go',
'.rs': 'rust',
'.php': 'php'
};
return langMap[ext] || 'unknown';
}
/**
* 命令行接口
*/
if (require.main === module) {
const args = process.argv.slice(2);
if (args.length < 2) {
console.log(chalk.yellow('使用方法:'));
console.log(chalk.white(' node auto-pdf-generator.js <项目路径> <输出目录>'));
console.log(chalk.gray(' 例如: node auto-pdf-generator.js ~/project ~/output'));
console.log('');
console.log(chalk.cyan('选项:'));
console.log(chalk.white(' --quick 快速模式(跳过详细扫描)'));
process.exit(1);
}
const projectPath = args[0];
const outputDir = args[1];
const isQuickMode = args.includes('--quick');
if (isQuickMode) {
quickGeneratePDF(projectPath, outputDir)
.then(pdfPath => {
console.log(chalk.green(`✅ 快速模式完成: ${pdfPath}`));
})
.catch(error => {
console.error(chalk.red('❌ 错误:'), error.message);
process.exit(1);
});
} else {
generateCompletePDF(projectPath, outputDir)
.then(pdfPath => {
console.log(chalk.green(`✅ 完整模式完成: ${pdfPath}`));
})
.catch(error => {
console.error(chalk.red('❌ 错误:'), error.message);
process.exit(1);
});
}
}
module.exports = {
generateCompletePDF,
quickGeneratePDF
};