Skip to content

Commit

Permalink
feat: Add the @digir/create-tokens CLI tool
Browse files Browse the repository at this point in the history
  • Loading branch information
unekinn committed Jun 14, 2024
1 parent 5def32d commit cf62c03
Show file tree
Hide file tree
Showing 26 changed files with 4,582 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/create-tokens/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
root: false,
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
},
};
5 changes: 5 additions & 0 deletions packages/create-tokens/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node

import { require } from 'tsx/cjs/api';

require('./src/index.ts', import.meta.url);
31 changes: 31 additions & 0 deletions packages/create-tokens/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "@digdir/create-tokens",
"version": "0.1.0-alpha.7",
"description": "CLI tool to create an initial token structure for Designsystemet",
"author": "Designsystemet team",
"repository": {
"type": "git",
"url": "git+https://github.com/digdir/designsystemet.git"
},
"homepage": "https://github.com/digdir/designsystemet/tree/main/packages/create-tokens",
"license": "MIT",
"type": "module",
"publishConfig": {
"access": "public"
},
"main": "index.js",
"bin": "index.js",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"change-case": "^5.3.0",
"kleur": "^3.0.3",
"prompts": "^2.4.0",
"tsx": "^4.11.2"
},
"devDependencies": {
"@tokens-studio/types": "^0.4.0",
"@types/prompts": "^2.4.9"
}
}
27 changes: 27 additions & 0 deletions packages/create-tokens/src/generateMetadataJson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { normalizeTokenSetName } from './utils.js';

interface Metadata {
tokenSetOrder: string[];
}

export default function generateMetadataJson(
modes: Array<'Light' | 'Dark' | 'Contrast'>,
themes: string[],
): Metadata {
return {
tokenSetOrder: [
'primitives/globals',
'primitives/typography/default',
...modes.flatMap((mode) => [
`primitives/colors/${normalizeTokenSetName(mode)}/global`,
...themes.map(
(theme) =>
`primitives/colors/${normalizeTokenSetName(mode)}/${normalizeTokenSetName(theme)}`,
),
]),
...themes.map((theme) => `themes/${normalizeTokenSetName(theme)}`),
'semantic/color',
'semantic/style',
],
};
}
69 changes: 69 additions & 0 deletions packages/create-tokens/src/generateThemesJson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { randomUUID } from 'node:crypto';

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

import { normalizeTokenSetName } from './utils.js';

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

function generateModesGroup(
modes: Array<'Light' | 'Dark' | 'Contrast'>,
themes: string[],
): ThemeObject[] {
return modes.map(
(mode): ThemeObject => ({
id: randomUUID(),
name: mode,
selectedTokenSets: Object.fromEntries([
[
`primitives/colors/${normalizeTokenSetName(mode)}/global`,
TokenSetStatus.ENABLED,
],
...themes.map(
(theme) =>
[
`primitives/colors/${normalizeTokenSetName(mode)}/${normalizeTokenSetName(theme)}`,
TokenSetStatus.ENABLED,
] as const,
),
]),
group: 'Mode',
}),
);
}

function generateThemesGroup(themes: string[]): ThemeObject[] {
return themes.map(
(theme): ThemeObject => ({
id: randomUUID(),
name: theme,
selectedTokenSets: {
[`themes/${normalizeTokenSetName(theme)}`]: TokenSetStatus.ENABLED,
},
group: 'Theme',
}),
);
}

function generateSemanticGroup(): ThemeObject {
return {
id: randomUUID(),
name: 'Semantic',
selectedTokenSets: {
'semantic/style': TokenSetStatus.ENABLED,
'semantic/color': TokenSetStatus.ENABLED,
'primitives/globals': TokenSetStatus.SOURCE,
'primitives/typography/default': TokenSetStatus.SOURCE,
},
group: 'Semantic',
};
}
Loading

0 comments on commit cf62c03

Please sign in to comment.