-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add codemod for css new subtle/strong variables (#1839)
- Loading branch information
Showing
5 changed files
with
105 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters