Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update init script to support latest design-tokens #2255

Merged
merged 7 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/gorgeous-readers-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@digdir/designsystemet': minor
---

feat: Update init script to new design-tokens format
2 changes: 1 addition & 1 deletion .github/workflows/conventional-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
commitlintRulesPath: './commitlint.rules.cjs'
commitlintRulesPath: './commitlint.rules.js'
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"build:react": "yarn workspace @digdir/designsystemet-react build",
"build:tokens": "yarn workspace @digdir/designsystemet build:tokens",
"build:css": "yarn workspace @digdir/designsystemet-css build",
"build:cli": "yarn workspace @digdir/designsystemet build",
"build:storybook": "yarn workspace @designsystemet/storybook build",
"build:storefront": "yarn workspace storefront build",
"build:devsite": "yarn workspace dev build",
Expand Down
40 changes: 36 additions & 4 deletions packages/cli/src/init/createTokensPackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,23 +217,55 @@ Will now create the following:
// Directory creation failed, probably because the directory already exists
}

try {
await fs.mkdir(path.join(TOKENS_TARGET_DIR, 'themes'));
} catch {
// Directory creation failed, probably because the directory already exists
}

try {
await fs.mkdir(path.join(TOKENS_TARGET_DIR, 'primitives/modes/typography/primary'), { recursive: true });
await fs.mkdir(path.join(TOKENS_TARGET_DIR, 'primitives/modes/typography/secondary'), { recursive: true });
} catch {
// Directory creation failed, probably because the directory already exists
}

