-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(web-react): Add spirit-codemod package
- Added codemod-spirit package with first transform for buttonLabel->buttonText - Added test to this transform
- Loading branch information
1 parent
07742b8
commit 1e9fc85
Showing
20 changed files
with
301 additions
and
14 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ node_modules | |
|
||
# Caches | ||
.cache | ||
.DS_Store | ||
|
||
# Log files | ||
*.log | ||
|
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 |
---|---|---|
@@ -1,5 +1,42 @@ | ||
# @lmc-eu/spirit-codemods | ||
|
||
> Codemods for migration to the newer version of the Spirit Design library. | ||
`spirit-codemods` is a **CLI tool** designed to assist you in migrating to the latest version of our Spirit Design System library. This tool efficiently handles the removal of breaking changes and deprecations with simple commands. | ||
|
||
For React transformations, it utilizes the [jscodeshift][jscodeshift] library. | ||
|
||
## Install | ||
|
||
No installation of this package is necessary; you can run it using `npx`. | ||
|
||
## Usage | ||
|
||
To view the available arguments for this package, use `-h` or `--help` as shown in the example below: | ||
|
||
```shell | ||
npx @lmc-eu/spirit-codemods -h | ||
``` | ||
|
||
There are **two mandatory arguments**: `-p`/`--path` and `-t`/`--transformation`. | ||
The former specifies the directory path where you want to execute transforms, while the latter specifies the desired codemod to run. | ||
|
||
```shell | ||
npx @lmc-eu/spirit-codemods -p ./ -t v2/web-react/codemod-name | ||
``` | ||
|
||
Other optional arguments include: | ||
|
||
- `-v`/`--version` - Displays current version | ||
- `-h`/`--help` - Displays this message | ||
- `-e`/`--extensions` - Extensions of the transformed files, default: `ts,tsx,js,jsx` | ||
- `--parser` - Parser to use (babel, ts, tsx, flow), default: `tsx` | ||
- `--ignore` - Ignore files or directories, default: `**/node_modules/**` | ||
|
||
For example, this could be the command you will run: | ||
|
||
```shell | ||
npx @lmc-eu/spirit-codemods -p ./src -t v2/web-react/button-text -e js,jsx --parser babel | ||
``` | ||
|
||
[jscodeshift]: https://github.com/facebook/jscodeshift |
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,42 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
const sourcePath = './src/transforms'; | ||
const destinationPath = './dist/transforms'; | ||
const excludedDirectories = ['__tests__', '__testfixtures__']; | ||
|
||
function copyFolderRecursive(src, dest) { | ||
// Check if the source path exists | ||
if (!fs.existsSync(src)) { | ||
throw new Error(`Source path doesn't exist: ${src}`); | ||
} | ||
|
||
// Create destination folder if it doesn't exist | ||
if (!fs.existsSync(dest)) { | ||
fs.mkdirSync(dest); | ||
} | ||
|
||
// Get all files and subdirectories in the source path | ||
const items = fs.readdirSync(src); | ||
|
||
// Copy each item to the destination path | ||
items.forEach((item) => { | ||
const srcPath = path.join(src, item); | ||
const destPath = path.join(dest, item); | ||
|
||
// Check if the item is a directory | ||
if (fs.statSync(srcPath).isDirectory()) { | ||
// Check if the directory is not in the excluded list | ||
if (!excludedDirectories.includes(item)) { | ||
// Recursively copy the directory | ||
copyFolderRecursive(srcPath, destPath); | ||
} | ||
} else { | ||
// Copy the file | ||
fs.copyFileSync(srcPath, destPath); | ||
} | ||
}); | ||
} | ||
|
||
// Call the function to start the copy process | ||
copyFolderRecursive(sourcePath, destinationPath); |
This file was deleted.
Oops, something went wrong.
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 @@ | ||
#!/usr/bin/env node | ||
// eslint-disable-next-line import/no-unresolved -- The import is relative to the `dist` root | ||
import { cli } from '../index.js'; | ||
|
||
cli(process.argv); |
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,55 @@ | ||
import { $ } from 'execa'; | ||
import sade from 'sade'; | ||
import { fs, path } from 'zx'; | ||
import { _dirname, errorMessage, logMessage } from './helpers'; | ||
|
||
const packageJson = fs.readJsonSync(path.resolve(_dirname, './package.json')); | ||
|
||
export default async function cli(args: string[]) { | ||
sade('spirit-codemods', true) | ||
.version(packageJson.version) | ||
.describe(packageJson.description) | ||
.option('-p, --path', 'Path to the code to be transformed') | ||
.example('-p ./') | ||
.option('-t, --transformation', 'Codemod transformation name to run') | ||
.example('-t v2/web-react/codemodName') | ||
.option('-e, --extensions', 'Extensions to look for when transforming files, default: ts,tsx,js,jsx') | ||
.example('-e ts, tsx, js, jsx') | ||
.option('-i, --ignore', 'Ignore files or directories, default: **/node_modules/**') | ||
.example('-i **/node_modules/**') | ||
.option('-r, --parser', 'Parser to use (babel, ts, tsx, flow), default: tsx') | ||
.example('--parser babel') | ||
.action(async ({ path: codePath, transformation, extensions, ignore, parser }) => { | ||
const defaultExtensions = 'ts,tsx,js,jsx'; | ||
const defaultIgnore = '**/node_modules/**'; | ||
const defaultParser = 'tsx'; | ||
|
||
if (!codePath || !fs.existsSync(codePath)) { | ||
errorMessage(codePath); | ||
errorMessage('Please provide a valid path'); | ||
process.exit(1); | ||
} | ||
|
||
if (!transformation) { | ||
errorMessage('Please provide a codemod name'); | ||
process.exit(1); | ||
} | ||
|
||
const codemodPath = path.resolve(_dirname, `./transforms/${transformation}.ts`); | ||
|
||
if (!fs.existsSync(codemodPath)) { | ||
errorMessage('Codemod does not exists'); | ||
process.exit(1); | ||
} | ||
|
||
const { stdout } = await $`jscodeshift --transform ${codemodPath} --extensions ${ | ||
extensions || defaultExtensions | ||
} --ignore-pattern=${ignore || defaultIgnore} --parser=${parser || defaultParser} ${codePath}`; | ||
|
||
// stdout object from jscodeshift | ||
logMessage(stdout); | ||
|
||
process.exit(0); | ||
}) | ||
.parse(args); | ||
} |
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,4 @@ | ||
import { errorMessage, infoMessage, logMessage } from './message'; | ||
import { _dirname } from './path'; | ||
|
||
export { _dirname, errorMessage, infoMessage, logMessage }; |
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,7 @@ | ||
// Here we are defining function with only usage of Console | ||
/* eslint-disable no-console */ | ||
import { chalk } from 'zx'; | ||
|
||
export const errorMessage = (message: string) => console.error(chalk.red(message)); | ||
export const infoMessage = (message: string) => console.info(chalk.magenta.bold(message)); | ||
export const logMessage = (message: string) => console.log(message); |
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,3 @@ | ||
import filedirname from 'filedirname'; | ||
|
||
export const [_filename, _dirname] = filedirname(); |
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,3 @@ | ||
import cli from './cli'; | ||
|
||
export { cli }; |
5 changes: 5 additions & 0 deletions
5
packages/codemods/src/transforms/v2/web-react/__testfixtures__/button-text.input.tsx
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 @@ | ||
import React from 'react'; | ||
// @ts-ignore | ||
import { Button } from '@lmc-eu/spirit-web-react'; | ||
|
||
export const MyComponent = () => <Button buttonLabel="Click me" />; |
5 changes: 5 additions & 0 deletions
5
packages/codemods/src/transforms/v2/web-react/__testfixtures__/button-text.output.tsx
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 @@ | ||
import React from 'react'; | ||
// @ts-ignore | ||
import { Button } from '@lmc-eu/spirit-web-react'; | ||
|
||
export const MyComponent = () => <Button buttonText="Click me" />; |
8 changes: 8 additions & 0 deletions
8
packages/codemods/src/transforms/v2/web-react/__tests__/button-text.test.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// eslint-disable-next-line import/extensions | ||
const { defineTest } = require('jscodeshift/dist/testUtils'); | ||
|
||
defineTest(__dirname, 'button-text', null, 'button-text', { | ||
parser: 'tsx', | ||
fixture: 'input', | ||
snapshot: true, | ||
}); |
54 changes: 54 additions & 0 deletions
54
packages/codemods/src/transforms/v2/web-react/button-text.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { API, FileInfo } from 'jscodeshift'; | ||
|
||
const transform = (fileInfo: FileInfo, api: API) => { | ||
const j = api.jscodeshift; | ||
const root = j(fileInfo.source); | ||
|
||
// Find import statements for the specific module and Button specifier | ||
const importStatements = root.find(j.ImportDeclaration, { | ||
// source: { | ||
// value: (value: string) => value === '@lmc-eu/spirit-web-react' || value === '@lmc-eu/spirit-web-react/components', | ||
// }, | ||
source: { | ||
value: (value: string) => /^@lmc-eu\/spirit-web-react(\/.*)?$/.test(value), | ||
}, | ||
}); | ||
|
||
// Check if the module is imported | ||
if (importStatements.length > 0) { | ||
const buttonSpecifier = importStatements.find(j.ImportSpecifier, { | ||
imported: { | ||
type: 'Identifier', | ||
name: 'Button', | ||
}, | ||
}); | ||
|
||
// Check if Button specifier is present | ||
if (buttonSpecifier.length > 0) { | ||
// Find Button components in the module | ||
const buttonComponents = root.find(j.JSXOpeningElement, { | ||
name: { | ||
type: 'JSXIdentifier', | ||
name: 'Button', | ||
}, | ||
}); | ||
|
||
// Rename 'buttonLabel' attribute to 'buttonText' | ||
buttonComponents | ||
.find(j.JSXAttribute, { | ||
name: { | ||
type: 'JSXIdentifier', | ||
name: 'buttonLabel', | ||
}, | ||
}) | ||
.forEach((attributePath) => { | ||
// Change attribute name to 'buttonText' | ||
attributePath.node.name.name = 'buttonText'; | ||
}); | ||
} | ||
} | ||
|
||
return root.toSource(); | ||
}; | ||
|
||
export default 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
Oops, something went wrong.