Skip to content

Commit 62317a1

Browse files
committed
feat:完成lc&lk指令
1 parent 9e96e0a commit 62317a1

File tree

11 files changed

+123
-57
lines changed

11 files changed

+123
-57
lines changed

bin/lc.js

+39-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,16 @@ import {aim} from "../resources/text/aim.js";
77
import {referMode} from "#common/utils/create-check/refer-mode.js";
88
import {getArgs} from "#common/utils/create-check/get-args.js";
99
import fs from "fs";
10-
const {version} = JSON.parse(fs.readFileSync("package.json",'utf-8'));
10+
import {getQuestionToday} from "#common/utils/question-getter/getQuestionToday.js";
11+
import {createQuestion} from "#common/utils/question-handler/createQuestion.js";
12+
import path from "path";
13+
import {getQuestionFileName} from "#common/utils/question-handler/getQuestionFileName.js";
14+
import {createQuestionCopy} from "#common/utils/question-handler/createQuestionCopy.js";
15+
import {getQuestionRandom} from "#common/utils/question-getter/getQuestionRandom.js";
16+
import {getQuestionById} from "#common/utils/question-getter/getQuestionById.js";
17+
import {setQuestion} from "#common/utils/store/store-realm.js";
18+
import {rootPath} from "#common/utils/file/getRootPath.js";
19+
const {version} = JSON.parse(fs.readFileSync(path.resolve(rootPath,"package.json"),'utf-8'));
1120

1221
program
1322
.version(version)
@@ -24,20 +33,42 @@ const cmdOpts = program.opts();
2433
// 模式对应的action
2534
const callModeAction = {
2635
'today': () => {
27-
// todo 获取今日题目的进程
28-
console.log("[leetcode-practice] 获取今日题目")
36+
getQuestionToday().then(question=>{
37+
setQuestion("today",question);
38+
const questionDir = path.join(process.cwd(),getQuestionFileName(question))
39+
createQuestion(question,questionDir).then(async (path)=>{
40+
if(!path)path = await createQuestionCopy(question,questionDir);
41+
console.log(`[lc] 获取今日题目成功\n题目为[${question.title}]\n文件地址为:${path}`)
42+
process.exit(0);
43+
})
44+
})
2945
},
3046
'random': () => {
31-
// todo 获取随机题目的进程
32-
console.log("[leetcode-practice] 获取随机题目")
47+
getQuestionRandom().then(question=>{
48+
setQuestion("random",question);
49+
const questionDir = path.join(process.cwd(),getQuestionFileName(question))
50+
createQuestion(question,questionDir).then(async (path)=>{
51+
if(!path)path = await createQuestionCopy(question,questionDir);
52+
console.log(`[lc] 获取随机题目成功\n题目为[${question.title}]\n文件地址为:${path}`)
53+
process.exit(0);
54+
})
55+
})
3356
},
3457
'identity': (id) => {
35-
// todo 获取指定题目的进程
36-
console.log(`[leetcode-practice] 获取指定题目[${id}]`)
58+
getQuestionById(id).then(question=>{
59+
setQuestion("identity",question);
60+
const questionDir = path.join(process.cwd(),getQuestionFileName(question))
61+
createQuestion(question,questionDir).then(async (path)=>{
62+
if(!path)path = await createQuestionCopy(question,questionDir);
63+
console.log(`[lc] 获取指定题目成功\n题目为[${question.title}]\n文件地址为:${path}`)
64+
process.exit(0);
65+
})
66+
})
3767
},
3868
}
3969
// 获取模式和参数
4070
const mode = referMode(cmdArgs, cmdOpts);
4171
const args = getArgs(mode,cmdArgs,cmdOpts);
4272
// 执行指令分发
43-
callModeAction[mode](args);
73+
await callModeAction[mode](args);
74+
//

bin/lk.js

+35-12
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ import {aim} from "../resources/text/aim.js";
77
import {referMode} from "#common/utils/create-check/refer-mode.js";
88
import {getArgs} from "#common/utils/create-check/get-args.js";
99
import fs from "fs";
10-
const {version} = JSON.parse(fs.readFileSync("package.json",'utf-8'));
11-
10+
import path from "path";
11+
import {checkQuestion} from "#common/utils/question-handler/checkQuestion.js";
12+
import {getQuestionByMode} from "#common/utils/store/store-realm.js";
13+
import {getQuestionById} from "#common/utils/question-getter/getQuestionById.js";
14+
import {getQuestionFileName} from "#common/utils/question-handler/getQuestionFileName.js";
15+
import {rootPath} from "#common/utils/file/getRootPath.js";
16+
const {version} = JSON.parse(fs.readFileSync(path.resolve(rootPath,"package.json"),'utf-8'));
1217
program
1318
.version(version)
1419
.description(`${artFontLogo}\n${aim}`)
@@ -21,23 +26,41 @@ program
2126