for (const theme of themes.map(normalizeTokenSetName)) {
for (const mode of modes.map(normalizeTokenSetName)) {
// Copy the global file for the color mode
await fs.cp(
path.join(TOKEN_TEMPLATE_FILES_PATH, `primitives/colors/${mode}/global.json`),
path.join(TOKENS_TARGET_DIR, `primitives/colors/${mode}/global.json`),
path.join(TOKEN_TEMPLATE_FILES_PATH, `primitives/modes/colors/${mode}/global.json`),
path.join(TOKENS_TARGET_DIR, `primitives/modes/colors/${mode}/global.json`),
{ recursive: true },
);

// Create theme primitives for the color mode
const template = await fs.readFile(
path.join(TOKEN_TEMPLATE_FILES_PATH, `primitives/colors/${mode}/theme-template.json`),
path.join(TOKEN_TEMPLATE_FILES_PATH, `primitives/modes/colors/${mode}/theme-template.json`),
);
await fs.writeFile(
path.join(TOKENS_TARGET_DIR, `primitives/colors/${mode}/${theme}.json`),
path.join(TOKENS_TARGET_DIR, `primitives/modes/colors/${mode}/${theme}.json`),
template.toString('utf-8').replaceAll('<theme>', theme),
);

// Create theme primitives for primary typography
const templatePrimaryTypo = await fs.readFile(
path.join(TOKEN_TEMPLATE_FILES_PATH, `primitives/modes/typography/primary/theme-template.json`),
);
await fs.writeFile(
path.join(TOKENS_TARGET_DIR, `primitives/modes/typography/primary/${theme}.json`),
templatePrimaryTypo.toString('utf-8').replaceAll('<theme>', theme),
);

// Create theme primitives for secondary typography
const templateSecondaryTypo = await fs.readFile(
path.join(TOKEN_TEMPLATE_FILES_PATH, `primitives/modes/typography/secondary/theme-template.json`),
);

await fs.writeFile(
path.join(TOKENS_TARGET_DIR, `primitives/modes/typography/secondary/${theme}.json`),
templateSecondaryTypo.toString('utf-8').replaceAll('<theme>', theme),
);
}

// Create main theme token set
Expand Down
10 changes: 6 additions & 4 deletions packages/cli/src/init/generateMetadataJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ interface Metadata {
export default function generateMetadataJson(modes: Array<'Light' | 'Dark' | 'Contrast'>, themes: string[]): Metadata {
return {
tokenSetOrder: [
'primitives/globals',
'primitives/typography/default',
'primitives/modes/globals',
'primitives/modes/typography/default',
...modes.flatMap((mode) => [
`primitives/colors/${normalizeTokenSetName(mode)}/global`,
...themes.map((theme) => `primitives/colors/${normalizeTokenSetName(mode)}/${normalizeTokenSetName(theme)}`),
`primitives/modes/colors/${normalizeTokenSetName(mode)}/global`,
...themes.map(
(theme) => `primitives/modes/colors/${normalizeTokenSetName(mode)}/${normalizeTokenSetName(theme)}`,
),
]),
...themes.map((theme) => `themes/${normalizeTokenSetName(theme)}`),
'semantic/color',
Expand Down
62 changes: 58 additions & 4 deletions packages/cli/src/init/generateThemesJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,41 @@ import { randomUUID } from 'node:crypto';

import { type ThemeObject, TokenSetStatus } from '@tokens-studio/types';

import { Theme } from '@adobe/leonardo-contrast-colors';
import { normalizeTokenSetName } from './utils.js';

export default function generateThemesJson(
modes: Array<'Light' | 'Dark' | 'Contrast'>,
themes: string[],
): ThemeObject[] {
return [...generateModesGroup(modes, themes), ...generateThemesGroup(themes), generateSemanticGroup()];
return [
...generateSizeGroup(),
...generateThemesGroup(themes),
...generateTypographyGroup(themes),
...generateModesGroup(modes, themes),
generateSemanticGroup(),
];
}

function generateSizeGroup(): ThemeObject[] {
return [
{
id: randomUUID(),
name: 'default',
selectedTokenSets: {
'primitives/size/default': TokenSetStatus.ENABLED,
},
group: 'Size',
},
{
id: randomUUID(),
name: 'compact',
selectedTokenSets: {
'primitives/size/compact': TokenSetStatus.ENABLED,
},
group: 'Size',
},
];
}

function generateModesGroup(modes: Array<'Light' | 'Dark' | 'Contrast'>, themes: string[]): ThemeObject[] {
Expand All @@ -17,11 +45,11 @@ function generateModesGroup(modes: Array<'Light' | 'Dark' | 'Contrast'>, themes:
id: randomUUID(),
name: mode,
selectedTokenSets: Object.fromEntries([
[`primitives/colors/${normalizeTokenSetName(mode)}/global`, TokenSetStatus.ENABLED],
[`primitives/modes/colors/${normalizeTokenSetName(mode)}/global`, TokenSetStatus.ENABLED],
...themes.map(
(theme) =>
[
`primitives/colors/${normalizeTokenSetName(mode)}/${normalizeTokenSetName(theme)}`,
`primitives/modes/colors/${normalizeTokenSetName(mode)}/${normalizeTokenSetName(theme)}`,
TokenSetStatus.ENABLED,
] as const,
),
Expand Down Expand Up @@ -52,8 +80,34 @@ function generateSemanticGroup(): ThemeObject {
'semantic/style': TokenSetStatus.ENABLED,
'semantic/color': TokenSetStatus.ENABLED,
'primitives/globals': TokenSetStatus.SOURCE,
'primitives/typography/default': TokenSetStatus.SOURCE,
},
group: 'Semantic',
};
}

function generateTypographyGroup(themes: string[]): ThemeObject[] {
return [
{
id: randomUUID(),
name: 'primary',
selectedTokenSets: Object.fromEntries(
themes.map((theme) => [
`primitives/modes/typography/primary/${normalizeTokenSetName(theme)}`,
TokenSetStatus.ENABLED,
]),
),
group: 'Typography',
},
{
id: randomUUID(),
name: 'secondary',
selectedTokenSets: Object.fromEntries(
themes.map((theme) => [
`primitives/modes/typography/secondary/${normalizeTokenSetName(theme)}`,
TokenSetStatus.ENABLED,
]),
),
group: 'Typography',
},
];
}
4 changes: 2 additions & 2 deletions packages/cli/src/init/nextStepsMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export function nextStepsMarkdown(themes: string[], modes: Mode[], tokensTargetD
1. Go to https://theme.designsystemet.no and set up a color theme
2. Press "Kopier tema"
3. Under "Json til Figma", copy the contents under ${modes.join(' / ')} to
the corresponding file under \`${tokensTargetDir}\`:
${themeModeCombinations.map(([theme, mode]) => ` **${theme}, ${mode}**: \`primitives/colors/${normalizeTokenSetName(mode)}/${normalizeTokenSetName(theme)}.json\` `).join('\n')}
the corresponding file under \`${tokensTargetDir}\`:
${themeModeCombinations.map(([theme, mode]) => ` **${theme}, ${mode}**: \`primitives/modes/colors/${normalizeTokenSetName(mode)}/${normalizeTokenSetName(theme)}.json\` `).join('\n')}
This can also be done in Tokens Studio for Figma.
4. **IMPORTANT!** In the JSON data you copied, replace \`theme\` on line 2
with the correct theme identifier, depending on the theme you're customizing.
Expand Down
Loading
Loading