UNPKG

oicontest

Version:

OI Contest Management Tool

50 lines (45 loc) 2.29 kB
import { Command } from "commander"; import { loadConfig, saveConfig } from "../lib/config"; import chalk from "chalk"; import path from "path"; import fs from 'fs-extra'; import { contestInfoToMarkdown } from "../utils/utils"; import { convertMarkdownToHtml } from "../lib/mdToHtml"; import open from "open"; export const genHtmlCommand = new Command('genhtml') .description('Generate contest html files.') .action(async () => { try { const contestDir = process.cwd(); const config = await loadConfig(process.cwd()); //没有完成所有题目的验证不能生成html后转pdf console.log(chalk.bold.blue('\n🌐 Generating Contest information to Html file\n')); if(!(config.status && config.status.problemsVerified)) { console.error(chalk.bold.red(`All problems can not generate HTML files without validation complete`)); process.exit(1); } console.log(chalk.green(`✓ All problems validation complete`)); //先将contest信息转换成markdown格式 const mdContent = await contestInfoToMarkdown(config); //生成转换后的文件 const mdPath = path.join(contestDir,config.name+".md"); await fs.outputFile(mdPath,mdContent); //转换成html const htmlStr = await convertMarkdownToHtml(mdContent,config.description); const htmlPath = path.join(contestDir, 'html', config.description + ".html"); // console.log(htmlStr); //生成转换后的文件 await fs.outputFile(htmlPath,htmlStr); //将需要的CDN文件转换成本地文件 // Update status config.status.pdfGenerated = true; await saveConfig(process.cwd(), config); console.log(chalk.green.bold('\n✅ Html generated successfully!')); console.log(chalk.cyan(`html Location: ${htmlPath}, now open this file to preview, you can print it to pdf file.`)); await open(htmlPath); } catch (err: any) { console.error(chalk.red(`Error generating Html: ${err.message}`)); process.exit(1); } });