-
Notifications
You must be signed in to change notification settings - Fork 19
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: CSS vars updater package #801
Open
adamviktora
wants to merge
19
commits into
patternfly:main
Choose a base branch
from
adamviktora:css-vars-updater
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
ed78998
refactor(tokenLists): change location, use Sets
adamviktora 64544ac
refactor(classNameUpdater utils): move to a shared helpers folder
adamviktora ce6121a
refactor(codemods): update to use new tokenLists structure
adamviktora 16324fb
feat: add new css-vars-updater package
adamviktora a33e15d
update yarn.lock
adamviktora 2b02879
refactor: move tokenLists
adamviktora 93e110d
revert mapping paths for helpers from baseUrl
adamviktora aa86311
update paths in package.json files to reflect new dist folder structure
adamviktora ac04c82
refactor: create shared-helpers package
adamviktora 1f97fe7
chore: update shared-helpers package to use index.ts
adamviktora 426bfb7
chore: update yarn classic to [email protected]
adamviktora 4760579
chore: update yarn.lock
adamviktora 59d15f4
chore: update github action
adamviktora c516618
chore: include dependencies in lerna build
adamviktora b657da2
docs: update README example
adamviktora 7b2478c
Merge branch 'main' into css-vars-updater
adamviktora ed0ed7b
update test file
adamviktora c3e96db
feat: print diff messages
adamviktora 7d3ebbc
chore(shared-helpers): make package private
adamviktora File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,8 @@ jobs: | |
- uses: actions/setup-node@v1 | ||
with: | ||
node-version: '18' | ||
- run: corepack enable | ||
- run: corepack prepare [email protected] --activate | ||
- uses: actions/cache@v2 | ||
id: yarn-cache | ||
name: Cache npm deps | ||
|
@@ -48,6 +50,8 @@ jobs: | |
- uses: actions/setup-node@v1 | ||
with: | ||
node-version: '18' | ||
- run: corepack enable | ||
- run: corepack prepare [email protected] --activate | ||
- uses: actions/cache@v2 | ||
id: yarn-cache | ||
name: Cache npm deps | ||
|
@@ -78,6 +82,8 @@ jobs: | |
- uses: actions/setup-node@v1 | ||
with: | ||
node-version: '18' | ||
- run: corepack enable | ||
- run: corepack prepare [email protected] --activate | ||
- uses: actions/cache@v2 | ||
id: yarn-cache | ||
name: Cache npm deps | ||
|
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 @@ | ||
nodeLinker: node-modules |
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
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
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,106 @@ | ||
# css-vars-updater | ||
|
||
This utility automatically identifies Patternfly CSS variables in v5 that need to be updated after the introduction of new [global tokens in Patternfly v6](https://www.patternfly.org/tokens/about-tokens). | ||
|
||
## Usage | ||
|
||
Requires Node.js >= 10. | ||
|
||
```sh | ||
npx @patternfly/css-vars-updater ./path-to-src | ||
``` | ||
|
||
Running this command will use default options, if you want a finer control over the updates, use an `-i` (or `--interactive`) option. It will start an interactive interface, where you can setup what files should be fixed and what tokens should be replaced: | ||
``` | ||
npx @patternfly/css-vars-updater ./path-to-src -i | ||
|
||
--> Enter file extensions to update (comma-separated): (scss,css,less,md) | ||
--> Do you want to exclude any files? (y/N) | ||
yes --> Enter files to exclude (comma-separated, relative paths) | ||
--> Do you want to run the fixer? (Y/n) | ||
--> Do you want to replace color variables with a temporary hot pink color variable? (Y/n) | ||
--> Do you want to replace other global variables with a matching token? (Y/n) | ||
--> Do you want to fix directional styles and how? E.g. PaddingLeft -> PaddingInlineStart (Use arrow keys) | ||
❯ Fix in left-to-right manner (English) | ||
Fix in right-to-left manner | ||
Fix in top-to-bottom manner | ||
Don’t fix | ||
``` | ||
|
||
Giving node more RAM can help for large codebases. | ||
|
||
```sh | ||
NODE_OPTIONS=--max-old-space-size=4096 npx @patternfly/css-vars-updater ./path-to-src | ||
``` | ||
|
||
### Options | ||
|
||
```sh | ||
Usage: @patternfly/css-vars-updater <path> [otherPaths...] | ||
|
||
Options: | ||
-V, --version output the version number | ||
-i, --interactive starts an interactive interface for finer control over the updates | ||
--fix whether to run fixer | ||
-h, --help display help for command | ||
``` | ||
|
||
### Example | ||
|
||
Using default options: | ||
|
||
In: | ||
|
||
```css | ||
.custom-class { | ||
/* Global non-color variables */ | ||
border-radius: var(--pf-v5-global--BorderRadius--lg); | ||
row-gap: var(--pf-v5-global--spacer--md); | ||
width: var(--pf-v5-global--arrow--width); | ||
|
||
/* Global color variables */ | ||
color: var(--pf-v5-global--Color--100); | ||
background-color: var(--pf-v5-global--BackgroundColor--200); | ||
|
||
/* Variables removed from v6 */ | ||
max-width: var(--pf-v5-c-accordion__toggle-text--MaxWidth); | ||
|
||
/* Variables staying in v6 */ | ||
height: var(--pf-v5-c-about-modal-box--Height); | ||
box-shadow: var(--pf-v5-c-alert--BoxShadow); | ||
|
||
/* Variables with direction updates */ | ||
padding-inline-start: var(--pf-v5-c-about-modal-box__brand--PaddingLeft); | ||
padding-inline-end: var(--pf-v5-c-about-modal-box__brand--PaddingRight); | ||
padding-block-start: var(--pf-v5-c-about-modal-box__brand--PaddingTop); | ||
padding-block-end: var(--pf-v5-c-about-modal-box__brand--PaddingBottom); | ||
} | ||
``` | ||
|
||
Out: | ||
|
||
```css | ||
.custom-class { | ||
/* Global non-color variables */ | ||
border-radius: var(--pf-t--global--border--radius--large); | ||
row-gap: var(--pf-t--global--spacer--md); | ||
width: var(--pf-v5-global--arrow--width); | ||
|
||
/* Global color variables */ | ||
color: var(--pf-t--temp--dev--tbd /* CODEMODS: original v5 color was:--pf-v5-global--Color--100 */); | ||
background-color: var(--pf-t--temp--dev--tbd /* CODEMODS: original v5 color was:--pf-v5-global--BackgroundColor--200 */); | ||
|
||
/* Variables removed from v6 */ | ||
max-width: var(--pf-v5-c-accordion__toggle-text--MaxWidth); | ||
|
||
/* Variables staying in v6 */ | ||
height: var(--pf-v6-c-about-modal-box--Height); | ||
box-shadow: var(--pf-v6-c-alert--BoxShadow); | ||
|
||
/* Variables with direction updates */ | ||
padding-inline-start: var(--pf-v6-c-about-modal-box__brand--PaddingInlineStart); | ||
padding-inline-end: var(--pf-v6-c-about-modal-box__brand--PaddingInlineEnd); | ||
padding-block-start: var(--pf-v6-c-about-modal-box__brand--PaddingBlockStart); | ||
padding-block-end: var(--pf-v6-c-about-modal-box__brand--PaddingBlockEnd); | ||
} | ||
``` |
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,32 @@ | ||
{ | ||
"name": "@patternfly/css-vars-updater", | ||
"version": "1.0.0", | ||
"description": "Utility to update Patternfly CSS variables when migrating from V5 to a new design token system in V6", | ||
"author": "Red Hat", | ||
"license": "MIT", | ||
"bin": { | ||
"css-vars-updater": "./dist/cli.js" | ||
}, | ||
"engines": { | ||
"node": ">=18" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"scripts": { | ||
"build": "tsc --build --verbose ./tsconfig.json", | ||
"clean": "rimraf ./dist" | ||
}, | ||
"dependencies": { | ||
"colors": "^1.4.0", | ||
"commander": "^5.1.0", | ||
"diff": "^5.1.0", | ||
"glob": "^10.2.6", | ||
"inquirer": "^12.0.1", | ||
"shared-helpers": "workspace:^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/diff": "^5.0.9", | ||
"typescript": "^5.4.2" | ||
} | ||
} |
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,9 @@ | ||
export type Answers = { | ||
fileExtensions: string; | ||
shouldExcludeFiles: boolean; | ||
filesToExclude?: string; | ||
fix: boolean; | ||
replaceGlobalColorsWithPink: boolean; | ||
replaceGlobalVars: boolean; | ||
direction: "ltr" | "rtl" | "ttb" | "none"; | ||
}; |
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,117 @@ | ||
#!/usr/bin/env node | ||
import { join } from "path"; | ||
import { Command } from "commander"; | ||
const program = new Command(); | ||
import inquirer from "inquirer"; | ||
|
||
import { isDir } from "shared-helpers"; | ||
import { cssVarsUpdate } from "./cssVarsUpdate"; | ||
import { Answers } from "./answers"; | ||
|
||
program | ||
.version(require("../package.json").version) | ||
.description("Update css variables from v5 to v6") | ||
.arguments("<path> [otherPaths...]") | ||
.option( | ||
"-i, --interactive", | ||
"Starts an interactive interface for better control over the updates" | ||
) | ||
.option("--fix", "Whether to run fixer") | ||
.action(runCssVarsUpdate); | ||
|
||
async function runCssVarsUpdate( | ||
path: string, | ||
otherPaths: string, | ||
options: { interactive: boolean; fix: boolean } | ||
) { | ||
const defaultAnswers: Answers = { | ||
fileExtensions: "", | ||
shouldExcludeFiles: false, | ||
fix: options.fix, | ||
replaceGlobalColorsWithPink: true, | ||
replaceGlobalVars: true, | ||
direction: "ltr", | ||
}; | ||
|
||
const answers: Answers = options.interactive | ||
? await inquirer.prompt([ | ||
{ | ||
type: "input", | ||
name: "fileExtensions", | ||
message: "Enter file extensions to update (comma-separated):", | ||
default: "scss,css,less,md", | ||
}, | ||
{ | ||
type: "confirm", | ||
name: "shouldExcludeFiles", | ||
message: "Do you want to exclude any files?", | ||
default: false, | ||
}, | ||
{ | ||
type: "input", | ||
name: "filesToExclude", | ||
message: "Enter files to exclude (comma-separated, relative paths):", | ||
when: (answers) => answers.shouldExcludeFiles === true, | ||
}, | ||
{ | ||
type: "confirm", | ||
name: "fix", | ||
message: "Do you want to run the fixer?", | ||
when: !options.fix, | ||
}, | ||
{ | ||
type: "confirm", | ||
name: "replaceGlobalColorsWithPink", | ||
message: | ||
"Do you want to replace color variables with a temporary hot pink color variable?", | ||
}, | ||
{ | ||
type: "confirm", | ||
name: "replaceGlobalVars", | ||
message: | ||
"Do you want to replace other global variables with a matching token?", | ||
}, | ||
{ | ||
type: "list", | ||
name: "direction", | ||
message: | ||
"Do you want to fix directional styles and how? E.g. PaddingLeft -> PaddingInlineStart", | ||
choices: [ | ||
{ | ||
name: "Fix in left-to-right manner (English)", | ||
value: "ltr", | ||
}, | ||
{ | ||
name: "Fix in right-to-left manner", | ||
value: "rtl", | ||
}, | ||
{ | ||
name: "Fix in top-to-bottom manner", | ||
value: "ttb", | ||
}, | ||
{ | ||
name: "Don’t fix", | ||
value: "none", | ||
}, | ||
], | ||
}, | ||
]) | ||
: defaultAnswers; | ||
|
||
let allPaths = [path, ...otherPaths]; | ||
|
||
// if all paths are resolved to be directories assume that they want to run on all contents of those directories | ||
const onlyDirs = await Promise.all(allPaths.map((path) => isDir(path))).then( | ||
(responses) => responses.every((isDirResult) => !!isDirResult) | ||
); | ||
|
||
if (onlyDirs) { | ||
allPaths = allPaths.map((path) => join(path, "**", "*")); | ||
} | ||
|
||
allPaths.forEach(async (path) => { | ||
await cssVarsUpdate(path, answers); | ||
}); | ||
} | ||
|
||
program.parse(process.argv); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want lerna managing and releasing the shared-helpers package? Or can it just be a private package that doesn't get published?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was meant to be a private package. It was necessary to add it to this packages list, so lerna can link the packages when building. I updated the
shared-helpers
package to be"private": true
.But if you see a reason to publish it, we can
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah nope, sounds good to me!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh actually..... if it's a full dep in the other packages rather than a devDep will it actually work without the package being published? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point, I don't know. LLM thinks it won't work, which makes sense given that shared-helpers won't be published anywhere. I at first thought that lerna can somehow bundle it into the other packages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like there are ways we could bundle it in, or maybe make it into a devDep, though just publishing it would probably be the fastest/easiest route.