Skip to content

Commit da6aa6d

Browse files
committed
fix: 优化代码
1 parent 62317a1 commit da6aa6d

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

bin/lc.js

+21-22
Original file line numberDiff line numberDiff line change
@@ -30,39 +30,39 @@ program
3030

3131
const cmdArgs = program.args;
3232
const cmdOpts = program.opts();
33+
// 创建
34+
const create = (mode,question)=>{
35+
return new Promise(resolve=>{
36+
setQuestion(mode,question);
37+
const questionDir = path.join(process.cwd(),getQuestionFileName(question))
38+
createQuestion(question,questionDir).then(async (path)=>{
39+
if(!path)path = await createQuestionCopy(question,questionDir);
40+
console.log(`[lc] 获取随机题目成功\n题目为[${question.title}]\n文件地址为:${path}`)
41+
resolve(true)
42+
})
43+
})
44+
}
3345
// 模式对应的action
3446
const callModeAction = {
3547
'today': () => {
3648
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-
})
49+
create("today",question).then(()=>{
50+
process.exit(0)
51+
});
4452
})
4553
},
4654
'random': () => {
4755
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-
})
56+
create("random",question).then(()=>{
57+
process.exit(0)
58+
});
5559
})
5660
},
5761
'identity': (id) => {
5862
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-
})
63+
create("identity",question).then(()=>{
64+
process.exit(0)
65+
});
6666
})
6767
},
6868
}
@@ -71,4 +71,3 @@ const mode = referMode(cmdArgs, cmdOpts);
7171
const args = getArgs(mode,cmdArgs,cmdOpts);
7272
// 执行指令分发
7373
await callModeAction[mode](args);
74-
//

0 commit comments

Comments
 (0)