Skip to content

Commit

Permalink
feat: Add codemod for css new subtle/strong variables (#1839)
Browse files Browse the repository at this point in the history
  • Loading branch information
Barsnes authored Apr 18, 2024
1 parent f2e35fc commit f9890c0
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 4 deletions.
12 changes: 9 additions & 3 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"repository": "https://github.com/digdir/designsystemet",
"homepage": "https://github.com/digdir/designsystemet/tree/main/scripts/cli",
"license": "MIT",
"type": "module",
"private": true,
"publishConfig": {
"access": "public"
Expand All @@ -15,19 +14,26 @@
"start": "esno ./src/build.ts",
"build": "yarn clean && yarn start -t ../../design-tokens -b Digdir Tilsynet Altinn Brreg",
"prepublish": "tsc",
"clean": "yarn workspace @digdir/designsystemet-theme clean"
"clean": "yarn workspace @digdir/designsystemet-theme clean",
"css-light-dark-codemod": "esno ./src/codemods/migrations/light-dark.ts"
},
"files": [
"./dist/**"
],
"bin": {
"designsystemet": "./dist/build/build.js"
},
"dependencies": {
"esno": "^4.7.0",
"fast-glob": "^3.3.2",
"postcss": "^8.4.38"
},
"devDependencies": {
"@tokens-studio/sd-transforms": "^0.9.3",
"@types/glob": "^8.1.0",
"@types/node": "^20.12.7",
"@types/ramda": "^0.29.9",
"@types/yargs": "^17.0.32",
"esno": "^4.7.0",
"ramda": "^0.29.1",
"rimraf": "^5.0.5",
"style-dictionary": "^3.8.0",
Expand Down
38 changes: 38 additions & 0 deletions packages/cli/src/codemods/css-var-codemod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import fs from 'fs';

import type { AcceptedPlugin, Plugin } from 'postcss';
import postcss from 'postcss';
import glob from 'fast-glob';

export const cssVarCodemod = (tokens: Record<string, string>) => {
const transformPlugin: Plugin = {
postcssPlugin: 'Dark/Light tokens to Strong/Subtle tokens',
Declaration(decl) {
Object.keys(tokens).forEach((key) => {
if (decl.value.includes(key)) {
decl.value = decl.value.replace(key, tokens[key]);
}
});
},
};

const plugins: AcceptedPlugin[] = [transformPlugin];

const processor = postcss(plugins);

const transform = async () => {
const files = glob.sync('./**/*.css');

const filePromises = files.map(async (file) => {
const contents = fs.readFileSync(file).toString();
const result = await processor.process(contents, { from: undefined });

fs.writeFileSync(file, result.css);
});

await Promise.all(filePromises);
};

// Run the transform.
void transform();
};
29 changes: 29 additions & 0 deletions packages/cli/src/codemods/migrations/light-dark.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { cssVarCodemod } from '../css-var-codemod';

void cssVarCodemod({
'--fds-semantic-surface-first-light': '--fds-semantic-surface-first-subtle',
'--fds-semantic-surface-first-light-hover':
'--fds-semantic-surface-first-subtle-hover',
'--fds-semantic-surface-first-light-active':
'--fds-semantic-surface-first-subtle-active',
'--fds-semantic-surface-first-dark': '--fds-semantic-surface-first-strong',
'--fds-semantic-surface-second-light': '--fds-semantic-surface-second-subtle',
'--fds-semantic-surface-second-light-hover':
'--fds-semantic-surface-second-subtle-hover',
'--fds-semantic-surface-second-light-active':
'--fds-semantic-surface-second-subtle-active',
'--fds-semantic-surface-second-dark': '--fds-semantic-surface-second-strong',
'--fds-semantic-surface-third-light': '--fds-semantic-surface-third-subtle',
'--fds-semantic-surface-third-light-hover':
'--fds-semantic-surface-third-subtle-hover',
'--fds-semantic-surface-third-light-active':
'--fds-semantic-surface-third-subtle-active',
'--fds-semantic-surface-third-dark': '--fds-semantic-surface-third-strong',
'--fds-semantic-surface-neutral-dark':
'--fds-semantic-surface-neutral-strong',
'--fds-semantic-surface-neutral-dark-hover':
'--fds-semantic-surface-neutral-strong-hover',
'--fds-semantic-border-action-dark': '--fds-semantic-border-action-strong',
'--fds-semantic-border-action-dark-hover':
'--fds-semantic-border-action-strong-hover',
});
5 changes: 5 additions & 0 deletions packages/cli/src/test/a.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.class {
margin-top: 42px;
color: var(--fds-semantic-surface-first-subtle);
background-color: #fff;
}
25 changes: 24 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2601,9 +2601,13 @@ __metadata:
resolution: "@digdir/designsystemet-cli@workspace:packages/cli"
dependencies:
"@tokens-studio/sd-transforms": "npm:^0.9.3"
"@types/glob": "npm:^8.1.0"
"@types/node": "npm:^20.12.7"
"@types/ramda": "npm:^0.29.9"
"@types/yargs": "npm:^17.0.32"
esno: "npm:^4.7.0"
fast-glob: "npm:^3.3.2"
postcss: "npm:^8.4.38"
ramda: "npm:^0.29.1"
rimraf: "npm:^5.0.5"
style-dictionary: "npm:^3.8.0"
Expand Down Expand Up @@ -5703,6 +5707,16 @@ __metadata:
languageName: node
linkType: hard

"@types/glob@npm:^8.1.0":
version: 8.1.0
resolution: "@types/glob@npm:8.1.0"
dependencies:
"@types/minimatch": "npm:^5.1.2"
"@types/node": "npm:*"
checksum: 9101f3a9061e40137190f70626aa0e202369b5ec4012c3fabe6f5d229cce04772db9a94fa5a0eb39655e2e4ad105c38afbb4af56a56c0996a8c7d4fc72350e3d
languageName: node
linkType: hard

"@types/hast@npm:^2.0.0":
version: 2.3.4
resolution: "@types/hast@npm:2.3.4"
Expand Down Expand Up @@ -5804,7 +5818,7 @@ __metadata:
languageName: node
linkType: hard

"@types/minimatch@npm:*":
"@types/minimatch@npm:*, @types/minimatch@npm:^5.1.2":
version: 5.1.2
resolution: "@types/minimatch@npm:5.1.2"
checksum: 94db5060d20df2b80d77b74dd384df3115f01889b5b6c40fa2dfa27cfc03a68fb0ff7c1f2a0366070263eb2e9d6bfd8c87111d4bc3ae93c3f291297c1bf56c85
Expand Down Expand Up @@ -5857,6 +5871,15 @@ __metadata:
languageName: node
linkType: hard

"@types/node@npm:^20.12.7":
version: 20.12.7
resolution: "@types/node@npm:20.12.7"
dependencies:
undici-types: "npm:~5.26.4"
checksum: b4a28a3b593a9bdca5650880b6a9acef46911d58cf7cfa57268f048e9a7157a7c3196421b96cea576850ddb732e3b54bc982c8eb5e1e5ef0635d4424c2fce801
languageName: node
linkType: hard

"@types/normalize-package-data@npm:^2.4.0":
version: 2.4.1
resolution: "@types/normalize-package-data@npm:2.4.1"
Expand Down

0 comments on commit f9890c0

Please sign in to comment.