Skip to content

Commit

Permalink
Update class-name-updater package to support v6 (#620)
Browse files Browse the repository at this point in the history
* Add pfVersion option

* Update regex

* Fix regex

* Update test scripts

* Top level scripts

* Refactor scripts based on feedback

* Update README
  • Loading branch information
jessiehuff authored Mar 26, 2024
1 parent f3ae25e commit 2c88f49
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 10 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
"test:console": "pf-codemods --v6 --no-cache test/console/frontend",
"test:integreatly": "pf-codemods --v6 --no-cache test/tutorial-web-app",
"test:packages": "yarn get:packages && node --unhandled-rejections=strict packages/pf-codemods/index.js --v6 --no-cache test/packages",
"test:classnames": "yarn build:classnames && lerna run test --scope=@patternfly/class-name-updater --stream",
"test:classnames": "lerna run test --scope=@patternfly/class-name-updater --stream",
"test:classnames:v5": "yarn build:classnames && lerna run test:v5 --scope=@patternfly/class-name-updater --stream",
"test:classnames:v6": "yarn build:classnames && lerna run test:v6 --scope=@patternfly/class-name-updater --stream",
"get:packages": "node getPackages.js",
"generate": "yarn build:generators && plop",
"build": "lerna run build",
Expand Down
3 changes: 2 additions & 1 deletion packages/class-name-updater/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# class-name-updater

This utility automatically identifies Patternfly class names that need to be updated after the introduction of versioned class names in Patternfly v5.
This utility automatically identifies Patternfly class names that need to be updated after the introduction of versioned class names in Patternfly v5 or v6. Currently v5 is set at the default so if you are updating to v6, please use the `--pfVersion 6` option.

## Usage

Expand All @@ -25,6 +25,7 @@ Usage: @patternfly/class-name-updater [options] <path> [otherPaths...]

Options:
-V, --version output the version number
--pfVersion version of PatternFly you are upgrading to, pass as number (i.e. 6)
--extensions comma-delineated list of file extensions to update, by default includes css, scss, less, ts, tsx, js, jsx and md
--exclude comma-delineated list of files to exclude, files should include their path relative to where this utility is being called
--fix whether to run fixer
Expand Down
4 changes: 3 additions & 1 deletion packages/class-name-updater/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
"scripts": {
"build": "tsc --build --verbose ./tsconfig.json",
"clean": "rimraf ./dist",
"test": "node ./dist/cli.js test --exclude test/largeFile.js"
"test": "yarn build && yarn test:v5 && yarn test:v6",
"test:v5": "node ./dist/cli.js test --exclude test/largeFile.js",
"test:v6": "node ./dist/cli.js test --exclude test/largeFile.js --pfVersion 6"
},
"dependencies": {
"colors": "^1.4.0",
Expand Down
13 changes: 8 additions & 5 deletions packages/class-name-updater/src/classNameUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import { join } from "path";
import { isDir } from "./utils";
import { printDiff } from "./printDiff";

export async function classNameUpdate(globTarget: string, makeChange: boolean, fileTypesRegex: RegExp, excludeFiles: string[] = []) {
export async function classNameUpdate(globTarget: string, makeChange: boolean, fileTypesRegex: RegExp, excludeFiles: string[] = [], pfVersion: number) {
const acceptedFileTypesRegex = fileTypesRegex || /\.(s?css|less|(t|j)sx?|md)$/;

const changeNeededRegex = /(\b|\$)pf-([cul]|global|theme|color)-/g;
const version = "v5";
const previousVersion = pfVersion ? "-v" + (pfVersion - 1) : "";
const changeNeededRegex = new RegExp(
"(\\b|\\$)pf" + previousVersion + "-([cul]|global|theme|color)-",
"g"
);
const newVersion = pfVersion || 5

const files = sync(globTarget, { ignore: "**/node_modules/**" });
const includedFiles = files.filter((filePath: string) => !excludeFiles.includes(filePath))
Expand All @@ -31,7 +34,7 @@ export async function classNameUpdate(globTarget: string, makeChange: boolean, f

const newContent = fileContent.replace(
changeNeededRegex,
`$1pf-${version}-$2-`
`$1pf-v${newVersion}-$2-`
);

printDiff(file, fileContent, newContent, changeNeededRegex);
Expand Down
8 changes: 6 additions & 2 deletions packages/class-name-updater/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ program
"Comma-delineated list of files to exclude, files should include their path relative to where this utility is being called"
)
.option("--fix", "Whether to run fixer")
.option(
"--pfVersion <pfVersion>",
"Version of PatternFly to use, pass just the version number."
)
.action(runClassNameUpdate);

async function runClassNameUpdate(
path: string,
otherPaths: string,
options: { extensions: string; fix: boolean; exclude: string[] | undefined }
options: { extensions: string; fix: boolean; exclude: string[] | undefined; pfVersion: number;}
) {
let allPaths = [path, ...otherPaths];

Expand All @@ -45,7 +49,7 @@ async function runClassNameUpdate(
}

allPaths.forEach(async (path) => {
await classNameUpdate(path, options.fix, fileTypes, options.exclude);
await classNameUpdate(path, options.fix, fileTypes, options.exclude, options.pfVersion);
});
}

Expand Down
4 changes: 4 additions & 0 deletions packages/class-name-updater/test/test.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@
.pf-u-screen-reader > .pf-l-stack__item{
color: red;
}

.pf-v5-c-toolbar__item {
color: green;
}

0 comments on commit 2c88f49

Please sign in to comment.