Skip to content

Commit aaabe89

Browse files
committed
feat: refactor genTypes to export individual config types
1 parent 86dcd7e commit aaabe89

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed

packages/cli/utils/gen.ts

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,41 +69,61 @@ export const KeyFormatPatterns: Record<KeyFormat, KeyFormatPattern> = {
6969
},
7070
};
7171

72+
function indentLines(str: string, indent = 2, lineBreak = "\n"): string {
73+
const indentStr = " ".repeat(indent);
74+
return str
75+
.split(lineBreak)
76+
.map((line) => `${indentStr}${line}`)
77+
.join(lineBreak);
78+
}
79+
7280
export function genFeatureKey(input: string, format: KeyFormat): string {
7381
return KeyFormatPatterns[format].transform(input);
7482
}
7583

7684
export function genRemoteConfig(remoteConfigs?: RemoteConfig[]) {
7785
const variants = remoteConfigs?.[0]?.variants;
78-
if (!variants?.length) return "boolean";
79-
const type = JSONToType(
86+
if (!variants?.length) return;
87+
return JSONToType(
8088
remoteConfigs![0].variants?.map(({ variant: { payload } }) => ({
8189
config: { payload },
8290
})),
8391
);
84-
// Indent all but first line to 4 spaces
85-
return type
86-
?.split("\n")
87-
.map((line, i) => (i === 0 ? line : " " + line))
88-
.join("\n");
8992
}
9093

9194
export function genTypes(features: Feature[], format: GenFormat = "react") {
95+
const configDefs = new Map<string, { name: string; definition: string }>();
96+
features.forEach(({ key, name, remoteConfigs }) => {
97+
const definition = genRemoteConfig(remoteConfigs);
98+
if (!definition) return;
99+
const configName = `${pascalCase(name)}Config`;
100+
configDefs.set(key, { name: configName, definition });
101+
});
102+
92103
return /* ts */ `
93104
// DO NOT EDIT THIS FILE. IT IS GENERATED BY THE BUCKET CLI AND WILL BE OVERWRITTEN.
94105
// eslint-disable
95106
// prettier-ignore
96107
import "@bucketco/${format}-sdk";
97108
98109
declare module "@bucketco/${format}-sdk" {
99-
interface Features {
110+
export interface Features {
100111
${features
101-
.map(
102-
({ key, remoteConfigs }) =>
103-
` "${key}": ${genRemoteConfig(remoteConfigs)};`,
104-
)
112+
.map(({ key }) => {
113+
const config = configDefs.get(key);
114+
return indentLines(
115+
`"${key}": ${config?.definition ? config.name : "boolean"};`,
116+
4,
117+
);
118+
})
105119
.join("\n")}
106120
}
121+
122+
${Array.from(configDefs.values())
123+
.map(({ name, definition }) => {
124+
return indentLines(`export type ${name} = ${definition}`);
125+
})
126+
.join("\n\n")}
107127
}
108128
`.trim();
109129
}

0 commit comments

Comments
 (0)