2227
const cmdArgs = program.args;
2328
const cmdOpts = program.opts();
29+
// 检测函数
30+
const check = async (mode,filePath,question)=>{
31+
if(!fs.existsSync(filePath)) {
32+
console.log(`文件[${filePath}]不存在,请确保已经创建!`)
33+
}else{
34+
console.log(`MODE: ${mode}\n题目[${question.id}.${question.title}]检测结果:`)
35+
await checkQuestion(filePath);
36+
}
37+
return true;
38+
}
2439
// 模式对应的action
2540
const callModeAction = {
26-
'today': () => {
27-
// todo 获取今日题目的进程
28-
console.log("[leetcode-practice] 检测今日题目")
41+
'today': async () => {
42+
const question = await getQuestionByMode("today");
43+
const filePath = path.join(process.cwd(),getQuestionFileName(question),'index.js');
44+
await check('today',filePath,question)
45+
process.exit(0);
2946
},
30-
'random': () => {
31-
// todo 获取随机题目的进程
32-
console.log("[leetcode-practice] 检测随机题目")
47+
'random': async () => {
48+
const question = await getQuestionByMode("random");
49+
const filePath = path.join(process.cwd(),getQuestionFileName(question),'index.js');
50+
await check('today',filePath,question)
51+
process.exit(0);
3352
},
34-
'identity': (id) => {
35-
// todo 获取指定题目的进程
36-
console.log(`[leetcode-practice] 检测指定题目[${id}]`)
53+
'identity': async (id) => {
54+
const question = !id?
55+
await getQuestionByMode(mode):
56+
await getQuestionById(id);
57+
const filePath = path.join(process.cwd(),getQuestionFileName(question),'index.js');
58+
await check('today',filePath,question)
59+
process.exit(0);
3760
},
3861
}
3962
// 获取模式和参数
4063
const mode = referMode(cmdArgs, cmdOpts);
4164
const args = getArgs(mode,cmdArgs,cmdOpts);
4265
// 执行指令分发
43-
callModeAction[mode](args);
66+
callModeAction[mode](args);

common/utils/file/getRootPath.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import {__dirname} from "#common/utils/file/getDirname.js";
2+
import path from "path";
3+
export const rootPath =path.dirname(path.dirname(path.dirname(__dirname)));

common/utils/question-handler/checkQuestion.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function executeScript(filePath, context) {
2020
*/
2121
export const checkQuestion = async (path)=>{
2222
return await executeScript(path, vm.createContext({
23-
showLogs
23+
showLogs,console
2424
}))
2525
}
2626

common/utils/question-handler/createMarkdown.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import path from 'path';
66
* @param {*} description
77
* @param {*} questionPath
88
*/
9-
function createMarkdown(description, questionPath) {
9+
export function createMarkdown(description, questionPath) {
1010
const dir = path.dirname(questionPath);
1111
const descriptionPath = path.join(dir, 'description.md');
1212
fs.writeFileSync(descriptionPath, description);
1313
}
14-
export default createMarkdown;

common/utils/question-handler/createQuestion.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import fs from "fs";
22
import path from "path";
33
import {fulfillQuestion} from "#common/utils/question-handler/fulfillQuestion.js";
4+
import {template} from "#resources/template/template.js";
45

5-
export const sourceFilePath = path.normalize('resources/template/template.js');
6+
/**
7+
* 创建问题
8+
* @param question 问题对象
9+
* @param questionDir 问题要创建的目录 截止到名字
10+
* @returns {Promise<unknown>}
11+
*/
612
export const createQuestion = (question, questionDir) => {
713
return new Promise(resolve => {
814
let filePath = path.normalize(path.join(questionDir, 'index.js'));
@@ -17,10 +23,8 @@ export const createQuestionFile = (questionDir, questionFilePath, question) => {
1723
return new Promise((resolve, reject) => {
1824
try {
1925
fs.mkdir(questionDir, {recursive: true}, () => {
20-
// 复制文件
21-
fs.copyFile(sourceFilePath, questionFilePath, 0, () => {
22-
fulfillQuestion(questionFilePath, question)
23-
resolve(questionFilePath)
26+
fs.writeFile(questionFilePath, template,null,()=>{
27+
fulfillQuestion(questionFilePath, question).then(()=>resolve(questionFilePath))
2428
});
2529
});
2630
} catch (e) {
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import fs from 'fs';
2-
import { removeDomTags } from '../functions/removeDomTags.js';
3-
import { getTestCase } from './getTestCase.js';
4-
import { getQuestionUrl } from './getQuestionUrl.js';
5-
import createMarkdown from './createMarkdown.js';
6-
import path from "path";
2+
import {removeDomTags} from '../functions/removeDomTags.js';
3+
import {getTestCase} from './getTestCase.js';
4+
import {getQuestionUrl} from './getQuestionUrl.js';
5+
import {createMarkdown} from './createMarkdown.js';
76
/**
87
* @typedef {Object} Question
98
* @property {string} title
@@ -23,28 +22,31 @@ import path from "path";
2322
*/
2423
export const generateTemplateContent = (data, question) =>
2524
data
26-
.replace('@题目', `${question.id}.${question.title} ${question.date ? `[${question.date}]` : ''}`)
27-
.replace('@描述', removeDomTags(question.detail)
28-
.replace('@url', question.url)
29-
.replace(/\n+/g, '\n')
30-
.replaceAll('\n', '\n * '))
31-
.replace('// @Function', question.jsCode)
32-
.replace('// @TestCase', getTestCase(question))
33-
.replace('@url', getQuestionUrl(question.slug));
25+
.replace('@题目', `${question.id}.${question.title} ${question.date ? `[${question.date}]` : ''}`)
26+
.replace('@描述', removeDomTags(question.detail)
27+
.replace('@url', question.url)
28+
.replace(/\n+/g, '\n')
29+
.replaceAll('\n', '\n * '))
30+
.replace('// @Function', question.jsCode)
31+
.replace('// @TestCase', getTestCase(question))
32+
.replace('@url', getQuestionUrl(question.slug));
3433
/**
3534
* 填充模板文件
3635
* @param questionPath
3736
* @param question
3837
*/
3938
export const fulfillQuestion = (questionPath, question) => {
40-
// 开始填充内容
41-
fs.readFile(questionPath, 'utf8', (err, data) => {
42-
if (err) throw err;
43-
// 修改文件内容
44-
const newData = generateTemplateContent(data, question);
45-
createMarkdown(question.detail, questionPath);
46-
fs.writeFile(questionPath, newData, (err) => {
47-
if (err) throw err;
48-
});
49-
});
39+
return new Promise(resolve => {
40+
// 开始填充内容
41+
fs.readFile(questionPath, 'utf8', (err, data) => {
42+
if (err) throw err;
43+
// 修改文件内容
44+
const newData = generateTemplateContent(data, question);
45+
createMarkdown(question.detail, questionPath);
46+
fs.writeFile(questionPath, newData, (err) => {
47+
if (err) throw err;
48+
resolve()
49+
});
50+
});
51+
})
5052
};

common/utils/store/store-realm.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import Realm from "realm";
22
import {Question} from "#common/utils/store/schemas/question.js";
33
import path from "path";
4-
const localPath = path.normalize("resources/stores/store.realm")
4+
import {rootPath} from "#common/utils/file/getRootPath.js";
5+
const localPath = path.resolve(rootPath,"resources/stores/store.realm")
56
/**
67
* 开启
78
* @returns {Promise<Realm>}
@@ -33,7 +34,7 @@ const exeOnce = async (callback)=>{
3334
export const getQuestionByMode = (mode)=>exeOnce((realm)=>{
3435
const all = realm.objects("Question");
3536
const question = all.filtered("mode=$0",mode)?.[0];
36-
return question.toJSON();
37+
return question?.toJSON();
3738
})
3839
/**
3940
* 存对象

jsconfig.json

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
"paths": {
55
"#common/*": [
66
"./common/*"
7+
],
8+
"#resources/*": [
9+
"./resources/*"
710
]
811
}
912
}

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"main": "index.js",
66
"type": "module",
77
"imports": {
8-
"#common/*": "./common/*"
8+
"#common/*": "./common/*",
9+
"#resources/*": "./resources/*"
910
},
1011
"packageManager": "pnpm@7.0.0+",
1112
"bin": {

resources/template/template.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
2-
1+
export const template = `
32
/**
43
* @题目
54
* @描述
@@ -10,4 +9,4 @@
109
* Test case
1110
*/
1211
// @TestCase
13-
console.log('点击跳转到题目提交:@url');
12+
console.log('点击跳转到题目提交:@url');`

0 commit comments

Comments
 (0)