Skip to content

Commit 4fc7795

Browse files
committed
chore(cli): add config file output in terminal
1 parent 3b576c8 commit 4fc7795

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

packages/cli/src/config/helpers.test.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { CONFIG_FILE_NAME, HIDDEN_CONFIG_DIR, HIDDEN_CONFIG_FILE_NAME, loadConfi
44

55
const existsSyncMock = vi.hoisted(() => vi.fn());
66
const loadConfigMock = vi.hoisted(() => vi.fn());
7-
const supportedExtensions = vi.hoisted(() => ['json', 'yaml', 'ts']);
7+
const konsolaInfoMock = vi.hoisted(() => vi.fn());
8+
const supportedExtensions = vi.hoisted(() => ['.json', '.yaml', '.ts']);
89

910
vi.mock('node:os', () => ({
1011
homedir: () => '/home/tester',
@@ -19,6 +20,12 @@ vi.mock('c12', () => ({
1920
loadConfig: (options: any) => loadConfigMock(options),
2021
}));
2122

23+
vi.mock('../utils/konsola', () => ({
24+
konsola: {
25+
info: konsolaInfoMock,
26+
},
27+
}));
28+
2229
const HOME_DIR = '/home/tester';
2330
const WORKSPACE_DIR = '/workspace/project';
2431
const HOME_CONFIG_DIR = resolvePath(HOME_DIR, HIDDEN_CONFIG_DIR);
@@ -34,7 +41,7 @@ function registerDirectory(path: string): void {
3441
}
3542

3643
function addConfigFile(dir: string, baseFile: string, extension: string): void {
37-
files.add(resolvePath(dir, `${baseFile}.${extension}`));
44+
files.add(resolvePath(dir, `${baseFile}${extension}`));
3845
}
3946

4047
function resetFilesystem(): void {
@@ -59,13 +66,14 @@ afterEach(() => {
5966
cwdSpy?.mockRestore();
6067
loadConfigMock.mockReset();
6168
existsSyncMock.mockReset();
69+
konsolaInfoMock.mockReset();
6270
});
6371

6472
describe('loadConfigLayers', () => {
6573
it('loads configs from home, workspace hidden dir, and project root honoring priority and supported extensions', async () => {
66-
addConfigFile(HOME_CONFIG_DIR, HIDDEN_CONFIG_FILE_NAME, 'json');
67-
addConfigFile(LOCAL_CONFIG_DIR, HIDDEN_CONFIG_FILE_NAME, 'yaml');
68-
addConfigFile(WORKSPACE_DIR, CONFIG_FILE_NAME, 'ts');
74+
addConfigFile(HOME_CONFIG_DIR, HIDDEN_CONFIG_FILE_NAME, '.json');
75+
addConfigFile(LOCAL_CONFIG_DIR, HIDDEN_CONFIG_FILE_NAME, '.yaml');
76+
addConfigFile(WORKSPACE_DIR, CONFIG_FILE_NAME, '.ts');
6977

7078
responses.set(HOME_CONFIG_DIR, { scope: 'home', ext: 'json' });
7179
responses.set(LOCAL_CONFIG_DIR, { scope: 'workspace-hidden', ext: 'yaml' });
@@ -85,6 +93,12 @@ describe('loadConfigLayers', () => {
8593
`${LOCAL_CONFIG_DIR}:${HIDDEN_CONFIG_FILE_NAME}`,
8694
`${WORKSPACE_DIR}:${CONFIG_FILE_NAME}`,
8795
]);
96+
expect(konsolaInfoMock).toHaveBeenCalledTimes(3);
97+
expect(konsolaInfoMock.mock.calls.map(([message]: [string]) => message)).toEqual([
98+
`Loaded Storyblok config: ${resolvePath(HOME_CONFIG_DIR, `${HIDDEN_CONFIG_FILE_NAME}.json`)}`,
99+
`Loaded Storyblok config: ${resolvePath(LOCAL_CONFIG_DIR, `${HIDDEN_CONFIG_FILE_NAME}.yaml`)}`,
100+
`Loaded Storyblok config: ${resolvePath(WORKSPACE_DIR, `${CONFIG_FILE_NAME}.ts`)}`,
101+
]);
88102

89103
const expectedCandidates = [
90104
resolvePath(HOME_CONFIG_DIR, `${HIDDEN_CONFIG_FILE_NAME}.json`),
@@ -100,7 +114,7 @@ describe('loadConfigLayers', () => {
100114
});
101115

102116
it('skips locations that do not expose a supported config file', async () => {
103-
addConfigFile(WORKSPACE_DIR, CONFIG_FILE_NAME, 'ts');
117+
addConfigFile(WORKSPACE_DIR, CONFIG_FILE_NAME, '.ts');
104118
responses.set(WORKSPACE_DIR, { scope: 'workspace-root', ext: 'ts' });
105119

106120
const layers = await loadConfigLayers();
@@ -111,5 +125,17 @@ describe('loadConfigLayers', () => {
111125
cwd: WORKSPACE_DIR,
112126
configFile: CONFIG_FILE_NAME,
113127
}));
128+
expect(konsolaInfoMock).toHaveBeenCalledTimes(1);
129+
expect(konsolaInfoMock).toHaveBeenCalledWith(
130+
`Loaded Storyblok config: ${resolvePath(WORKSPACE_DIR, `${CONFIG_FILE_NAME}.ts`)}`,
131+
expect.any(Object),
132+
);
133+
});
134+
135+
it('logs fallback info when no config layers are detected', async () => {
136+
const layers = await loadConfigLayers();
137+
138+
expect(layers).toEqual([]);
139+
expect(konsolaInfoMock).toHaveBeenCalledWith('No Storyblok config files found. Falling back to defaults.');
114140
});
115141
});

packages/cli/src/config/helpers.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { homedir } from 'node:os';
44
import { loadConfig, SUPPORTED_EXTENSIONS } from 'c12';
55
import { toCamelCase } from '../utils/format';
66
import { isPlainObject } from '../utils/object';
7+
import { konsola } from '../utils/konsola';
78
import type {
89
CommanderCommand,
910
CommanderOption,
@@ -78,20 +79,25 @@ export function getOptionPath(option: CommanderOption): string[] {
7879
.map(segment => toCamelCase(segment));
7980
}
8081

81-
function hasSupportedConfigFile(cwd: string, configFile: string): boolean {
82-
return SUPPORTED_EXTENSIONS.some((ext) => {
83-
const candidate = resolvePath(cwd, `${configFile}.${ext}`);
84-
return existsSync(candidate);
85-
});
82+
function resolveConfigFilePath(cwd: string, configFile: string): string | null {
83+
for (const ext of SUPPORTED_EXTENSIONS) {
84+
const candidate = resolvePath(cwd, `${configFile}${ext}`);
85+
if (existsSync(candidate)) {
86+
return candidate;
87+
}
88+
}
89+
return null;
8690
}
8791

8892
async function loadConfigLayer({ cwd, configFile }: ConfigLocation): Promise<Record<string, any> | null> {
8993
if (!existsSync(cwd)) {
9094
return null;
9195
}
92-
if (!hasSupportedConfigFile(cwd, configFile)) {
96+
const filePath = resolveConfigFilePath(cwd, configFile);
97+
if (!filePath) {
9398
return null;
9499
}
100+
konsola.info(`Loaded Storyblok config: ${filePath}`, { margin: false });
95101
const { config } = await loadConfig({
96102
name: 'storyblok',
97103
cwd,
@@ -130,5 +136,8 @@ export async function loadConfigLayers(): Promise<Record<string, any>[]> {
130136
layers.push(layer);
131137
}
132138
}
139+
if (!layers.length) {
140+
konsola.info('No Storyblok config files found. Falling back to defaults.');
141+
}
133142
return layers;
134143
}

0 commit comments

Comments
 (0)