Skip to content

Commit 734281a

Browse files
committed
wip
1 parent 67bb3ab commit 734281a

5 files changed

Lines changed: 56 additions & 7 deletions

File tree

designsystemet.config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"$schema": "packages/cli/dist/config-next.schema.json",
33
"outDir": "design-tokens",
44
"clean": true,
5-
"output": [],
5+
"output": [{ "type": "design-tokens" }, { "type": "css" }],
66
"themes": {
77
"designsystemet": {
88
"colors": {

packages/cli/bin/designsystemet.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import { checkAutomigrate } from '../src/automigrate.ts';
88
import { convertToHex } from '../src/colors/index.ts';
99
import type { CssColor } from '../src/colors/types.ts';
1010
import migrations from '../src/migrations/index.ts';
11-
import { parseConfig } from '../src/schemas/helpers.ts';
12-
import type { RootConfigSchema, rootConfig } from '../src/schemas/next/schema.ts';
11+
import { getSchemaDefaults, parseConfig } from '../src/schemas/helpers.ts';
12+
import type { RootConfigSchema } from '../src/schemas/next/schema.ts';
13+
import { rootConfig } from '../src/schemas/next/schema.ts';
1314
import { buildTokens } from '../src/tokens/build.ts';
1415
import { createTokens, systemTokenToFiles, tokenSetDimensions, tokenSetsToFiles } from '../src/tokens/create.ts';
1516
import { generateConfigFromTokens } from '../src/tokens/generate-config.ts';
@@ -300,8 +301,12 @@ program
300301
process.exit(1);
301302
}
302303

303-
const parsedConfig = await parseConfig<RootConfigSchema>(configFile, configFilePath);
304-
const config = parsedConfig ? parsedConfig : parseConfig<RootConfigSchema>(configFile, configFilePath);
304+
const parsedConfig = parseConfig<RootConfigSchema>(configFile, configFilePath);
305+
const defaultConfig = getSchemaDefaults(rootConfig);
306+
const config = { ...defaultConfig, ...parsedConfig };
307+
308+
console.log('Default config:', defaultConfig);
309+
console.log('Merged config:', config);
305310

306311
const themeNames = Object.keys(config.themes);
307312
if (themeNames.length > 0) {

packages/cli/src/schemas/helpers.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { type ParseError, parse as parseJsoncRaw, printParseErrorCode } from 'jsonc-parser';
22
import pc from 'picocolors';
33
import * as R from 'ramda';
4-
import type { z } from 'zod';
4+
import { z } from 'zod';
55
import { fromError } from 'zod-validation-error';
66

77
export const deprecatedCLIOptions = {
@@ -97,6 +97,41 @@ export function parseJsonc<T>(content: string): T {
9797
return result;
9898
}
9999

100+
/**
101+
* Recursively extracts default values from a Zod schema.
102+
*
103+
* Fields with `.default()` return their default value, nested objects are
104+
* traversed recursively, optional/nullable wrappers are unwrapped, and
105+
* single-value literals return their literal value. Fields without a
106+
* resolvable default are omitted from the result.
107+
*
108+
* @template T - The Zod schema type.
109+
* @param schema - The Zod schema to extract default values from.
110+
* @returns The default values described by the schema.
111+
*/
112+
export function getSchemaDefaults<T extends z.ZodType>(schema: T): Partial<z.infer<T>> {
113+
return getDefaultValue(schema) as Partial<z.infer<T>>;
114+
}
115+
116+
function getDefaultValue(schema: z.ZodType): unknown {
117+
if (schema instanceof z.ZodDefault) {
118+
return schema.def.defaultValue;
119+
}
120+
if (schema instanceof z.ZodOptional || schema instanceof z.ZodNullable) {
121+
return getDefaultValue(schema.def.innerType as z.ZodType);
122+
}
123+
if (schema instanceof z.ZodObject) {
124+
const entries = Object.entries(schema.shape)
125+
.map(([key, value]) => [key, getDefaultValue(value as z.ZodType)] as const)
126+
.filter(([, value]) => value !== undefined);
127+
return Object.fromEntries(entries);
128+
}
129+
if (schema instanceof z.ZodLiteral && schema.def.values.length === 1) {
130+
return schema.def.values[0];
131+
}
132+
return undefined;
133+
}
134+
100135
export function parseConfig<T>(configFile: string): T {
101136
if (!configFile) {
102137
return {} as T;
@@ -109,3 +144,12 @@ export function parseConfig<T>(configFile: string): T {
109144
throw new Error(validationError?.toString());
110145
}
111146
}
147+
148+
// export function getDefaults<Schema extends z.ZodObject>(schema: Schema) {
149+
// return Object.fromEntries(
150+
// Object.entries(schema.shape).map(([key, value]) => {
151+
// if (value instanceof z.ZodDefault) return [key, (value.def.defaultValue as () => unknown)()];
152+
// return [key, undefined];
153+
// }),
154+
// );
155+
// }

packages/cli/src/schemas/next/defaults.ts

Whitespace-only changes.

packages/cli/src/schemas/next/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ export const rootConfig = configFileCreateSchema.extend({
2424
output: z.array(outputSchema).describe('An array of output files'),
2525
});
2626

27-
export type RootConfigSchema = z.input<typeof rootConfig>;
27+
export type RootConfigSchema = z.infer<typeof rootConfig>;

0 commit comments

Comments
 (0)