Skip to content

Commit 9e96e0a

Browse files
committed
feat: finish the view create & check
1 parent 2933545 commit 9e96e0a

11 files changed

+182
-38
lines changed

Readme.md

-3
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ yarn leet-check
8181
```
8282

8383
此指令会根据今天的题目信息去执行对应的题目文件,输出结果。
84-
> NOTE:缓存的实现是在`commom/resouces/store.json`,如果只想让内容在本地存在,不上传到个人项目中的话,执行`git update-index --aussume-unchanged common/resources/store.json`来忽略本地的文件变更即可。
85-
>
8684

8785
```shell
8886
yarn leet-check
@@ -193,7 +191,6 @@ yarn leet-create -r
193191
D:\GitHub\leetcode-practice> yarn leet-create -r
194192
yarn run v1.22.19
195193
$ node common/scripts/create.js -r
196-
[store]数据存储成功[random-question-info]:[[object Object]]
197194
[fulfillQuestion]题目[43][字符串相乘]已完成填充.
198195
Done in 1.78s.
199196
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {readdirSync} from "fs";
2+
3+
export function getFileListBySameName(dir, name) {
4+
return readdirSync(dir).filter((filename) => filename.includes(name));
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import fs from "fs";
2+
import vm from "vm";
3+
import {showLogs} from "#common/utils/question-handler/showLogs.js";
4+
5+
/**
6+
* 执行脚本 - 可传入上下文
7+
* @param filePath
8+
* @param context
9+
* @returns {any}
10+
*/
11+
export function executeScript(filePath, context) {
12+
const fileContent = fs.readFileSync(filePath, 'utf-8');
13+
const script = new vm.Script(fileContent);
14+
return script.runInContext(context);
15+
}
16+
17+
/**
18+
* 执行问题检测进程
19+
* @param path
20+
*/
21+
export const checkQuestion = async (path)=>{
22+
return await executeScript(path, vm.createContext({
23+
showLogs
24+
}))
25+
}
26+
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
import fs from "fs";
22
import path from "path";
33
import {fulfillQuestion} from "#common/utils/question-handler/fulfillQuestion.js";
4+
45
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);
17-
});
18-
});
19-
}
20-
})
6+
export const createQuestion = (question, questionDir) => {
7+
return new Promise(resolve => {
8+
let filePath = path.normalize(path.join(questionDir, 'index.js'));
9+
if (fs.existsSync(filePath)) {
10+
resolve(false);
11+
} else {
12+
createQuestionFile(questionDir, filePath, question).then(path => resolve(path)).catch(e => resolve(false));
13+
}
14+
})
15+
}
16+
export const createQuestionFile = (questionDir, questionFilePath, question) => {
17+
return new Promise((resolve, reject) => {
18+
try {
19+
fs.mkdir(questionDir, {recursive: true}, () => {
20+
// 复制文件
21+
fs.copyFile(sourceFilePath, questionFilePath, 0, () => {
22+
fulfillQuestion(questionFilePath, question)
23+
resolve(questionFilePath)
24+
});
25+
});
26+
} catch (e) {
27+
reject(e);
28+
}
29+
})
2130
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11
//todo 创建问题副本
2-
export const createQuestionCopy = (question)=>{
2+
import {getCountBySameName} from "#common/utils/file/getCountBySameName.js";
3+
import {createQuestionFile} from "#common/utils/question-handler/createQuestion.js";
4+
import path from "path";
35

6+
/**
7+
* 创建副本
8+
* @param question
9+
* @param questionDir
10+
* @returns {Promise<unknown>}
11+
*/
12+
export const createQuestionCopy = (question, questionDir)=>{
13+
if(!question||!question.id)return Promise.reject("question is empty");
14+
const dir = path.dirname(questionDir);
15+
const name = `${question.id}.${question.slug}`;
16+
const affix = ` [${getCountBySameName(dir,name)}]`;
17+
const copyFileDir = path.join(dir,`${name}${affix}`);
18+
const copyFilePath = path.join(copyFileDir,`index.js`);
19+
return createQuestionFile(copyFileDir,copyFilePath,question);
420
}

common/utils/store/store-realm.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ const open = async ()=>{
2020
*/
2121
const exeOnce = async (callback)=>{
2222
const realm = await open();
23-
const res = callback(realm);
24-
realm.close()
23+
const res = await callback(realm);
24+
realm.close();
2525
return res;
2626
}
2727

@@ -47,7 +47,7 @@ export const setQuestion = (mode,question)=>exeOnce((realm)=>{
4747
realm.delete(realm.objects("Question").filtered("mode=$0",mode));
4848
newQuestion = realm.create("Question", Object.assign(question,{mode}));
4949
});
50-
return newQuestion;
50+
return newQuestion.toJSON();
5151
});
5252
/**
5353
* 删除某一个模式

common/view/check.view.js

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import inquirer from "inquirer";
2+
import path from "path";
3+
import {getQuestionFileName} from "#common/utils/question-handler/getQuestionFileName.js";
4+
import {getQuestionById} from "#common/utils/question-getter/getQuestionById.js";
5+
import {getQuestionToday} from "#common/utils/question-getter/getQuestionToday.js";
6+
import {getQuestionRandom} from "#common/utils/question-getter/getQuestionRandom.js";
7+
import {createQuestion} from "#common/utils/question-handler/createQuestion.js";
8+
import {createQuestionCopy} from "#common/utils/question-handler/createQuestionCopy.js";
9+
import {getQuestionByMode} from "#common/utils/store/store-realm.js";
10+
import {checkQuestion} from "#common/utils/question-handler/checkQuestion.js";
11+
import {getCountBySameName} from "#common/utils/file/getCountBySameName.js";
12+
import {getFileListBySameName} from "#common/utils/file/getFileListBySameName.js";
13+
const modeQuestion = [{
14+
type: 'list',
15+
name: 'mode',
16+
message: '请选择检查问题的模式:',
17+
choices: ['today', 'identity', 'random'],
18+
}];
19+
// 第一个问题 选择的模式
20+
const {mode} = await inquirer.prompt(modeQuestion,null);
21+
const identityQuestion = [{
22+
type: 'input',
23+
name: 'identity',
24+
message: '请输入题目编号:',
25+
}];
26+
let question;
27+
switch (mode){
28+
case "identity":
29+
const {identity} = await inquirer.prompt(identityQuestion,null);
30+
question = !identity?
31+
await getQuestionByMode(mode):
32+
await getQuestionById(identity);
33+
break;
34+
case "random":
35+
question = await getQuestionByMode(mode);
36+
break;
37+
case "today":
38+
default:
39+
question = await getQuestionByMode(mode);
40+
break;
41+
}
42+
// 检查题目
43+
const questionFileName = getQuestionFileName(question);
44+
const currentDir = process.cwd();
45+
let questionDir = path.join(currentDir,questionFileName);
46+
// 创建路径确认
47+
const pathRightQuestion = [{
48+
type: 'confirm',
49+
name: 'dirRight',
50+
message: `是否检测当前目录[ ${currentDir} ]下的题目[ ${questionFileName} ]?`,
51+
}];
52+
const {dirRight} = await inquirer.prompt(pathRightQuestion,null);
53+
if(!dirRight){
54+
const newPathRightQuestion = [{
55+
type: 'confirm',
56+
name: 'newDirRight',
57+
message: `请选择要检测的目录?`,
58+
}];
59+
const {newDirRight} = await inquirer.prompt(newPathRightQuestion,null);
60+
if(!newDirRight){
61+
console.log("用户取消检测操作")
62+
}else{
63+
const newDirQuestion = [{
64+
type: 'input',
65+
name: 'newDir',
66+
message: `请选择新目录(基础地址为${currentDir}):`,
67+
}];
68+
const {newDir} = await inquirer.prompt(newDirQuestion,null);
69+
questionDir = path.join(path.join(process.cwd(),newDir),`${questionFileName}`)
70+
}
71+
}
72+
const questionParentDir = path.dirname(questionDir);
73+
// 先检测有几个副本
74+
if(getCountBySameName(questionParentDir,questionFileName)>0){
75+
const selectQuestionQuestion = [{
76+
type: 'list',
77+
name: 'selectQuestion',
78+
message: `题目[ ${questionFileName} ]有多个副本,请选择一个进行检测:`,
79+
choices:getFileListBySameName(questionParentDir,questionFileName)
80+
}]
81+
// 选择其中一个副本进行检查
82+
const {selectQuestion} = await inquirer.prompt(selectQuestionQuestion,null);
83+
questionDir = path.join(questionParentDir,selectQuestion);
84+
console.log(`用户选择题目[ ${questionFileName}]的副本[ ${selectQuestion}]进行检测`)
85+
}
86+
const filePath = path.join(questionDir,"index.js");
87+
await checkQuestion(filePath);
88+
console.log(`题目[${questionFileName}]检查完成!\n文件地址为: ${filePath}`)
89+
process.exit(0)

common/view/create.view.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {getQuestionToday} from "#common/utils/question-getter/getQuestionToday.j
66
import {getQuestionRandom} from "#common/utils/question-getter/getQuestionRandom.js";
77
import {createQuestion} from "#common/utils/question-handler/createQuestion.js";
88
import {createQuestionCopy} from "#common/utils/question-handler/createQuestionCopy.js";
9+
import {setQuestion} from "#common/utils/store/store-realm.js";
910
const modeQuestion = [{
1011
type: 'list',
1112
name: 'mode',
@@ -34,6 +35,9 @@ switch (mode){
3435
question = await getQuestionToday()
3536
break;
3637
}
38+
const store = await setQuestion(mode,question);
39+
if(!store) console.warn(`[create][${mode}]问题[${question.title}]未成功缓存`)
40+
console.log(`[create][${mode}]问题[${question.title}]已缓存`,store)
3741
// 创建题目
3842
const questionFileName = getQuestionFileName(question);
3943
const currentDir = process.cwd();
@@ -64,8 +68,9 @@ if(!dirRight){
6468
questionDir = path.join(path.join(process.cwd(),newDir),`${questionFileName}`)
6569
}
6670
}
67-
let filePath = await createQuestion(question,questionDir)
71+
let filePath = await createQuestion(question,questionDir);
6872
if(!filePath){
69-
filePath = createQuestionCopy(question)
73+
filePath = await createQuestionCopy(question,questionDir);
7074
}
71-
console.log(`题目[${questionFileName}]创建完成!`)
75+
console.log(`题目[${questionFileName}]创建完成!\n文件地址为: ${filePath}`)
76+
process.exit(0)

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
"leet-create": "node scripts/create.js",
1818
"leet-create:i": "node scripts/create.js -i",
1919
"leet-check": "node scripts/check.js",
20+
"easy-create": "node common/view/create.view.js",
21+
"easy-check": "node common/view/check.view.js",
2022
"commit": "cz",
2123
"test": "vitest --run",
2224
"coverage": "vitest run --coverage",

scripts/check.js

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/**
22
* 执行脚本
33
*/
4-
import {temExe} from "#common/utils/temExe.js";
5-
import {readStore} from "#common/utils/store.js";
64
import {showLogs} from "#common/utils/showLogs.js";
75
import fs from 'fs';
86
import vm from 'vm'
@@ -39,20 +37,18 @@ switch (args[0]) {
3937
break;
4038
}
4139

42-
function executeScript(filePath, content) {
43-
const fileContent = fs.readFileSync(filePath, 'utf-8');
44-
45-
const script = new vm.Script(fileContent);
46-
return script.runInContext(content);
40+
/**
41+
* 执行脚本
42+
* @param filePath
43+
* @param context
44+
* @returns {any}
45+
*/
4746

48-
}
4947
async function main() {
5048
const src = parseFilePath(`${process.cwd()}/src/${name}/index.js`);
5149

5250
try {
53-
await executeScript(src, vm.createContext({
54-
showLogs
55-
}))
51+
5652
} catch (error) {
5753
console.log('执行失败', error)
5854
}

scripts/create.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {createQuestion} from "#common/utils/question-handler/createQuestion.js";
22
import {getQuestionToday} from "#common/utils/question-getter/getQuestionToday.js";
33
import {fulfillQuestion} from "#common/utils/question-handler/fulfillQuestion.js";
4-
import {writeStore} from "#common/utils/store/store.js";
54
import {getQuestionById} from "#common/utils/question-getter/getQuestionById.js";
65
import {getRandomId} from "#common/utils/question-handler/getRandomId.js";
76
import {setQuestion} from "#common/utils/store/store-realm.js";

0 commit comments

Comments
 (0)