Skip to content

Commit 3ff1d7b

Browse files
committed
feat: add lc all option
1 parent 5d2189f commit 3ff1d7b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+412
-308
lines changed

.commitlintrc.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module.exports = {
2-
extends: ['@commitlint/config-conventional']
2+
extends: ['@commitlint/config-conventional'],
33
}

.eslintrc.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@ module.exports = {
22
env: {
33
browser: true,
44
commonjs: true,
5-
es2021: true
5+
es2021: true,
66
},
77
extends: '@antfu',
88
overrides: [
99
{
1010
env: {
11-
node: true
11+
node: true,
1212
},
1313
files: ['.eslintrc.{js,cjs}'],
1414
parserOptions: {
15-
sourceType: 'script'
16-
}
17-
}
15+
sourceType: 'script',
16+
},
17+
},
1818
],
1919
parserOptions: {
20-
ecmaVersion: 'latest'
20+
ecmaVersion: 'latest',
2121
},
22-
rules: {}
22+
rules: {},
2323
}

bin/lc.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import { aim } from '#resources/text/aim.js'
77
import { referMode } from '#common/utils/cli-utils/referMode.js'
88
import { getArgs } from '#common/utils/cli-utils/getArgs.js'
99
import { getQuestionToday } from '#common/utils/question-getter/getQuestionToday.js'
10-
1110
import { getQuestionRandom } from '#common/utils/question-getter/getQuestionRandom.js'
12-
11+
import { getAllQuestionList } from '#common/utils/question-getter/getAllQuestionList.js'
1312
import { easyCreateView } from '#common/view/create.view.js'
1413
import { description } from '#resources/text/description.js'
1514
import { DefaultVer } from '#common/constants/question.const.js'
1615
import { createQuestionById } from '#common/utils/cli-utils/createQuestion.js'
1716
import { create } from '#common/utils/cli-utils/create.js'
1817
import { commonMode } from '#common/utils/cli-utils/commonMode.js'
18+
import { setAllQuestion } from '#common/utils/store/controller/allQuestion.js'
1919

2020
const version = process.env.VERSION ?? DefaultVer
2121
program
@@ -29,9 +29,10 @@ program
2929
.option('-e, --easy', 'Use easy mode.')
3030
.option('-d, --directory <directory>', 'Set the question directory.')
3131
.option('-l, --language [language]', 'Set/Get the code language of question.')
32+
.option('-a, --all', 'Get all questions.')
3233
.option(
3334
'-u, --update',
34-
'Check the version to determine whether to update to the latest one.'
35+
'Check the version to determine whether to update to the latest one.',
3536
)
3637
.parse(process.argv)
3738

@@ -58,6 +59,13 @@ export const callModeAction = {
5859
identity: async (id) => {
5960
await createQuestionById(id, baseDir)
6061
process.exit(0)
62+
},
63+
all: async () => {
64+
const allQuestionData = await getAllQuestionList()
65+
for(const question of allQuestionData) {
66+
setAllQuestion(question)
67+
}
68+
process.exit(0)
6169
}
6270
}
6371
// 获取模式和参数

bin/lf.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ program
2020
.option('-l, --language [language]', 'Set/Get the code language of question.')
2121
.option(
2222
'-u, --update',
23-
'Check the version to determine whether to update to the latest one.'
23+
'Check the version to determine whether to update to the latest one.',
2424
)
2525
.parse(process.argv)
2626

bin/lk.js

+14-12
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ program
3131
.option('-t, --today', 'Check the question today.')
3232
.option(
3333
'-i, --identity <identity>',
34-
'Check the specified question by identity.'
34+
'Check the specified question by identity.',
3535
)
3636
.option('-r, --random', 'Check the last random question.')
3737
.option('-e, --easy', 'Use easy mode.')
3838
.option('-d, --directory <directory>', 'Set the question directory.')
3939
.option('-l, --language [language]', 'Set/Get the code language of question.')
4040
.option(
4141
'-u, --update',
42-
'Check the version to determine whether to update to the latest one.'
42+
'Check the version to determine whether to update to the latest one.',
4343
)
4444
.parse(process.argv)
4545

