import * as fse from "fs-extra"; import { resolve } from "path"; import { ListItem } from '../types/index'; import { selectWorkspaceFolder } from "../utils/selectWorkspaceFolder"; import { filterInvalidPath } from "../utils/filterInvalidPath"; import { Uri, ViewColumn, window } from "vscode"; export async function openQuestion(ele: ListItem): Promise { const { name, type = 'md', detail, question_id } = ele; const workspaceFolder: string = await selectWorkspaceFolder(); if (!workspaceFolder) { return; } const codeTemplate = getCodeTemplate(type, detail); const finalPath = await showProblem( resolve( workspaceFolder, question_id + "." + filterInvalidPath(name) + "." + type ), codeTemplate || "" ); await window.showTextDocument(Uri.file(finalPath), { preview: false, viewColumn: ViewColumn.One, }); return; } async function showProblem(filePath: string, codeTemplate: string) { if (!(await fse.pathExists(filePath))) { await fse.createFile(filePath); await fse.writeFile(filePath, codeTemplate); } return filePath; } function getCodeTemplate(type: "md" | "js", detail: string) { if (type === "md") { return `# 问题: ${detail} *[攀峰]: start *[攀峰]: end `; } else { return `// 问题: ${detail} // @interview start // @interview end `; } }