Skip to content

Commit

Permalink
🔧 Add main in package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
ulricden authored and clementdejoie committed Mar 14, 2024
1 parent 2859111 commit f1ef402
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 151 deletions.
3 changes: 0 additions & 3 deletions index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@zerogachis/smartway-design-token",
"version": "0.0.1",
"description": "Smartway design tokens",
"main": "tokens.json",
"main": "src/index.ts",
"scripts": {
"type:check": "tsc",
"tsc": "tsc --project tsconfig.build.json"
Expand Down
294 changes: 147 additions & 147 deletions src/Tokens.ts
Original file line number Diff line number Diff line change
@@ -1,169 +1,169 @@
import * as tokensJson from "../tokens.json";
import {
BoxShadows,
BoxShadow,
Font,
Fonts,
Typography,
FontSize,
FontWeight,
FontFamily,
Spacings,
BorderRadius,
LineHeight,
Color,
BoxShadows,
BoxShadow,
Font,
Fonts,
Typography,
FontSize,
FontWeight,
FontFamily,
Spacings,
BorderRadius,
LineHeight,
Color,
} from "./TokensType";

const parseShadow = (value: any): BoxShadow | void => {
if (typeof value !== "string")
return {
color: value.color,
type: value.type,
x: value.x,
y: value.y,
blur: value.blur,
spread: value.spread,
};
if (typeof value !== "string")
return {
color: value.color,
type: value.type,
x: value.x,
y: value.y,
blur: value.blur,
spread: value.spread,
};
};

const parseAliasShadow =
(shadow: BoxShadows) =>
(value: any): BoxShadow | void => {
if (typeof value === "string") {
const key = value.replace(/\D/g, "");
return shadow[key as keyof BoxShadows];
}
};
(shadow: BoxShadows) =>
(value: any): BoxShadow | void => {
if (typeof value === "string") {
const key = value.replace(/\D/g, "");
return shadow[key as keyof BoxShadows];
}
};

const parseTypography =
(font: Fonts) =>
(value: any): Font => {
return {
fontWeight:
font.fontWeight[
value["fontWeight"]
.replace("}", "")
.split(".")
.slice(-1)[0] as keyof FontWeight
],
fontSize:
font.fontSize[
value["fontSize"]
.replace("}", "")
.split(".")
.slice(-1)[0] as keyof FontSize
],
fontFamily:
font.fontFamily[
value["fontFamily"]
.replace("{", "")
.replace("}", "") as keyof FontFamily
],
};
};
(font: Fonts) =>
(value: any): Font => {
return {
fontWeight:
font.fontWeight[
value["fontWeight"]
.replace("}", "")
.split(".")
.slice(-1)[0] as keyof FontWeight
],
fontSize:
font.fontSize[
value["fontSize"]
.replace("}", "")
.split(".")
.slice(-1)[0] as keyof FontSize
],
fontFamily:
font.fontFamily[
value["fontFamily"]
.replace("{", "")
.replace("}", "") as keyof FontFamily
],
};
};

const parseString = (value: string) => value;

const getValueToken = <T, R>(
tokenValue: any,
parseMethod: (value: any) => R
tokenValue: any,
parseMethod: (value: any) => R
): T => {
let values = {};
const value = tokenValue;
Object.keys(value).forEach((key) => {
const parsedValue = parseMethod(value[key].value);
if (parsedValue) {
(values as Record<string, R>)[key] = parsedValue;
}
});
return values as T;
let values = {};
const value = tokenValue;
Object.keys(value).forEach((key) => {
const parsedValue = parseMethod(value[key].value);
if (parsedValue) {
(values as Record<string, R>)[key] = parsedValue;
}
});
return values as T;
};

const getTokens = () => {
const shadows = getValueToken<BoxShadows, BoxShadow | void>(
tokensJson.global.shadow,
parseShadow
);
const fontSize = getValueToken<FontSize, number>(
tokensJson.global["font-size"],
parseInt
);
const fontWeight = getValueToken<FontWeight, number>(
tokensJson.global["font-weight"],
parseInt
);
const fontFamily: FontFamily = {
"public-sans": "Public Sans",
"noto-sans": "Noto Sans",
};
const font: Fonts = { fontWeight, fontSize, fontFamily };

return {
spacing: getValueToken<Spacings, number>(
tokensJson.global.spacing,
parseInt
),
boxShadow: {
...shadows,
...getValueToken<BoxShadows, BoxShadow | void>(
const shadows = getValueToken<BoxShadows, BoxShadow | void>(
tokensJson.global.shadow,
parseAliasShadow(shadows)
),
},
borderRadius: getValueToken<BorderRadius, number>(
tokensJson.global["border-radius"],
parseInt
),
fontWeight: fontWeight,
lineHeight: getValueToken<LineHeight, string>(
tokensJson.global["line-height"],
parseString
),
color: {
primary: getValueToken<Color, string>(
tokensJson.global.primary,
parseString
),
secondary: getValueToken<Color, string>(
tokensJson.global.secondary,
parseString
),
info: getValueToken<Color, string>(tokensJson.global.info, parseString),
success: getValueToken<Color, string>(
tokensJson.global.success,
parseString
),
warning: getValueToken<Color, string>(
tokensJson.global.warning,
parseString
),
error: getValueToken<Color, string>(tokensJson.global.error, parseString),
neutral: getValueToken<Color, string>(
tokensJson.global.neutral,
parseString
),
},
fontSize: fontSize,
fontFamily: fontFamily,
typography: {
headline: getValueToken<Typography, Font>(
tokensJson.global.headline,
parseTypography(font)
),
body: getValueToken<Typography, Font>(
tokensJson.global.body,
parseTypography(font)
),
label: getValueToken<Typography, Font>(
tokensJson.global.label,
parseTypography(font)
),
button: getValueToken<Typography, Font>(
tokensJson.global.button,
parseTypography(font)
),
},
};
parseShadow
);
const fontSize = getValueToken<FontSize, number>(
tokensJson.global["font-size"],
parseInt
);
const fontWeight = getValueToken<FontWeight, number>(
tokensJson.global["font-weight"],
parseInt
);
const fontFamily: FontFamily = {
"public-sans": "Public Sans",
"noto-sans": "Noto Sans",
};
const font: Fonts = {fontWeight, fontSize, fontFamily};

return {
spacing: getValueToken<Spacings, number>(
tokensJson.global.spacing,
parseInt
),
boxShadow: {
...shadows,
...getValueToken<BoxShadows, BoxShadow | void>(
tokensJson.global.shadow,
parseAliasShadow(shadows)
),
},
borderRadius: getValueToken<BorderRadius, number>(
tokensJson.global["border-radius"],
parseInt
),
fontWeight: fontWeight,
lineHeight: getValueToken<LineHeight, string>(
tokensJson.global["line-height"],
parseString
),
color: {
primary: getValueToken<Color, string>(
tokensJson.global.primary,
parseString
),
secondary: getValueToken<Color, string>(
tokensJson.global.secondary,
parseString
),
info: getValueToken<Color, string>(tokensJson.global.info, parseString),
success: getValueToken<Color, string>(
tokensJson.global.success,
parseString
),
warning: getValueToken<Color, string>(
tokensJson.global.warning,
parseString
),
error: getValueToken<Color, string>(tokensJson.global.error, parseString),
neutral: getValueToken<Color, string>(
tokensJson.global.neutral,
parseString
),
},
fontSize: fontSize,
fontFamily: fontFamily,
typography: {
headline: getValueToken<Typography, Font>(
tokensJson.global.headline,
parseTypography(font)
),
body: getValueToken<Typography, Font>(
tokensJson.global.body,
parseTypography(font)
),
label: getValueToken<Typography, Font>(
tokensJson.global.label,
parseTypography(font)
),
button: getValueToken<Typography, Font>(
tokensJson.global.button,
parseTypography(font)
),
},
};
};

const tokens = getTokens();
Expand Down

0 comments on commit f1ef402

Please sign in to comment.