Skip to content

Commit 3b576c8

Browse files
committed
feat(cli): add type helpers for user's config file
1 parent 04727e5 commit 3b576c8

File tree

5 files changed

+107
-1
lines changed

5 files changed

+107
-1
lines changed

packages/cli/build.config.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ import { defineBuildConfig } from 'unbuild';
22

33
export default defineBuildConfig({
44
declaration: true,
5-
entries: ['./src/index'],
5+
entries: [
6+
'./src/index',
7+
{
8+
input: './src/entrypoints/config',
9+
name: 'config/index',
10+
},
11+
],
612
failOnWarn: false,
713
sourcemap: true,
814
});

packages/cli/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,18 @@
1818
"node",
1919
"javascript"
2020
],
21+
"exports": {
22+
".": {
23+
"types": "./dist/index.d.ts",
24+
"import": "./dist/index.mjs"
25+
},
26+
"./config": {
27+
"types": "./dist/config/index.d.ts",
28+
"import": "./dist/config/index.mjs"
29+
}
30+
},
2131
"main": "./dist/index.mjs",
32+
"types": "./dist/index.d.ts",
2233
"bin": {
2334
"storyblok": "./dist/index.mjs"
2435
},

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Command } from 'commander';
33
import { resolveConfig } from './resolver';
44
import { applyConfigToCommander } from './commander';
55
import { DEFAULT_GLOBAL_CONFIG } from './defaults';
6+
import { defineConfig } from './types';
67
import * as helpers from './helpers';
78

89
interface CommandHierarchy {
@@ -168,3 +169,21 @@ describe('config resolver', () => {
168169
expect(pull.getOptionValue('suffix')).toBe('resolved');
169170
});
170171
});
172+
173+
describe('defineConfig', () => {
174+
it('returns the same config reference for typed authoring', () => {
175+
const input = {
176+
region: 'us',
177+
modules: {
178+
components: {
179+
path: '.storyblok',
180+
pull: {
181+
separateFiles: true,
182+
},
183+
},
184+
},
185+
} as const;
186+
const result = defineConfig(input);
187+
expect(result).toBe(input);
188+
});
189+
});

packages/cli/src/config/types.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import type { RegionCode } from '../constants';
22
import type { Command, Option } from 'commander';
3+
import type { CommandOptions } from '../types';
4+
import type { PullComponentsOptions } from '../commands/components/pull/constants';
5+
import type { PushComponentsOptions } from '../commands/components/push/constants';
6+
import type { PullDatasourcesOptions } from '../commands/datasources/pull/constants';
7+
import type { PushDatasourcesOptions } from '../commands/datasources/push/constants';
8+
import type { MigrationsGenerateOptions } from '../commands/migrations/generate/constants';
9+
import type { MigrationsRunOptions } from '../commands/migrations/run/constants';
10+
import type { GenerateTypesOptions } from '../commands/types/generate/constants';
311

412
export type PlainObject = Record<string, any>;
513

@@ -41,6 +49,59 @@ export interface ResolvedCliConfig extends GlobalConfig {
4149
[key: string]: any;
4250
}
4351

52+
type StripCommandOption<T> = T extends CommandOptions ? Omit<T, keyof CommandOptions> : T;
53+
type CommandConfig<T> = Partial<StripCommandOption<T>>;
54+
55+
export interface ComponentsModuleConfig extends Record<string, unknown> {
56+
space?: string;
57+
path?: string;
58+
pull?: CommandConfig<PullComponentsOptions>;
59+
push?: CommandConfig<PushComponentsOptions>;
60+
}
61+
62+
export interface DatasourcesModuleConfig extends Record<string, unknown> {
63+
space?: string;
64+
path?: string;
65+
pull?: CommandConfig<PullDatasourcesOptions>;
66+
push?: CommandConfig<PushDatasourcesOptions>;
67+
}
68+
69+
export interface MigrationsModuleConfig extends Record<string, unknown> {
70+
space?: string;
71+
path?: string;
72+
generate?: CommandConfig<MigrationsGenerateOptions>;
73+
run?: CommandConfig<MigrationsRunOptions>;
74+
}
75+
76+
export interface TypesModuleConfig extends Record<string, unknown> {
77+
space?: string;
78+
path?: string;
79+
generate?: CommandConfig<GenerateTypesOptions>;
80+
}
81+
82+
export type ModulesConfig = Record<string, PlainObject> & {
83+
components?: ComponentsModuleConfig;
84+
datasources?: DatasourcesModuleConfig;
85+
migrations?: MigrationsModuleConfig;
86+
types?: TypesModuleConfig;
87+
};
88+
89+
export type DeepPartial<T> = {
90+
[K in keyof T]?: T[K] extends (...args: any[]) => any
91+
? T[K]
92+
: T[K] extends object
93+
? DeepPartial<T[K]>
94+
: T[K];
95+
};
96+
97+
export interface StoryblokConfig extends DeepPartial<GlobalConfig> {
98+
modules?: ModulesConfig;
99+
}
100+
101+
export function defineConfig<T extends StoryblokConfig>(config: T): T {
102+
return config;
103+
}
104+
44105
export type OptionParser<T> = (value: string, previous?: T) => T;
45106

46107
export interface GlobalOptionDefinition<T = unknown> {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export { defineConfig } from '../config/types';
2+
export type {
3+
ComponentsModuleConfig,
4+
DatasourcesModuleConfig,
5+
MigrationsModuleConfig,
6+
ModulesConfig,
7+
StoryblokConfig,
8+
TypesModuleConfig,
9+
} from '../config/types';

0 commit comments

Comments
 (0)