@@ -59,13 +59,14 @@ async function check(mode, question) {
5959
const filePath = path.join(
6060
baseDir,
6161
getQuestionFileName(question),
62-
`question${getQuestionFileExtension(question?.lang)}`
62+
`question${getQuestionFileExtension(question?.lang)}`,
6363
)
6464
if (!fs.existsSync(filePath)) {
6565
logger.info(`文件[${filePath}]不存在,请确保已经创建!`)
66-
} else {
66+
}
67+
else {
6768
logger.info(
68-
`MODE: ${mode}\n题目[${getQuestionChineseName(question)}]检查结果:`
69+
`MODE: ${mode}\n题目[${getQuestionChineseName(question)}]检查结果:`,
6970
)
7071
await checkQuestionByPath(filePath)
7172
}
@@ -89,13 +90,14 @@ const callModeAction = {
8990
// 如果未指定id说明是要检测模式创建的题目
9091
question = await getQuestionByMode(mode)
9192
await check('identity', question)
92-
} else {
93+
}
94+
else {
9395
question = await getFilePathById(id)
9496
const needToSelect = {
9597
type: 'list',
9698
name: 'need',
9799
message: `在当前目录下存在id为[${id}]的题目副本,请选择你要检查的副本:`,
98-
choices: []
100+
choices: [],
99101
}
100102
/**
101103
* 只检查一个题目
@@ -108,13 +110,13 @@ const callModeAction = {
108110
name: 'check',
109111
message: '当前题目目录中存在多个题目文件副本,请选择一个进行检查:',
110112
choices: [],
111-
default: null
113+
default: null,
112114
}
113115
let filePath
114116
switch (typeof fileOrFiles) {
115117
case 'undefined':
116118
logger.warn(
117-
`虽然在题目目录中,但当前目录下不存在[${id}]的题目文件!`
119+
`虽然在题目目录中,但当前目录下不存在[${id}]的题目文件!`,
118120
)
119121
process.exit(0)
120122
break
@@ -125,8 +127,8 @@ const callModeAction = {
125127
needToCheck.choices = fileOrFiles.map((o) => {
126128
return { name: o, value: o }
127129
})
128-
needToCheck.default = fileOrFiles?.find((o) =>
129-
o.endsWith(getQuestionFileExtension(DefaultLang))
130+
needToCheck.default = fileOrFiles?.find(o =>
131+
o.endsWith(getQuestionFileExtension(DefaultLang)),
130132
)
131133
filePath = await select(needToCheck)
132134
break
@@ -155,7 +157,7 @@ const callModeAction = {
155157
await checkOne(files)
156158
}
157159
process.exit(0)
158-
}
160+
},
159161
}
160162
// 执行指令分发
161163
callModeAction[mode](args)

common/origin/checkUpdate.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@ import { Day } from '#common/constants/date.const.js'
55
import {
66
NpmInstall,
77
PnpmInstall,
8-
YarnInstall
8+
YarnInstall,
99
} from '#common/constants/manager.const.js'
1010
import { PackageName } from '#common/constants/question.const.js'
1111

1212
const { timestamp, hasShow } = (await getStore('checkResult')) ?? {}
13-
if (Date.now() - timestamp <= Day || hasShow) process.exit(0)
13+
if (Date.now() - timestamp <= Day || hasShow)
14+
process.exit(0)
1415

1516
const { localVersion, npmVersion, isCliUpdate } = await checkUpdate()
1617
const needShow = false
1718
if (isCliUpdate) {
1819
const installInfo = [NpmInstall, YarnInstall, PnpmInstall]
19-
.map((fun) => fun(PackageName, true, true)) // 暂时先默认为全局
20+
.map(fun => fun(PackageName, true, true)) // 暂时先默认为全局
2021
.join('\n')
2122
logger.warn(
22-
`[leetcode-practice] 检测到新版本[ ${npmVersion} ]已经发布! 您当前的版本为[ ${localVersion} ]! 您可以执行对应的指令进行手动更新~`
23+
`[leetcode-practice] 检测到新版本[ ${npmVersion} ]已经发布! 您当前的版本为[ ${localVersion} ]! 您可以执行对应的指令进行手动更新~`,
2324
)
2425
logger.info(`${installInfo}`)
2526
}

common/structures/ListNode.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ export class ListNode {
1212
}
1313

1414
static parse(arr) {
15-
if (arr.length === 0) return null // Return null for an empty array
15+
if (arr.length === 0)
16+
return null // Return null for an empty array
1617

1718
const head = new ListNode(arr.shift(), null)
1819
let current = head
@@ -24,7 +25,8 @@ export class ListNode {
2425
}
2526

2627
static toArray(listNodes, arr = []) {
27-
if (listNodes === undefined || listNodes === null) return arr
28+
if (listNodes === undefined || listNodes === null)
29+
return arr
2830

2931
arr.push(listNodes.val)
3032
return ListNode.toArray(listNodes.next, arr)

common/structures/Node.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ export class Node {
3030
}
3131

3232
static toArray(node) {
33-
if (!node) return []
33+
if (!node)
34+
return []
3435

3536
const visited = new Set()
3637
const result = []
3738

3839
const dfs = (currentNode) => {
39-
if (visited.has(currentNode.val)) return
40+
if (visited.has(currentNode.val))
41+
return
4042

4143
const { neighbors, val } = currentNode
4244
visited.add(val)

common/structures/TreeNode.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ export class TreeNode {
77
}
88

99
static parse(arr) {
10-
if (arr.length === 0) return null
10+
if (arr.length === 0)
11+
return null
1112
const root = new TreeNode(arr[0])
1213
const queue = [root]
1314
for (let i = 1; i < arr.length; i += 2) {
@@ -26,7 +27,8 @@ export class TreeNode {
2627

2728
static toArray(treeNode) {
2829
const result = []
29-
if (!treeNode) return result
30+
if (!treeNode)
31+
return result
3032

3133
const queue = [treeNode]
3234

@@ -36,7 +38,8 @@ export class TreeNode {
3638
result.push(node.val)
3739
queue.push(node.left)
3840
queue.push(node.right)
39-
} else {
41+
}
42+
else {
4043
result.push(null)
4144
}
4245
}

common/utils/cli-utils/commonMode.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export async function commonMode(cmdOpts, easyCallback) {
2626
rootPath,
2727
currentEnv() === 'cli'
2828
? 'origin/checkUpdate.js'
29-
: 'common/origin/checkUpdate.js'
29+
: 'common/origin/checkUpdate.js',
3030
)
3131
fork(jsPath)
3232
// todo 监听额外线程的消息
@@ -46,7 +46,8 @@ export async function commonMode(cmdOpts, easyCallback) {
4646
if (cmdOpts.language) {
4747
if (cmdOpts.language !== true) {
4848
await easyLanguageView(cmdOpts.language)
49-
} else {
49+
}
50+
else {
5051
const lang = await getQuestionLanguage()
5152
logger.info(`当前CLI语言环境为:${lang}`)
5253
}

common/utils/cli-utils/create.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ export function create(mode, question, baseDir) {
2020
setQuestion(mode, question)
2121
const questionDir = path.join(baseDir, getQuestionFileName(question))
2222
createQuestion(question, questionDir).then(async (path) => {
23-
if (!path) path = await createQuestionCopy(question, questionDir)
23+
if (!path)
24+
path = await createQuestionCopy(question, questionDir)
2425
const line = (await getLineNumberByContent(path, '@QUESTION_START')) + 1
2526
logger.info(
26-
`题目[${getQuestionChineseName(question)}]获取成功!\n题目文件地址为:file://${path}:${line}`
27+
`题目[${getQuestionChineseName(question)}]获取成功!\n题目文件地址为:file://${path}:${line}`,
2728
)
2829
resolve(true)
2930
})

common/utils/cli-utils/createQuestion.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { create } from '#common/utils/cli-utils/create.js'
1111
*/
1212
export async function createQuestionByTitleSlug(
1313
titleSlug,
14-
baseDir = process.cwd()
14+
baseDir = process.cwd(),
1515
) {
1616
const { question } = await getQuestionIdBySlug(titleSlug)
1717
await createQuestionById(question.questionId, baseDir)
@@ -25,6 +25,7 @@ export async function createQuestionByTitleSlug(
2525
*/
2626
export async function createQuestionById(id, baseDir = process.cwd()) {
2727
const question = await getQuestionById(id)
28-
if (!question?.id) logger.warn(`指定编号: [ ${id} ] 题目不存在.`)
28+
if (!question?.id)
29+
logger.warn(`指定编号: [ ${id} ] 题目不存在.`)
2930
await create('identity', question, baseDir)
3031
}

common/utils/cli-utils/referMode.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
// 推测模式
22
export function referMode(args, opts) {
3-
if (args.length > 0 || opts.identity) return 'identity'
3+
if (args.length > 0 || opts.identity)
4+
return 'identity'
45

5-
if (opts.random) return 'random'
6+
if (opts.random)
7+
return 'random'
8+
9+
if (opts.all)
10+
return 'all'
611

712
return 'today'
813
}

common/utils/etc/createColorFont.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ export function createColorFont(font) {
77
const code = gradient_string([
88
{ color: '#ff0000', pos: 0 },
99
{ color: '#ffc600', pos: 0.5 },
10-
{ color: '#003dff', pos: 1 }
10+
{ color: '#003dff', pos: 1 },
1111
])(font)
1212
writeFileSync(path.resolve(process.cwd(), 'colorFont.js'), code)
1313
console.log(
14-
`[ColorFont]Create color font: ${font}\ncode location:${path.resolve(process.cwd(), 'colorFont.js')}`
14+
`[ColorFont]Create color font: ${font}\ncode location:${path.resolve(process.cwd(), 'colorFont.js')}`,
1515
)
1616
console.log(code)
1717
}

common/utils/etc/typeof_.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* @private
66
*/
77
export function typeof_(data) {
8-
if (data === null) return 'null'
8+
if (data === null)
9+
return 'null'
910
else return typeof data
1011
}

common/utils/file/getCountBySameName.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ import { readdirSync } from 'node:fs'
77
* @returns {number}
88
*/
99
export function getCountBySameName(dir, name) {
10-
return readdirSync(dir).filter((filename) => filename.includes(name)).length
10+
return readdirSync(dir).filter(filename => filename.includes(name)).length
1111
}
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { readdirSync } from 'node:fs'
22

33
export function getFileListBySameName(dir, name) {
4-
return readdirSync(dir).filter((filename) => filename.includes(name))
4+
return readdirSync(dir).filter(filename => filename.includes(name))
55
}

common/utils/file/getFilePathById.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import fs from 'node:fs'
88
*/
99
export function getFilePathById(id, baseDir = process.cwd()) {
1010
const dir = fs.readdirSync(baseDir)
11-
const files = dir.filter((o) => o.startsWith(`${id}.`))
12-
if (files.length > 1) return files
11+
const files = dir.filter(o => o.startsWith(`${id}.`))
12+
if (files.length > 1)
13+
return files
1314
return files?.[0]
1415
}

common/utils/file/getQuestionInDir.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import path from 'node:path'
99
export function getQuestionFileInDir(dir) {
1010
const list = fs.readdirSync(dir)
1111
const questionLikes = list
12-
.filter((name) => name.startsWith('question'))
13-
.map((file) => path.resolve(dir, file))
12+
.filter(name => name.startsWith('question'))
13+
.map(file => path.resolve(dir, file))
1414
return questionLikes?.length === 1 ? questionLikes[0] : questionLikes
1515
}

0 commit comments

Comments
 (0)