From 48e4d189b04013cacdabd5168c85bd82b7a615d8 Mon Sep 17 00:00:00 2001 From: devKobe24 Date: Sat, 19 Sep 2026 07:42:43 +0900 Subject: [PATCH 1/2] feat: add config command to show config path --- src/commands/config.ts | 36 ++++++++++++++++++ test/config-command.test.ts | 76 +++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 src/commands/config.ts create mode 100644 test/config-command.test.ts diff --git a/src/commands/config.ts b/src/commands/config.ts new file mode 100644 index 0000000..f1ed458 --- /dev/null +++ b/src/commands/config.ts @@ -0,0 +1,36 @@ +import {Flags} from '@oclif/core'; + +import HackMDCommand from '../command'; +import config from '../config'; +import {getConfigFilePath} from '../utils'; + +export default class Config extends HackMDCommand { + static description + = 'Show config file path and current configuration (credentials are never printed)'; + static examples = [ + `$ hackmd-cli config +Config file: /Users/you/.hackmd/config.json +hackmdAPIEndpointURL: https://api.hackmd.io/v1 +accessToken: (set)`, + `$ hackmd-cli config --path +/Users/you/.hackmd/config.json`, + ]; + static flags = { + help: Flags.help({char: 'h'}), + path: Flags.boolean({description: 'print only the config file path'}), + }; + + async run() { + const {flags} = await this.parse(Config); + const configFilePath = getConfigFilePath(); + + if (flags.path) { + this.log(configFilePath); + return; + } + + this.log(`Config file: ${configFilePath}`); + this.log(`hackmdAPIEndpointURL: ${config.hackmdAPIEndpointURL}`); + this.log(`accessToken: ${config.accessToken ? '(set)' : '(not set)'}`); + } +} diff --git a/test/config-command.test.ts b/test/config-command.test.ts new file mode 100644 index 0000000..12cf1b9 --- /dev/null +++ b/test/config-command.test.ts @@ -0,0 +1,76 @@ +import {expect} from 'chai'; +import path from 'node:path'; + +import {tempDir} from './utils'; + +const SECRET_TOKEN = 'super-secret-token-value'; + +function loadConfigCommand() { + // config is read at import time, so reload modules after setting env + delete require.cache[require.resolve('../src/config')]; + delete require.cache[require.resolve('../src/commands/config')]; + return require('../src/commands/config').default; +} + +async function runConfig( + flags: {path?: boolean}, + env: Record, +) { + const originalEnv = process.env; + process.env = {...env}; + + const lines: string[] = []; + try { + const ConfigCommand = loadConfigCommand(); + const command = { + log: (message = '') => lines.push(message), + parse: async () => ({flags}), + }; + await ConfigCommand.prototype.run.call(command); + } finally { + process.env = originalEnv; + } + + return lines.join('\n'); +} + +describe('Config command', () => { + it('prints the config file path', async () => { + const configDir = tempDir(); + const output = await runConfig({}, {HMD_CLI_CONFIG_DIR: configDir}); + + expect(output).to.include(path.join(configDir, 'config.json')); + }); + + it('never prints the access token value', async () => { + const output = await runConfig( + {}, + { + HMD_API_ACCESS_TOKEN: SECRET_TOKEN, + HMD_CLI_CONFIG_DIR: tempDir(), + }, + ); + + expect(output).to.not.include(SECRET_TOKEN); + expect(output).to.include('accessToken: (set)'); + }); + + it('shows (not set) when no access token is configured', async () => { + const output = await runConfig({}, {HMD_CLI_CONFIG_DIR: tempDir()}); + + expect(output).to.include('accessToken: (not set)'); + }); + + it('prints only the path with --path', async () => { + const configDir = tempDir(); + const output = await runConfig( + {path: true}, + { + HMD_API_ACCESS_TOKEN: SECRET_TOKEN, + HMD_CLI_CONFIG_DIR: configDir, + }, + ); + + expect(output).to.equal(path.join(configDir, 'config.json')); + }); +}); From 44221d9cfa07b40310083d9386988d3be4506f93 Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Tue, 24 Nov 2026 17:13:57 +0800 Subject: [PATCH 2/2] style: match repo formatting in config command; document it in README Agent: T3 Code (Claude Opus 5.5) Co-Authored-By: Claude Opus 5.5 --- README.md | 2 ++ src/commands/config.ts | 28 ++++++++--------- test/config-command.test.ts | 62 ++++++++++++++++++------------------- 3 files changed, 47 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index 195dfd7..840919d 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,8 @@ Or in JSON file (`~/.hackmd/config.json`): ### Configuration Reference +Run `hackmd-cli config` to see which config file is in use and the current settings (the access token value is never printed), or `hackmd-cli config --path` to print only the config file path. + All available configurations are listed in the table below. | Config key | Environment Variable | Data Type | Example Value | Description | diff --git a/src/commands/config.ts b/src/commands/config.ts index f1ed458..6262513 100644 --- a/src/commands/config.ts +++ b/src/commands/config.ts @@ -1,12 +1,12 @@ -import {Flags} from '@oclif/core'; +import {Flags} from '@oclif/core' -import HackMDCommand from '../command'; -import config from '../config'; -import {getConfigFilePath} from '../utils'; +import HackMDCommand from '../command' +import config from '../config' +import {getConfigFilePath} from '../utils' export default class Config extends HackMDCommand { static description - = 'Show config file path and current configuration (credentials are never printed)'; + = 'Show config file path and current configuration (credentials are never printed)' static examples = [ `$ hackmd-cli config Config file: /Users/you/.hackmd/config.json @@ -14,23 +14,23 @@ hackmdAPIEndpointURL: https://api.hackmd.io/v1 accessToken: (set)`, `$ hackmd-cli config --path /Users/you/.hackmd/config.json`, - ]; + ] static flags = { help: Flags.help({char: 'h'}), path: Flags.boolean({description: 'print only the config file path'}), - }; + } async run() { - const {flags} = await this.parse(Config); - const configFilePath = getConfigFilePath(); + const {flags} = await this.parse(Config) + const configFilePath = getConfigFilePath() if (flags.path) { - this.log(configFilePath); - return; + this.log(configFilePath) + return } - this.log(`Config file: ${configFilePath}`); - this.log(`hackmdAPIEndpointURL: ${config.hackmdAPIEndpointURL}`); - this.log(`accessToken: ${config.accessToken ? '(set)' : '(not set)'}`); + this.log(`Config file: ${configFilePath}`) + this.log(`hackmdAPIEndpointURL: ${config.hackmdAPIEndpointURL}`) + this.log(`accessToken: ${config.accessToken ? '(set)' : '(not set)'}`) } } diff --git a/test/config-command.test.ts b/test/config-command.test.ts index 12cf1b9..202f9c8 100644 --- a/test/config-command.test.ts +++ b/test/config-command.test.ts @@ -1,46 +1,46 @@ -import {expect} from 'chai'; -import path from 'node:path'; +import {expect} from 'chai' +import path from 'node:path' -import {tempDir} from './utils'; +import {tempDir} from './utils' -const SECRET_TOKEN = 'super-secret-token-value'; +const SECRET_TOKEN = 'super-secret-token-value' function loadConfigCommand() { // config is read at import time, so reload modules after setting env - delete require.cache[require.resolve('../src/config')]; - delete require.cache[require.resolve('../src/commands/config')]; - return require('../src/commands/config').default; + delete require.cache[require.resolve('../src/config')] + delete require.cache[require.resolve('../src/commands/config')] + return require('../src/commands/config').default } async function runConfig( flags: {path?: boolean}, env: Record, ) { - const originalEnv = process.env; - process.env = {...env}; + const originalEnv = process.env + process.env = {...env} - const lines: string[] = []; + const lines: string[] = [] try { - const ConfigCommand = loadConfigCommand(); + const ConfigCommand = loadConfigCommand() const command = { log: (message = '') => lines.push(message), parse: async () => ({flags}), - }; - await ConfigCommand.prototype.run.call(command); + } + await ConfigCommand.prototype.run.call(command) } finally { - process.env = originalEnv; + process.env = originalEnv } - return lines.join('\n'); + return lines.join('\n') } describe('Config command', () => { it('prints the config file path', async () => { - const configDir = tempDir(); - const output = await runConfig({}, {HMD_CLI_CONFIG_DIR: configDir}); + const configDir = tempDir() + const output = await runConfig({}, {HMD_CLI_CONFIG_DIR: configDir}) - expect(output).to.include(path.join(configDir, 'config.json')); - }); + expect(output).to.include(path.join(configDir, 'config.json')) + }) it('never prints the access token value', async () => { const output = await runConfig( @@ -49,28 +49,28 @@ describe('Config command', () => { HMD_API_ACCESS_TOKEN: SECRET_TOKEN, HMD_CLI_CONFIG_DIR: tempDir(), }, - ); + ) - expect(output).to.not.include(SECRET_TOKEN); - expect(output).to.include('accessToken: (set)'); - }); + expect(output).to.not.include(SECRET_TOKEN) + expect(output).to.include('accessToken: (set)') + }) it('shows (not set) when no access token is configured', async () => { - const output = await runConfig({}, {HMD_CLI_CONFIG_DIR: tempDir()}); + const output = await runConfig({}, {HMD_CLI_CONFIG_DIR: tempDir()}) - expect(output).to.include('accessToken: (not set)'); - }); + expect(output).to.include('accessToken: (not set)') + }) it('prints only the path with --path', async () => { - const configDir = tempDir(); + const configDir = tempDir() const output = await runConfig( {path: true}, { HMD_API_ACCESS_TOKEN: SECRET_TOKEN, HMD_CLI_CONFIG_DIR: configDir, }, - ); + ) - expect(output).to.equal(path.join(configDir, 'config.json')); - }); -}); + expect(output).to.equal(path.join(configDir, 'config.json')) + }) +})