@digdir/designsystemet 0.1.0-alpha.9 → 0.1.0-next.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (114) hide show
  1. package/dist/bin/designsystemet.js +34 -0
  2. package/dist/src/colors/colorUtils.js +242 -0
  3. package/dist/src/colors/index.js +3 -0
  4. package/dist/src/colors/themeUtils.js +245 -0
  5. package/dist/src/colors/types.js +0 -0
  6. package/dist/src/init/createTokensPackage.js +250 -0
  7. package/dist/src/init/generateMetadataJson.js +19 -0
  8. package/dist/src/init/generateThemesJson.js +52 -0
  9. package/dist/src/init/index.js +10 -0
  10. package/dist/src/init/nextStepsMarkdown.js +92 -0
  11. package/dist/src/init/template/default-files/README.md +10 -0
  12. package/dist/src/init/template/default-files/design-tokens/README.md +3 -0
  13. package/dist/src/init/template/default-files/design-tokens/primitives/globals.json +197 -0
  14. package/dist/src/init/template/default-files/design-tokens/primitives/typography/default.json +86 -0
  15. package/dist/src/init/template/default-files/design-tokens/semantic/color.json +562 -0
  16. package/dist/src/init/template/default-files/design-tokens/semantic/style.json +543 -0
  17. package/dist/src/init/template/prettier.config.js +5 -0
  18. package/dist/src/init/template/template-files/design-tokens/primitives/colors/contrast/global.json +376 -0
  19. package/dist/src/init/template/template-files/design-tokens/primitives/colors/contrast/theme-template.json +314 -0
  20. package/dist/src/init/template/template-files/design-tokens/primitives/colors/dark/global.json +376 -0
  21. package/dist/src/init/template/template-files/design-tokens/primitives/colors/dark/theme-template.json +314 -0
  22. package/dist/src/init/template/template-files/design-tokens/primitives/colors/light/global.json +376 -0
  23. package/dist/src/init/template/template-files/design-tokens/primitives/colors/light/theme-template.json +314 -0
  24. package/dist/src/init/template/template-files/design-tokens/themes/theme-template.json +314 -0
  25. package/dist/src/init/template/template-files/package.json +24 -0
  26. package/dist/src/init/utils.js +15 -0
  27. package/dist/src/migrations/beta-to-v1.js +344 -0
  28. package/dist/src/migrations/codemods/css/plugins.js +47 -0
  29. package/dist/src/migrations/codemods/css/run.js +19 -0
  30. package/dist/src/migrations/codemods/jsx/classname-prefix.js +63 -0
  31. package/dist/src/migrations/codemods/jsx/run.js +22 -0
  32. package/dist/src/migrations/index.js +9 -0
  33. package/dist/src/migrations/react-beta-to-v1.js +5 -0
  34. package/dist/src/tokens/actions.js +33 -0
  35. package/dist/src/tokens/build.js +74 -0
  36. package/dist/src/tokens/configs.js +223 -0
  37. package/dist/src/tokens/formats/css.js +170 -0
  38. package/dist/src/tokens/formats/js-tokens.js +31 -0
  39. package/dist/src/tokens/transformers.js +47 -0
  40. package/dist/src/tokens/utils/noCase.js +24 -0
  41. package/dist/src/tokens/utils/permutateThemes.js +64 -0
  42. package/dist/src/tokens/utils/utils.js +14 -0
  43. package/package.json +21 -13
  44. package/LICENSE +0 -7
  45. package/dist/build/bin/designsystemet.js +0 -50
  46. package/dist/build/src/colors/colorUtils.js +0 -314
  47. package/dist/build/src/colors/index.js +0 -3
  48. package/dist/build/src/colors/themeUtils.js +0 -290
  49. package/dist/build/src/colors/types.js +0 -1
  50. package/dist/build/src/migrations/beta-to-v1.js +0 -341
  51. package/dist/build/src/migrations/codemods/css/plugins.js +0 -45
  52. package/dist/build/src/migrations/codemods/css/run.js +0 -17
  53. package/dist/build/src/migrations/codemods/jsx/classname-prefix.js +0 -80
  54. package/dist/build/src/migrations/codemods/jsx/run.js +0 -19
  55. package/dist/build/src/migrations/index.js +0 -6
  56. package/dist/build/src/migrations/react-beta-to-v1.js +0 -2
  57. package/dist/build/src/test/jsx-test.js +0 -10
  58. package/dist/build/src/tokens/actions.js +0 -20
  59. package/dist/build/src/tokens/build.js +0 -63
  60. package/dist/build/src/tokens/configs.js +0 -229
  61. package/dist/build/src/tokens/formats/css-classes.js +0 -50
  62. package/dist/build/src/tokens/formats/css-variables.js +0 -39
  63. package/dist/build/src/tokens/formats/js-tokens.js +0 -29
  64. package/dist/build/src/tokens/transformers.js +0 -42
  65. package/dist/build/src/tokens/utils/noCase.js +0 -30
  66. package/dist/build/src/tokens/utils/permutateThemes.js +0 -56
  67. package/dist/build/src/tokens/utils/utils.js +0 -21
  68. package/dist/build/tsconfig.tsbuildinfo +0 -1
  69. package/dist/types/bin/designsystemet.d.ts +0 -3
  70. package/dist/types/bin/designsystemet.d.ts.map +0 -1
  71. package/dist/types/src/colors/colorUtils.d.ts +0 -118
  72. package/dist/types/src/colors/colorUtils.d.ts.map +0 -1
  73. package/dist/types/src/colors/index.d.ts +0 -4
  74. package/dist/types/src/colors/index.d.ts.map +0 -1
  75. package/dist/types/src/colors/themeUtils.d.ts +0 -101
  76. package/dist/types/src/colors/themeUtils.d.ts.map +0 -1
  77. package/dist/types/src/colors/types.d.ts +0 -16
  78. package/dist/types/src/colors/types.d.ts.map +0 -1
  79. package/dist/types/src/migrations/beta-to-v1.d.ts +0 -3
  80. package/dist/types/src/migrations/beta-to-v1.d.ts.map +0 -1
  81. package/dist/types/src/migrations/codemods/css/plugins.d.ts +0 -6
  82. package/dist/types/src/migrations/codemods/css/plugins.d.ts.map +0 -1
  83. package/dist/types/src/migrations/codemods/css/run.d.ts +0 -8
  84. package/dist/types/src/migrations/codemods/css/run.d.ts.map +0 -1
  85. package/dist/types/src/migrations/codemods/jsx/classname-prefix.d.ts +0 -10
  86. package/dist/types/src/migrations/codemods/jsx/classname-prefix.d.ts.map +0 -1
  87. package/dist/types/src/migrations/codemods/jsx/run.d.ts +0 -7
  88. package/dist/types/src/migrations/codemods/jsx/run.d.ts.map +0 -1
  89. package/dist/types/src/migrations/index.d.ts +0 -6
  90. package/dist/types/src/migrations/index.d.ts.map +0 -1
  91. package/dist/types/src/migrations/react-beta-to-v1.d.ts +0 -3
  92. package/dist/types/src/migrations/react-beta-to-v1.d.ts.map +0 -1
  93. package/dist/types/src/test/jsx-test.d.ts +0 -4
  94. package/dist/types/src/test/jsx-test.d.ts.map +0 -1
  95. package/dist/types/src/tokens/actions.d.ts +0 -3
  96. package/dist/types/src/tokens/actions.d.ts.map +0 -1
  97. package/dist/types/src/tokens/build.d.ts +0 -11
  98. package/dist/types/src/tokens/build.d.ts.map +0 -1
  99. package/dist/types/src/tokens/configs.d.ts +0 -23
  100. package/dist/types/src/tokens/configs.d.ts.map +0 -1
  101. package/dist/types/src/tokens/formats/css-classes.d.ts +0 -6
  102. package/dist/types/src/tokens/formats/css-classes.d.ts.map +0 -1
  103. package/dist/types/src/tokens/formats/css-variables.d.ts +0 -3
  104. package/dist/types/src/tokens/formats/css-variables.d.ts.map +0 -1
  105. package/dist/types/src/tokens/formats/js-tokens.d.ts +0 -6
  106. package/dist/types/src/tokens/formats/js-tokens.d.ts.map +0 -1
  107. package/dist/types/src/tokens/transformers.d.ts +0 -5
  108. package/dist/types/src/tokens/transformers.d.ts.map +0 -1
  109. package/dist/types/src/tokens/utils/noCase.d.ts +0 -11
  110. package/dist/types/src/tokens/utils/noCase.d.ts.map +0 -1
  111. package/dist/types/src/tokens/utils/permutateThemes.d.ts +0 -7
  112. package/dist/types/src/tokens/utils/permutateThemes.d.ts.map +0 -1
  113. package/dist/types/src/tokens/utils/utils.d.ts +0 -17
  114. package/dist/types/src/tokens/utils/utils.d.ts.map +0 -1
