diff --git a/lib/config.js b/lib/config.js index 8aba23e..58a64a1 100644 --- a/lib/config.js +++ b/lib/config.js @@ -1,9 +1,13 @@ const homedir = process.env.HOME || require('node:os').homedir(); const path = require('node:path'); const fs = require('node:fs'); +const chalk = require('chalk'); const configDir = path.resolve(homedir, '.config', 'fanyi'); const configPath = path.resolve(configDir, '.fanyirc'); +// 初始化一个带颜色的 chalk 实例 +const chalkInstance = new chalk.Instance({ level: 3 }); + const config = { async load(path = configPath) { const emptyObj = {}; @@ -21,9 +25,14 @@ const config = { async write(options = {}, path = configPath) { const defaultOptions = await config.load(path); const mergedOptions = { ...defaultOptions, ...options }; - const content = JSON.stringify(mergedOptions); + const content = JSON.stringify(mergedOptions, null, 2); fs.existsSync(configDir) || fs.mkdirSync(configDir, { recursive: true }); - return fs.writeFileSync(path, content); + fs.writeFileSync(path, content); + console.log( + `${chalkInstance.bgGreen(JSON.stringify(options))} config saved at ${chalkInstance.gray(path)}:`, + ); + console.log(); + console.log(chalkInstance.greenBright(content)); }, }; diff --git a/lib/searchHistory.js b/lib/searchHistory.js index cd707b8..d5188ed 100644 --- a/lib/searchHistory.js +++ b/lib/searchHistory.js @@ -43,6 +43,9 @@ function getDaySplit(someDay) { exports.searchList = (args) => { const { someDay, recentDays, showFile } = args; + console.log(); + console.log(chalk.gray('fanyi history:')); + console.log(); let targetContent; // 与配置放在一起 fs.ensureFileSync(searchFilePath); diff --git a/tests/__snapshots__/index.test.ts.snap b/tests/__snapshots__/index.test.ts.snap index 771cda2..fb1134d 100644 --- a/tests/__snapshots__/index.test.ts.snap +++ b/tests/__snapshots__/index.test.ts.snap @@ -1,50 +1,5 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`fanyi CLI > should be able to config global options 1`] = ` -" - config --no-color ~ iciba.com - - ----- - -" -`; - -exports[`fanyi CLI > should be able to config global options 2`] = ` -" - config --color ~ iciba.com - - ----- - -" -`; - -exports[`fanyi CLI > should be able to config global options 3`] = ` -" - config --no-iciba ~ iciba.com - - ----- - -" -`; - -exports[`fanyi CLI > should be able to config global options 4`] = ` -" - config --iciba ~ iciba.com - - ----- - -" -`; - -exports[`fanyi CLI > should print config list if config list is given 1`] = ` -" - config list ~ iciba.com - - ----- - -" -`; - exports[`fanyi CLI > should print help if -h is given 1`] = ` "Usage: fanyi [options] [command] @@ -66,37 +21,6 @@ Examples: " `; -exports[`fanyi CLI > should print search history 1`] = ` -"2024-09-11 - xx - abbr. without securities or warrants 无权或无保障(代号); - - love - vt.& vi. 爱,热爱;爱戴;喜欢;赞美,称赞; - vt. 喜爱;喜好;喜欢;爱慕; - n. 爱情,爱意;疼爱;热爱;爱人,所爱之物; - word - n. 单词;话语;诺言;消息; - vt. 措辞,用词;用言语表达; - vi. 讲话; - config - - hello - int. 哈喽,喂;你好,您好;表示问候;打招呼; - n. “喂”的招呼声或问候声; - vi. 喊“喂”; - helxlo - - config list - - config --no-color - - config --color - - -" -`; - exports[`fanyi CLI > should print translation of the word 1`] = ` " hello 英[ hə'ləʊ ] 美[ həˈloʊ ] ~ iciba.com diff --git a/tests/index.test.ts b/tests/index.test.ts index e06469c..a11872e 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -46,18 +46,18 @@ describe('fanyi CLI', () => { }); it('should be able to config global options', async () => { - const { stdout } = await runScript(['config --no-color']); - expect(stdout).toMatchSnapshot(); - const { stdout: stdout2 } = await runScript(['config --color']); - expect(stdout2).toMatchSnapshot(); - const { stdout: stdout3 } = await runScript(['config --no-iciba']); - expect(stdout3).toMatchSnapshot(); - const { stdout: stdout4 } = await runScript(['config --iciba']); - expect(stdout4).toMatchSnapshot(); + const { stdout } = await runScript(['config', '--no-color']); + expect(stdout).toContain('{"color":false}'); + const { stdout: stdout2 } = await runScript(['config', '--color']); + expect(stdout2).toContain('{"color":true}'); + const { stdout: stdout3 } = await runScript(['config', '--no-iciba']); + expect(stdout3).toContain('{"iciba":false}'); + const { stdout: stdout4 } = await runScript(['config', '--iciba']); + expect(stdout4).toContain('{"iciba":true}'); }); it('should print search history', async () => { const { stdout } = await runScript(['list']); - expect(stdout).toMatchSnapshot(); + expect(stdout).toContain('fanyi history:'); }); });