-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ed0ed7b
commit c3e96db
Showing
7 changed files
with
176 additions
and
76 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
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,98 @@ | ||
import { | ||
globalNonColorCssVarNamesMap, | ||
oldCssVarNames, | ||
oldGlobalColorCssVarNames, | ||
oldGlobalNonColorCssVarNames, | ||
v6DirectionCssVars, | ||
} from "shared-helpers"; | ||
import { Answers } from "./answers"; | ||
import { getDirectionMap } from "./directionStyles"; | ||
|
||
export type CssVarReplacement = { | ||
newVar?: string; | ||
varWasRemoved?: boolean; | ||
directionReplacementExists?: boolean; | ||
} | null; | ||
|
||
const getGlobalNonColorVarReplacement = ( | ||
oldCssVar: string, | ||
shouldReplace: boolean | ||
): CssVarReplacement => { | ||
const newCssVar = | ||
globalNonColorCssVarNamesMap[ | ||
oldCssVar as keyof typeof globalNonColorCssVarNamesMap | ||
]; | ||
if (newCssVar === "SKIP") { | ||
return { varWasRemoved: true }; | ||
} | ||
if (shouldReplace) { | ||
return { newVar: newCssVar }; | ||
} | ||
return null; | ||
}; | ||
|
||
const getGlobalColorVarReplacement = ( | ||
oldCssVar: string, | ||
shouldReplace: boolean | ||
): CssVarReplacement => { | ||
if (shouldReplace) { | ||
return { | ||
newVar: `--pf-t--temp--dev--tbd /* CODEMODS: original v5 color was:${oldCssVar} */`, | ||
}; | ||
} | ||
return null; | ||
}; | ||
|
||
const getDirectionVarReplacement = ( | ||
oldCssVar: string, | ||
direction: Answers["direction"] | ||
): CssVarReplacement => { | ||
const directionMap = getDirectionMap(direction); | ||
const directions = Object.keys(directionMap); | ||
|
||
if (directions.some((dir) => oldCssVar.endsWith(dir))) { | ||
const newCssVar = oldCssVar | ||
.replace( | ||
/(Left|Right|Top|Bottom)$/, | ||
(match) => directionMap[match as keyof typeof directionMap] | ||
) | ||
.replace("v5", "v6"); | ||
|
||
if (v6DirectionCssVars.has(newCssVar)) { | ||
if (direction === "none") { | ||
return { directionReplacementExists: true }; | ||
} | ||
return { newVar: newCssVar }; | ||
} | ||
} | ||
return null; | ||
}; | ||
|
||
export const getCssVarReplacement = ( | ||
oldCssVar: string, | ||
answers: Answers | ||
): CssVarReplacement => { | ||
if (oldGlobalNonColorCssVarNames.has(oldCssVar)) { | ||
return getGlobalNonColorVarReplacement( | ||
oldCssVar, | ||
answers.replaceGlobalVars | ||
); | ||
} | ||
|
||
if (oldGlobalColorCssVarNames.has(oldCssVar)) { | ||
return getGlobalColorVarReplacement( | ||
oldCssVar, | ||
answers.replaceGlobalColorsWithPink | ||
); | ||
} | ||
|
||
if (oldCssVarNames.has(oldCssVar)) { | ||
return ( | ||
getDirectionVarReplacement(oldCssVar, answers.direction) ?? { | ||
varWasRemoved: true, | ||
} | ||
); | ||
} | ||
|
||
return { newVar: oldCssVar.replace("v5", "v6") }; | ||
}; |
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
2 changes: 1 addition & 1 deletion
2
...css-vars-updater/src/directionalStyles.ts → ...s/css-vars-updater/src/directionStyles.ts
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,39 @@ | ||
import colors from "colors"; | ||
import { CssVarReplacement } from "./cssVarReplacement"; | ||
|
||
const printFormatted = (lineNumber: number, ...args: string[]) => { | ||
console.log(`${lineNumber}: `, ...args); | ||
}; | ||
|
||
export const printLineChange = ( | ||
lineNumber: number, | ||
oldVar: string, | ||
replacement: CssVarReplacement | ||
) => { | ||
if (!replacement) { | ||
return; | ||
} | ||
|
||
if (replacement.varWasRemoved) { | ||
printFormatted(lineNumber, colors.yellow(oldVar), "was removed in v6"); | ||
return; | ||
} | ||
|
||
if (replacement.directionReplacementExists) { | ||
printFormatted( | ||
lineNumber, | ||
colors.yellow(oldVar), | ||
"may be replaced with an -InlineStart, -InlineEnd, -BlockStart or -BlockEnd variable" | ||
); | ||
return; | ||
} | ||
|
||
if (replacement.newVar) { | ||
printFormatted( | ||
lineNumber, | ||
colors.red(oldVar), | ||
"->", | ||
colors.green(replacement.newVar) | ||
); | ||
} | ||
}; |