@@ -0,0 +1,223 @@
1
+ import * as tokenStudio from "@tokens-studio/sd-transforms";
2
+ import * as R from "ramda";
3
+ import StyleDictionary from "style-dictionary";
4
+ import { outputReferencesFilter } from "style-dictionary/utils";
5
+ import { makeEntryFile } from "./actions.js";
6
+ import * as formats from "./formats/css.js";
7
+ import { jsTokens } from "./formats/js-tokens.js";
8
+ import { nameKebab, sizeRem, typographyShorthand } from "./transformers.js";
9
+ import { permutateThemes as permutateThemes_ } from "./utils/permutateThemes.js";
10
+ import { typeEquals } from "./utils/utils.js";
11
+ void tokenStudio.registerTransforms(StyleDictionary);
12
+ const prefix = "ds";
13
+ const basePxFontSize = 16;
14
+ const separator = "_";
15
+ const fileHeader = () => [`These files are generated from design tokens defind using Token Studio`];
16
+ StyleDictionary.registerTransform(sizeRem);
17
+ StyleDictionary.registerTransform(nameKebab);
18
+ StyleDictionary.registerTransform(typographyShorthand);
19
+ StyleDictionary.registerFormat(jsTokens);
20
+ StyleDictionary.registerFormat(formats.colormode);
21
+ StyleDictionary.registerFormat(formats.semantic);
22
+ StyleDictionary.registerFormat(formats.typography);
23
+ StyleDictionary.registerAction(makeEntryFile);
24
+ const dsTransformers = [
25
+ nameKebab.name,
26
+ `ts/resolveMath`,
27
+ "ts/size/px",
28
+ sizeRem.name,
29
+ "ts/typography/fontWeight",
30
+ typographyShorthand.name,
31
+ "ts/color/modifiers",
32
+ "ts/color/css/hexrgba",
33
+ "ts/size/lineheight",
34
+ "ts/shadow/css/shorthand"
35
+ ];
36
+ const paritionPrimitives = R.partition(R.test(/(?!.*global\.json).*primitives.*/));
37
+ const hasUnknownProps = R.pipe(R.values, R.none(R.equals("unknown")), R.not);
38
+ const outputColorReferences = (token) => {
39
+ if (R.test(/accent|neutral|brand1|brand2|brand3|success|danger|warning/, token.name) && R.includes("semantic/color", token.filePath)) {
40
+ return true;
41
+ }
42
+ return false;
43
+ };
44
+ const permutateThemes = ($themes) => permutateThemes_($themes, {
45
+ separator
46
+ });
47
+ const colorModeVariables = ({ mode = "light", outPath, theme }) => {
48
+ const selector = `${mode === "light" ? ":root, " : ""}[data-ds-color-mode="${mode}"]`;
49
+ const layer = `ds.theme.color-mode.${mode}`;
50
+ return {
51
+ log: { verbosity: "silent" },
52
+ preprocessors: ["tokens-studio"],
53
+ platforms: {
54
+ css: {
55
+ // custom
56
+ outPath,
57
+ mode,
58
+ theme,
59
+ selector,
60
+ layer,
61
+ //
62
+ prefix,
63
+ buildPath: `${outPath}/${theme}/`,
64
+ transforms: dsTransformers,
65
+ actions: [makeEntryFile.name],
66
+ files: [
67
+ {
68
+ destination: `color-mode/${mode}.css`,
69
+ format: formats.colormode.name,
70
+ filter: (token) => !token.isSource && typeEquals("color", token)
71
+ }
72
+ ],
73
+ options: {
74
+ fileHeader,
75
+ outputReferences: (token, options) => outputColorReferences(token) && outputReferencesFilter(token, options)
76
+ }
77
+ }
78
+ }
79
+ };
80
+ };
81
+ const semanticVariables = ({ outPath, theme }) => {
82
+ const selector = `:root`;
83
+ const layer = `ds.theme.semantic`;
84
+ const isCalculatedToken = (token) => typeEquals(["spacing", "sizing", "borderRadius"], token);
85
+ return {
86
+ log: { verbosity: "silent" },
87
+ preprocessors: ["tokens-studio"],
88
+ platforms: {
89
+ css: {
90
+ // custom
91
+ outPath,
92
+ theme,
93
+ basePxFontSize,
94
+ isCalculatedToken,
95
+ selector,
96
+ layer,
97
+ //
98
+ prefix,
99
+ buildPath: `${outPath}/${theme}/`,
100
+ transforms: dsTransformers,
101
+ actions: [makeEntryFile.name],
102
+ files: [
103
+ {
104
+ destination: `semantic.css`,
105
+ format: formats.semantic.name,
106
+ filter: (token) => (!token.isSource || isCalculatedToken(token)) && !typeEquals(["color", "fontWeights", "fontFamilies"], token)
107
+ }
108
+ ],
109
+ options: {
110
+ fileHeader,
111
+ outputReferences: (token, options) => isCalculatedToken(token) && outputReferencesFilter(token, options)
112
+ }
113
+ }
114
+ }
115
+ };
116
+ };
117
+ const typescriptTokens = ({ mode = "unknown", outPath, theme }) => {
118
+ return {
119
+ log: { verbosity: "silent" },
120
+ preprocessors: ["tokens-studio"],
121
+ platforms: {
122
+ ts: {
123
+ prefix,
124
+ basePxFontSize,
125
+ transforms: dsTransformers,
126
+ buildPath: `${outPath}/${theme}/`,
127
+ files: [
128
+ {
129
+ destination: `${mode}.ts`,
130
+ format: jsTokens.name,
131
+ outputReferences: outputColorReferences,
132
+ filter: (token) => {
133
+ if (R.test(/primitives\/modes|\/themes/, token.filePath)) {
134
+ return false;
135
+ }
136
+ if (R.test(/accent|neutral|brand1|brand2|brand3|success|danger|warning/, token.name) || R.includes("semantic", token.filePath)) {
137
+ return true;
138
+ }
139
+ return false;
140
+ }
141
+ }
142
+ ],
143
+ options: {
144
+ fileHeader
145
+ }
146
+ }
147
+ }
148
+ };
149
+ };
150
+ const typographyCSS = ({ outPath, theme, typography }) => {
151
+ const selector = `${typography === "primary" ? ":root, " : ""}[data-ds-typography="${typography}"]`;
152
+ const layer = `ds.theme.typography.${typography}`;
153
+ return {
154
+ log: { verbosity: "silent" },
155
+ preprocessors: ["tokens-studio"],
156
+ platforms: {
157
+ css: {
158
+ prefix,
159
+ typography,
160
+ selector,
161
+ layer,
162
+ buildPath: `${outPath}/${theme}/`,
163
+ basePxFontSize,
164
+ transforms: [nameKebab.name, "ts/size/px", sizeRem.name, "ts/size/lineheight", "ts/typography/fontWeight"],
165
+ files: [
166
+ {
167
+ destination: `typography/${typography}.css`,
168
+ format: formats.typography.name,
169
+ filter: (token) => {
170
+ return typeEquals(
171
+ ["typography", "fontweights", "fontfamilies", "lineheights", "fontsizes", "letterSpacing"],
172
+ token
173
+ ) && !(token.path[0] || "").startsWith("theme");
174
+ }
175
+ }
176
+ ],
177
+ options: {
178
+ fileHeader
179
+ }
180
+ }
181
+ }
182
+ };
183
+ };
184
+ const getConfigs = (getConfig, outPath, tokensDir, permutatedThemes) => permutatedThemes.map((permutatedTheme) => {
185
+ const {
186
+ selectedTokenSets = [],
187
+ mode = "unknown",
188
+ theme = "unknown",
189
+ semantic = "unknown",
190
+ size = "unknown",
191
+ typography = "unknown"
192
+ } = permutatedTheme;
193
+ if (hasUnknownProps(permutatedTheme)) {
194
+ throw Error(`Theme ${permutatedTheme.name} has unknown props: ${JSON.stringify(permutatedTheme)}`);
195
+ }
196
+ const setsWithPaths = selectedTokenSets.map((x) => `${tokensDir}/${x}.json`);
197
+ const [source, include] = paritionPrimitives(setsWithPaths);
198
+ const config_ = getConfig({
199
+ outPath,
200
+ theme,
201
+ mode,
202
+ semantic,
203
+ size,
204
+ typography
205
+ });
206
+ const config = {
207
+ ...config_,
208
+ source,
209
+ include
210
+ };
211
+ return { mode, theme, semantic, size, typography, config };
212
+ }).sort();
213
+ export {
214
+ basePxFontSize,
215
+ colorModeVariables,
216
+ getConfigs,
217
+ permutateThemes,
218
+ prefix,
219
+ semanticVariables,
220
+ separator,
221
+ typescriptTokens,
222
+ typographyCSS
223
+ };
@@ -0,0 +1,170 @@
1
+ import * as R from "ramda";
2
+ import { createPropertyFormatter, fileHeader, getReferences, usesReferences } from "style-dictionary/utils";
3
+ import { prefix } from "../configs.js";
4
+ import { getValue, typeEquals } from "../utils/utils.js";
5
+ const prefersColorScheme = (mode, content) => `
6
+ @media (prefers-color-scheme: ${mode}) {
7
+ [data-ds-color-mode="auto"] ${content}
8
+ }
9
+ `;
10
+ const colormode = {
11
+ name: "ds/css-colormode",
12
+ format: async ({ dictionary, file, options, platform }) => {
13
+ const { allTokens } = dictionary;
14
+ const { outputReferences } = options;
15
+ const { selector, mode, layer } = platform;
16
+ const mode_ = mode;
17
+ const header = await fileHeader({ file });
18
+ const format = createPropertyFormatter({
19
+ outputReferences,
20
+ dictionary,
21
+ format: "css"
22
+ });
23
+ const colorSchemeProperty = mode_ === "dark" || mode_ === "light" ? `color-scheme: ${mode_};
24
+ ` : "";
25
+ const content = `{
26
+ ${allTokens.map(format).join("\n")}
27
+ ${colorSchemeProperty}}
28
+ `;
29
+ const autoSelectorContent = ["light", "dark"].includes(mode_) ? prefersColorScheme(mode_, content) : "";
30
+ return header + `@layer ${layer} {
31
+ ${selector} ${content} ${autoSelectorContent}
32
+ }
33
+ `;
34
+ }
35
+ };
36
+ const calculatedVariable = R.pipe(R.split(/:(.*?);/g), (split) => `${split[0]}: calc(${R.trim(split[1])});`);
37
+ const semantic = {
38
+ name: "ds/css-semantic",
39
+ format: async ({ dictionary, file, options, platform }) => {
40
+ const { allTokens } = dictionary;
41
+ const { outputReferences } = options;
42
+ const { selector, isCalculatedToken, layer } = platform;
43
+ const header = await fileHeader({ file });
44
+ const format = createPropertyFormatter({
45
+ outputReferences,
46
+ dictionary,
47
+ format: "css"
48
+ });
49
+ const formatTokens = R.map((token) => {
50
+ const originalValue = getValue(token.original);
51
+ if (usesReferences(originalValue) && typeof outputReferences === "function" && outputReferences?.(token, { dictionary })) {
52
+ if (isCalculatedToken?.(token, options)) {
53
+ return calculatedVariable(format(token));
54
+ }
55
+ }
56
+ return format(token);
57
+ });
58
+ const formattedVariables = formatTokens(allTokens);
59
+ const content = `{
60
+ ${formattedVariables.join("\n")}
61
+ }
62
+ `;
63
+ return header + `@layer ${layer} {
64
+ ${selector} ${content}
65
+ }
66
+ `;
67
+ }
68
+ };
69
+ const sortByType = R.sortBy((token) => token?.type === "typography");
70
+ const getVariableName = R.pipe(
71
+ R.split(":"),
72
+ R.head,
73
+ R.defaultTo(""),
74
+ R.trim,
75
+ (name) => `var(${name})`
76
+ );
77
+ const bemify = R.pipe(
78
+ (path) => {
79
+ const filteredPath = path.filter((p) => p !== "typography");
80
+ const withPrefix = R.concat([prefix], R.remove(0, 0, filteredPath));
81
+ const [rest, last] = R.splitAt(-1, withPrefix);
82
+ const className = `${rest.join("-")}--${R.head(last)}`;
83
+ return className;
84
+ },
85
+ R.trim,
86
+ R.toLower
87
+ );
88
+ const classSelector = R.pipe(R.prop("path"), bemify);
89
+ const sortTypographyLast = R.sortWith([
90
+ R.ascend((token) => typeEquals("typography")(token) ? 1 : 0)
91
+ ]);
92
+ const typography = {
93
+ name: "ds/css-typography",
94
+ format: async ({ dictionary, file, options, platform }) => {
95
+ const { outputReferences } = options;
96
+ const { selector, layer } = platform;
97
+ const header = await fileHeader({ file });
98
+ const format = createPropertyFormatter({
99
+ outputReferences,
100
+ dictionary,
101
+ format: "css"
102
+ });
103
+ const sortedTokens = sortTypographyLast(dictionary.allTokens);
104
+ const formattedTokens = R.pipe(
105
+ sortByType,
106
+ R.reduce(
107
+ (acc, token) => {
108
+ if (typeEquals("fontweights", token)) {
109
+ const className = `
110
+ .${classSelector(token)} {
111
+ font-weight: ${getValue(token)};
112
+ }`;
113
+ return {
114
+ ...acc,
115
+ variables: [...acc.variables, format(token)],
116
+ classes: [...acc.classes, className]
117
+ };
118
+ }
119
+ if (typeEquals("lineheights", token)) {
120
+ const className = `
121
+ .${classSelector(token)} {
122
+ line-height: ${getValue(token)};
123
+ }`;
124
+ return {
125
+ ...acc,
126
+ variables: [...acc.variables, format(token)],
127
+ classes: [...acc.classes, className]
128
+ };
129
+ }
130
+ if (typeEquals("typography", token)) {
131
+ const references = getReferences(getValue(token.original), dictionary.tokens);
132
+ const fontweight = R.find(typeEquals(["fontweights"]))(references);
133
+ const lineheight = R.find(typeEquals(["lineheights"]))(references);
134
+ const fontsize = R.find(typeEquals(["fontsizes"]))(references);
135
+ const letterSpacing = R.find(typeEquals(["letterSpacing"]))(references);
136
+ const fontSizeVar = fontsize ? getVariableName(format(fontsize)) : null;
137
+ const fontWeightVar = fontweight ? getVariableName(format(fontweight)) : null;
138
+ const lineheightVar = lineheight ? getVariableName(format(lineheight)) : null;
139
+ const letterSpacingVar = letterSpacing ? getVariableName(format(letterSpacing)) : null;
140
+ const className = `
141
+ .${classSelector(token)} {
142
+ ${fontSizeVar && `font-size: ${fontSizeVar};`}
143
+ ${lineheightVar && `line-height: ${lineheightVar};`}
144
+ ${fontWeightVar && `font-weight: ${fontWeightVar};`}
145
+ ${letterSpacingVar && `letter-spacing: ${letterSpacingVar};`}
146
+ }`;
147
+ return { ...acc, classes: [className, ...acc.classes] };
148
+ }
149
+ return { ...acc, variables: [...acc.variables, format(token)] };
150
+ },
151
+ { variables: [], classes: [] }
152
+ )
153
+ )(sortedTokens);
154
+ const classes = formattedTokens.classes.join("\n");
155
+ const variables = formattedTokens.variables.join("\n");
156
+ const content = selector ? `${selector} {
157
+ ${variables}
158
+ ${classes}
159
+ }` : classes;
160
+ return header + `@layer ${layer} {
161
+ ${content}
162
+ }
163
+ `;
164
+ }
165
+ };
166
+ export {
167
+ colormode,
168
+ semantic,
169
+ typography
170
+ };
@@ -0,0 +1,31 @@
1
+ import * as R from "ramda";
2
+ import { createPropertyFormatter, fileHeader } from "style-dictionary/utils";
3
+ import { getType } from "../utils/utils.js";
4
+ const groupByType = R.groupBy((token) => getType(token));
5
+ const removeUnwatedTokens = R.filter(
6
+ (token) => !["fds-base_spacing", "fds-base_sizing"].includes(token.name)
7
+ );
8
+ const toCssVarName = R.pipe(R.split(":"), R.head, R.trim);
9
+ const jsTokens = {
10
+ name: "ds/js-tokens",
11
+ format: async ({ dictionary, file }) => {
12
+ const format = createPropertyFormatter({
13
+ dictionary,
14
+ format: "css"
15
+ });
16
+ const formatTokens = R.map((token) => ({
17
+ ...token,
18
+ name: toCssVarName(format(token))
19
+ }));
20
+ const processTokens = R.pipe(removeUnwatedTokens, formatTokens, groupByType);
21
+ const tokens = processTokens(dictionary.allTokens);
22
+ const content = Object.entries(tokens).map(
23
+ ([name, token]) => `export const ${name} = ${JSON.stringify(token, null, 2).replace(/"([^"]+)":/g, "$1:")}
24
+ `
25
+ ).join("\n");
26
+ return fileHeader({ file }).then((fileHeaderText) => fileHeaderText + content);
27
+ }
28
+ };
29
+ export {
30
+ jsTokens
31
+ };
@@ -0,0 +1,47 @@
1
+ import * as R from "ramda";
2
+ import { noCase } from "./utils/noCase.js";
3
+ import { getValue, typeEquals } from "./utils/utils.js";
4
+ const isPx = R.test(/\b\d+px\b/g);
5
+ const sizeRem = {
6
+ name: "ds/size/toRem",
7
+ type: "value",
8
+ transitive: true,
9
+ filter: (token) => typeEquals(["sizing", "spacing", "borderRadius", "fontsizes"], token),
10
+ transform: (token, config) => {
11
+ const value = getValue(token);
12
+ if (isPx(value)) {
13
+ const baseFont = config.basePxFontSize || 16;
14
+ const size = parseInt(value);
15
+ if (size === 0) {
16
+ return "0";
17
+ }
18
+ return `${size / baseFont}rem`;
19
+ }
20
+ return value;
21
+ }
22
+ };
23
+ const nameKebab = {
24
+ name: "name/cti/hierarchical-kebab",
25
+ type: "name",
26
+ transform: (token, options) => {
27
+ return noCase([options?.prefix].concat(token.path).join("-"), {
28
+ delimiter: "-",
29
+ stripRegexp: /[^A-Z0-9_]+/gi
30
+ });
31
+ }
32
+ };
33
+ const typographyShorthand = {
34
+ name: "typography/shorthand",
35
+ type: "value",
36
+ transitive: true,
37
+ filter: (token) => token.type === "typography",
38
+ transform: (token) => {
39
+ const typography = getValue(token);
40
+ return `${typography.fontWeight} ${typography.fontSize}/${typography.lineHeight} '${typography.fontFamily}'`;
41
+ }
42
+ };
43
+ export {
44
+ nameKebab,
45
+ sizeRem,
46
+ typographyShorthand
47
+ };
@@ -0,0 +1,24 @@
1
+ const lowerCase = (text = "") => text.toLowerCase();
2
+ const DEFAULT_SPLIT_REGEXP = [/([a-z0-9])([A-Z])/g, /([A-Z])([A-Z][a-z])/g];
3
+ const DEFAULT_STRIP_REGEXP = /[^A-Z0-9]+/gi;
4
+ function noCase(input, options = {}) {
5
+ const {
6
+ splitRegexp = DEFAULT_SPLIT_REGEXP,
7
+ stripRegexp = DEFAULT_STRIP_REGEXP,
8
+ transform = lowerCase,
9
+ delimiter = " "
10
+ } = options;
11
+ const result = replace(replace(input, splitRegexp, "$1\0$2"), stripRegexp, "\0");
12
+ let start = 0;
13
+ let end = result.length;
14
+ while (result.charAt(start) === "\0") start++;
15
+ while (result.charAt(end - 1) === "\0") end--;
16
+ return result.slice(start, end).split("\0").map(transform).join(delimiter);
17
+ }
18
+ function replace(input, re, value) {
19
+ if (re instanceof RegExp) return input.replace(re, value);
20
+ return re.reduce((input2, re2) => input2.replace(re2, value), input);
21
+ }
22
+ export {
23
+ noCase
24
+ };
@@ -0,0 +1,64 @@
1
+ import { TokenSetStatus } from "@tokens-studio/types";
2
+ import * as R from "ramda";
3
+ function mapThemesToSetsObject(themes) {
4
+ return themes.map((theme) => ({ name: theme.name, selectedTokenSets: filterTokenSets(theme.selectedTokenSets) }));
5
+ }
6
+ function permutateThemes(themes, { separator = "-" } = {}) {
7
+ if (!themes.some((theme) => theme.group)) {
8
+ return mapThemesToSetsObject(themes);
9
+ }
10
+ const groups = {};
11
+ themes.forEach((theme) => {
12
+ if (theme.group) {
13
+ groups[theme.group] = [...groups[theme.group] ?? [], theme];
14
+ } else {
15
+ throw new Error(
16
+ `Theme ${theme.name} does not have a group property, which is required for multi-dimensional theming.`
17
+ );
18
+ }
19
+ });
20
+ if (Object.keys(groups).length <= 1) {
21
+ return mapThemesToSetsObject(themes);
22
+ }
23
+ const permutations = cartesian(Object.values(groups));
24
+ const permutatedThemes = permutations.map((perm) => {
25
+ const permutatedTheme = perm.reduce(
26
+ (acc, theme) => {
27
+ const { group, name, selectedTokenSets } = theme;
28
+ let updatedPermutatedTheme = acc;
29
+ if (group) {
30
+ const groupProp = R.lensProp(group.toLowerCase());
31
+ updatedPermutatedTheme = R.set(groupProp, name.toLowerCase(), updatedPermutatedTheme);
32
+ }
33
+ const updatedName = `${String(acc.name)}${acc ? separator : ""}${name}`;
34
+ const sets = [...updatedPermutatedTheme.selectedTokenSets, ...filterTokenSets(selectedTokenSets)];
35
+ return {
36
+ ...updatedPermutatedTheme,
37
+ name: updatedName,
38
+ selectedTokenSets: sets
39
+ };
40
+ },
41
+ { name: "", selectedTokenSets: [] }
42
+ );
43
+ const uniqueTokenSets = new Set(permutatedTheme.selectedTokenSets);
44
+ return { ...permutatedTheme, selectedTokenSets: Array.from(uniqueTokenSets) };
45
+ });
46
+ return permutatedThemes;
47
+ }
48
+ function filterTokenSets(tokensets) {
49
+ return Object.entries(tokensets).filter(([, val]) => val !== TokenSetStatus.DISABLED).sort((a, b) => {
50
+ if (a[1] === TokenSetStatus.SOURCE && b[1] === TokenSetStatus.ENABLED) {
51
+ return -1;
52
+ }
53
+ if (a[1] === TokenSetStatus.ENABLED && b[1] === TokenSetStatus.SOURCE) {
54
+ return 1;
55
+ }
56
+ return 0;
57
+ }).map((entry) => entry[0]);
58
+ }
59
+ function cartesian(a) {
60
+ return a.reduce((a2, b) => a2.flatMap((d) => b.map((e) => [d, e].flat())));
61
+ }
62
+ export {
63
+ permutateThemes
64
+ };
@@ -0,0 +1,14 @@
1
+ import * as R from "ramda";
2
+ const getType = (token) => (token.$type ?? token.type) || "";
3
+ const getValue = (token) => token.$value ?? token.value;
4
+ const typeEquals = R.curry((types, token) => {
5
+ if (R.isNil(token)) {
6
+ return false;
7
+ }
8
+ return R.includes(R.toLower(getType(token)), R.map(R.toLower, Array.isArray(types) ? types : [types]));
9
+ });
10
+ export {
11
+ getType,
12
+ getValue,
13
+ typeEquals
14
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@digdir/designsystemet",
3
- "version": "0.1.0-alpha.9",
3
+ "version": "0.1.0-next.21",
4
4
  "description": "CLI for Designsystemet",
5
5
  "author": "Designsystemet team",
6
6
  "repository": {
@@ -10,8 +10,13 @@
10
10
  "homepage": "https://github.com/digdir/designsystemet/tree/main/scripts/cli",
11
11
  "license": "MIT",
12
12
  "type": "module",
13
+ "types": "./dist/types",
14
+ "files": [
15
+ "./dist/**"
16
+ ],
17
+ "bin": "dist/bin/designsystemet.js",
13
18
  "exports": {
14
- "./color": "./src/colors/index.ts"
19
+ "./color": "./dist/src/colors/index.js"
15
20
  },
16
21
  "publishConfig": {
17
22
  "access": "public"
@@ -20,20 +25,19 @@
20
25
  "designsystemet": "tsx ./bin/designsystemet.ts",
21
26
  "build:tokens": "yarn clean:theme && yarn designsystemet tokens -p -t ../../design-tokens -o ../../packages/theme/brand",
22
27
  "build:tokens:debug": "yarn clean:theme && tsx --inspect-brk ./bin/designsystemet.ts tokens -p -t ../../design-tokens -o ../../packages/theme/brand",
23
- "build": "yarn clean && tsc",
24
- "prepublish:tsup": "yarn clean && tsup src/**/*.ts bin/designsystemet.ts --cjsInterop --shims --format esm",
28
+ "build": "tsup",
29
+ "build:swc": "yarn clean && swc src bin --copy-files -d dist && yarn build:types",
30
+ "build:types": "tsc --emitDeclarationOnly --declaration",
25
31
  "clean": "rimraf dist",
26
32
  "clean:theme": "yarn workspace @digdir/designsystemet-theme clean"
27
33
  },
28
- "files": [
29
- "./dist/**"
30
- ],
31
- "bin": "dist/build/bin/designsystemet.js",
32
34
  "dependencies": {
33
35
  "@adobe/leonardo-contrast-colors": "^1.0.0",
34
36
  "@commander-js/extra-typings": "^12.0.1",
35
37
  "@tokens-studio/sd-transforms": "^0.16.1",
38
+ "apca-w3": "^0.1.9",
36
39
  "chalk": "^5.3.0",
40
+ "change-case": "^5.3.0",
37
41
  "chroma-js": "^2.4.2",
38
42
  "commander": "^12.0.0",
39
43
  "fast-glob": "^3.3.2",
@@ -41,21 +45,25 @@
41
45
  "jscodeshift": "^0.15.2",
42
46
  "object-hash": "^3.0.0",
43
47
  "postcss": "^8.4.38",
48
+ "prompts": "^2.4.0",
44
49
  "ramda": "^0.29.1",
45
50
  "rimraf": "^5.0.5",
46
- "style-dictionary": "^4.0.0-prerelease.34",
47
- "tsup": "^8.0.2"
51
+ "style-dictionary": "^4.0.0-prerelease.34"
48
52
  },
49
53
  "devDependencies": {
54
+ "@swc/cli": "^0.3.14",
55
+ "@swc/core": "^1.6.5",
56
+ "@types/apca-w3": "^0.1.3",
50
57
  "@types/fs-extra": "^11.0.4",
51
58
  "@types/glob": "^8.1.0",
52
59
  "@types/jscodeshift": "^0.11.11",
53
- "@types/node": "^20.12.7",
60
+ "@types/node": "^20.14.12",
54
61
  "@types/object-hash": "^3",
62
+ "@types/prompts": "^2.4.9",
55
63
  "@types/ramda": "^0.29.9",
56
64
  "fs-extra": "^11.2.0",
65
+ "tsup": "^8.1.0",
57
66
  "tsx": "^4.11.2",
58
67
  "typescript": "^5.4.5"
59
- },
60
- "gitHead": "87591aa2ce0bbcb4fc451ddcbb2ae9f15cfcfeca"
68
+ }
61
69
  }
package/LICENSE DELETED
@@ -1,7 +0,0 @@
1
- Copyright 2024 Digitaliseringsdirektoratet (Digdir)
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
-
5
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
-
7
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.