|
| 1 | +import * as grit from '@getgrit/api'; |
| 2 | + |
| 3 | +export async function execute(options: grit.WorkflowOptions) { |
| 4 | + const transformResult = await grit.stdlib.transform( |
| 5 | + { |
| 6 | + objective: `You are an expert software engineer working on a migration from styled-components to Tailwind CSS. |
| 7 | + Given a styled component, you must migrate it a simple component with appropriate Tailwind classes.`, |
| 8 | + principles: ['Use the twMerge library to conditionally combine classes.'], |
| 9 | + model: 'slow', |
| 10 | + query: 'or { js"styled.$_`$_`", js"styled($_)`$_`" }', |
| 11 | + examples: [ |
| 12 | + { |
| 13 | + input: `const StyledComponent = styled(({ backgroundColor, ...otherProps }) => ( |
| 14 | + <MyContainer {...otherProps} /> |
| 15 | +))\` |
| 16 | + position: relative; |
| 17 | + background-color: \${({ backgroundColor }) => backgroundColor}; |
| 18 | +\`;`, |
| 19 | + replacements: [ |
| 20 | + `({backgroundColor, ...otherProps}) => { |
| 21 | + const bgColorClass = backgroundColor ? \`bg-\${backgroundColor}\` : ''; |
| 22 | + const className = twMerge(\`relative \${bgColorClass}\`); |
| 23 | + return ( |
| 24 | + <MyContainer className={className} {...otherProps} /> |
| 25 | + ); |
| 26 | +}`, |
| 27 | + ], |
| 28 | + }, |
| 29 | + ], |
| 30 | + }, |
| 31 | + {}, |
| 32 | + ); |
| 33 | + if (!transformResult.success) return transformResult; |
| 34 | + await grit.stdlib.apply( |
| 35 | + { |
| 36 | + query: `js"twMerge" as $mg where { $mg <: ensure_import_from(source=js"'tailwind-merge'") }`, |
| 37 | + }, |
| 38 | + { paths: transformResult.paths }, |
| 39 | + ); |
| 40 | + await grit.stdlib.apply( |
| 41 | + { |
| 42 | + query: `js"import $_ from 'styled-components'" => .`, |
| 43 | + }, |
| 44 | + { paths: transformResult.paths }, |
| 45 | + ); |
| 46 | + return { |
| 47 | + success: true, |
| 48 | + message: `Successfully migrated ${transformResult.paths.length} files.`, |
| 49 | + }; |
| 50 | +} |
0 commit comments