|
1 | 1 | import fs from "fs";
|
2 | 2 | import path from "path";
|
3 |
| -import readlinePromises from "node:readline/promises"; |
4 |
| -import {getCountBySameName} from "../getCountBySameName.js"; |
5 |
| -import { __dirname} from '#common/utils/getDirname.js' |
6 |
| -export const sourceFilePath = path.normalize(path.resolve(__dirname, '../template/template.js')); |
7 |
| -export function createQuestion(newPath) { |
8 |
| - const rl = readlinePromises.createInterface({ |
9 |
| - input: process.stdin, |
10 |
| - output: process.stdout, |
11 |
| - }); |
12 |
| - return new Promise((resolve, reject) => { |
13 |
| - let newDir = path.normalize(`${process.cwd()}/src/${newPath}`); |
14 |
| - let newFilePath = path.join(newDir, 'index.js'); |
15 |
| - // 判断是否存在 |
16 |
| - fs.exists(newFilePath, async (exists) => { |
17 |
| - if (exists) { |
18 |
| - const recover = await rl.question(`你想创建的题目[${newPath}]已经存在,是否覆盖? (Y/N)\n`); |
19 |
| - // 如果不覆盖 可以给出两个选择 一个是退出创建进程 一个是创建一个别名 |
20 |
| - if (!['y', ''].includes(recover.toLocaleLowerCase())) { |
21 |
| - let reconfirm = await rl.question('是否添加额外内容对题目进行区分,以创建此题目的另外一个解?(Enter/[[$+任意字符]]/N)\n[注:`$`符为原题目完整标题,添加额外符号后按`Enter`键确认,如不填写直接确认,系统会自动添加`$[num]`标识,例如:1.模拟[1]]\n'); |
22 |
| - if (reconfirm.toLocaleLowerCase() === 'n') { |
23 |
| - console.log(`题目[${newPath}]创建终止`); |
24 |
| - process.exit(); |
25 |
| - } |
26 |
| - if (reconfirm === '' || reconfirm.includes('$')) { |
27 |
| - // 默认 添加序号 |
28 |
| - reconfirm ||= `$[${getCountBySameName(path.normalize('./src'), newPath)}]`; |
29 |
| - const newFileName = reconfirm.replace('$', newPath); |
30 |
| - newDir = path.normalize(`./src/${newFileName}`); |
31 |
| - newFilePath = path.join(newDir, 'index.js'); |
32 |
| - console.log(`[create]题目[${newPath}]创建副本[${newFileName}]`); |
33 |
| - } else { |
34 |
| - console.log(`提示:出于安全性和综合情况考虑,请务必按规定格式创建题目,违规题目暂时不予创建。 |
35 |
| -当前尝试创建题目为:[${reconfirm}]`); |
36 |
| - process.exit(); |
37 |
| - } |
38 |
| - }// 如果覆盖 直接进行下一步 |
39 |
| - } |
40 |
| - rl.close(); |
41 |
| - try { |
42 |
| - // 创建目录 |
43 |
| - fs.mkdir(newDir, { recursive: true }, () => { |
44 |
| - // 复制文件 |
45 |
| - fs.copyFile(sourceFilePath, newFilePath, 0, () => { |
46 |
| - resolve(newFilePath); |
47 |
| - }); |
| 3 | +import {fulfillQuestion} from "#common/utils/question-handler/fulfillQuestion.js"; |
| 4 | +export const sourceFilePath = path.normalize('resources/template/template.js'); |
| 5 | +export const createQuestion = (question,questionDir) => { |
| 6 | + return new Promise(resolve=>{ |
| 7 | + let filePath = path.normalize(path.join(questionDir, 'index.js')); |
| 8 | + if(fs.existsSync(filePath)){ |
| 9 | + resolve(false); |
| 10 | + }else{ |
| 11 | + // 创建目录 |
| 12 | + fs.mkdir(questionDir, { recursive: true }, () => { |
| 13 | + // 复制文件 |
| 14 | + fs.copyFile(sourceFilePath, filePath, 0, () => { |
| 15 | + fulfillQuestion(filePath,question) |
| 16 | + resolve(filePath); |
48 | 17 | });
|
49 |
| - } catch (error) { |
50 |
| - reject('[createQuestion]Error:', error.message); |
51 |
| - } |
52 |
| - }); |
53 |
| - }); |
| 18 | + }); |
| 19 | + } |
| 20 | + }) |
54 | 21 | }
|
0 commit comments