Skip to content

Commit 9d8b5ab

Browse files
committed
feat:finder add baseDir
1 parent e99e76b commit 9d8b5ab

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

bin/lf.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#! /usr/bin/env node
2+
import path from 'node:path'
23
import { program } from 'commander'
34
import { description } from '#resources/text/description.js'
45
import { artFontLogo } from '#resources/text/art-font-logo.js'
@@ -17,6 +18,7 @@ program
1718
.description(`${description}\n${artFontLogo}\n${aim}`)
1819
.addHelpText('after', lfExamples + love)
1920
.option('-l, --language [language]', 'Set/Get the code language of question.')
21+
.option('-d, --directory <directory>', 'Set the question directory.')
2022
.option(
2123
'-u, --update',
2224
'Check the version to determine whether to update to the latest one.'
@@ -40,11 +42,15 @@ if (cmdOpts.language) {
4042
}
4143
process.exit(0)
4244
}
45+
// 根据dir 参数来设置基本目录
46+
const baseDir = cmdOpts.directory
47+
? path.join(process.cwd(), cmdOpts.directory)
48+
: process.cwd()
4349
// 检测更新
4450
if (cmdOpts.update) {
4551
await easyUpdateView()
4652
process.exit(0)
4753
}
4854
// 进入视图操作
49-
await easyFinderView()
55+
await easyFinderView(baseDir)
5056
process.exit(0)

common/view/finder.view.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from '#common/utils/create-check/createUtil.js'
1212
import { getQuestionByKeyword } from '#common/utils/question-getter/getQuestionByKeyword.js'
1313

14-
async function hotMode() {
14+
async function hotMode(baseDir = process.cwd()) {
1515
const createMode = await select({
1616
message: '拉题模式',
1717
choices: [
@@ -30,12 +30,12 @@ async function hotMode() {
3030
}
3131
const singleChoice = await select(singleMode)
3232

33-
await createQuestionByTitleSlug(singleChoice)
33+
await createQuestionByTitleSlug(singleChoice, baseDir)
3434
}
3535
if (createMode === 'all') await getHot100QuestionListCode()
3636
}
3737

38-
async function keywordMode() {
38+
async function keywordMode(baseDir = process.cwd()) {
3939
const keyword = await input({ message: '请输入关键词', name: 'keyword' })
4040
const data = await getQuestionByKeyword(keyword)
4141
const list = data?.map((q) => {
@@ -52,11 +52,11 @@ async function keywordMode() {
5252
}
5353
const chooseQuestion = await select(listQuestion)
5454
console.log(chooseQuestion)
55-
await createQuestionById(chooseQuestion, process.cwd())
55+
await createQuestionById(chooseQuestion, baseDir)
5656
}
57-
async function selectMode() {}
57+
async function selectMode(baseDir) {}
5858

59-
export async function easyFinderView() {
59+
export async function easyFinderView(baseDir = process.cwd()) {
6060
const choices = [
6161
{ name: '关键词搜索', value: 'keyword', description: '关键词描述' },
6262
{
@@ -78,5 +78,5 @@ export async function easyFinderView() {
7878
keyword: keywordMode,
7979
select: selectMode
8080
}
81-
await modeMap[mode]()
81+
await modeMap[mode](baseDir)
8282
}

test/create.spec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import { generateTemplateContent } from '#common/utils/question-handler/fulfillQ
55

66
vi.mock('fs/promises', () => {
77
return {
8-
writeFile: vi.fn()
8+
writeFile: vi.fn(),
99
}
1010
})
1111
const funRegex = /var\s+(\w+)\s*=\s*function\s*\(([^)]*)\)\s*{\s*([^}]*)\s*}/
12-
const isContainJsCode = (input) => funRegex.test(input)
13-
const isContainTestCase = (input) => input.includes('showLogs(')
12+
const isContainJsCode = input => funRegex.test(input)
13+
const isContainTestCase = input => input.includes('showLogs(')
1414

1515
const mockKeys = ['id', 'slug', 'title', 'detail', 'lang', 'code', 'date']
1616

0 commit comments

Comments
 (0)