Repository navigation
Expand file tree
/
Copy pathgen.ts
More file actions
148 lines (129 loc) · 4.14 KB
/
Copy pathgen.ts
File metadata and controls
148 lines (129 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { mkdir, writeFile } from "node:fs/promises";
import { dirname, isAbsolute, join } from "node:path";
import { camelCase, kebabCase, pascalCase, snakeCase } from "change-case";
import { Flag, RemoteConfig } from "../services/flags.js";
import { JSONToType, quoteKey } from "./json.js";
export type GenFormat = "react" | "node";
// Keep in sync with Reflag main repo
export const KeyFormats = [
"custom",
"pascalCase",
"camelCase",
"snakeCaseUpper",
"snakeCaseLower",
"kebabCaseUpper",
"kebabCaseLower",
] as const;
export type KeyFormat = (typeof KeyFormats)[number];
type KeyFormatPattern = {
transform: (key: string) => string;
regex: RegExp;
message: string;
};
export const KeyFormatPatterns: Record<KeyFormat, KeyFormatPattern> = {
custom: {
transform: (key) => key?.trim(),
regex: /^[\p{L}\p{N}\p{P}\p{S}\p{Z}]+$/u,
message:
"Key must contain only letters, numbers, punctuation, symbols, or spaces.",
},
pascalCase: {
transform: (key) => pascalCase(key),
regex: /^[\p{Lu}][\p{L}\p{N}]*$/u,
message:
"Key must start with uppercase letter and contain only letters and numbers.",
},
camelCase: {
transform: (key) => camelCase(key),
regex: /^[\p{Ll}][\p{L}\p{N}]*$/u,
message:
"Key must start with lowercase letter and contain only letters and numbers.",
},
snakeCaseUpper: {
transform: (key) => snakeCase(key).toUpperCase(),
regex: /^[\p{Lu}][\p{Lu}\p{N}]*(?:_[\p{Lu}\p{N}]+)*$/u,
message: "Key must be uppercase with words separated by underscores.",
},
snakeCaseLower: {
transform: (key) => snakeCase(key).toLowerCase(),
regex: /^[\p{Ll}][\p{Ll}\p{N}]*(?:_[\p{Ll}\p{N}]+)*$/u,
message: "Key must be lowercase with words separated by underscores.",
},
kebabCaseUpper: {
transform: (key) => kebabCase(key).toUpperCase(),
regex: /^[\p{Lu}][\p{Lu}\p{N}]*(?:-[\p{Lu}\p{N}]+)*$/u,
message: "Key must be uppercase with words separated by hyphens.",
},
kebabCaseLower: {
transform: (key) => kebabCase(key).toLowerCase(),
regex: /^[\p{Ll}][\p{Ll}\p{N}]*(?:-[\p{Ll}\p{N}]+)*$/u,
message: "Key must be lowercase with words separated by hyphens.",
},
};
export function indentLines(
str: string,
indent = 2,
lineBreak = "\n",
trim = false,
): string {
const indentStr = " ".repeat(indent);
return str
.split(lineBreak)
.map((line) => `${indentStr}${trim ? line.trim() : line}`)
.join(lineBreak);
}
export function genFlagKey(input: string, format: KeyFormat): string {
return KeyFormatPatterns[format].transform(input);
}
export function genRemoteConfig(remoteConfigs?: RemoteConfig[]) {
const variants = remoteConfigs?.[0]?.variants;
if (!variants?.length) return;
return JSONToType(
remoteConfigs![0].variants?.map(({ variant: { payload } }) => payload),
);
}
export function genTypes(flags: Flag[], format: GenFormat = "react") {
const configDefs = new Map<string, { name: string; definition: string }>();
flags.forEach(({ key, name, remoteConfigs }) => {
const definition = genRemoteConfig(remoteConfigs);
if (!definition) {
return;
}
const configName = `${pascalCase(name)}ConfigPayload`;
configDefs.set(key, { name: configName, definition });
});
return /* ts */ `
// DO NOT EDIT THIS FILE. IT IS GENERATED BY THE REFLAG CLI AND WILL BE OVERWRITTEN.
// eslint-disable
// prettier-ignore
import "@reflag/${format}-sdk";
declare module "@reflag/${format}-sdk" {
export interface Flags {
${flags
.map(({ key }) => {
const config = configDefs.get(key);
return indentLines(
`${quoteKey(key)}: ${config?.definition ? `{ config: { payload: ${config.name} } }` : "boolean"};`,
4,
);
})
.join("\n")}
}
${Array.from(configDefs.values())
.map(({ name, definition }) => {
return indentLines(`export type ${name} = ${definition}`);
})
.join("\n\n")}
}
`.trim();
}
export async function writeTypesToFile(
types: string,
outPath: string,
projectPath: string,
) {
const fullPath = isAbsolute(outPath) ? outPath : join(projectPath, outPath);
await mkdir(dirname(fullPath), { recursive: true });
await writeFile(fullPath, types);
return fullPath;
}