Skip to content

Commit

Permalink
fixup! Feat(web-react): Add codemod package
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelklibani committed Feb 27, 2024
1 parent ef9cd1a commit aa5576a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/codemods/config/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineConfig } from 'tsup';

export default defineConfig({
entry: ['src/index.ts'],
entry: ['src/index.ts', 'src/transforms/**/*.ts'],
format: ['esm', 'cjs'],
dts: true,
splitting: true,
Expand Down
Empty file modified packages/codemods/src/bin/spirit-codemods.js
100644 → 100755
Empty file.
36 changes: 23 additions & 13 deletions packages/codemods/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import sade from 'sade';
import { fs, path } from 'zx';
import { run as jscodeshift } from 'jscodeshift/src/Runner';
// TODO: solve this eslint error
// eslint-disable-next-line import/extensions
import { run as jscodeshift } from 'jscodeshift/src/Runner.js';
import { _dirname, errorMessage, infoMessage } from './helpers';

const packageJson = fs.readJsonSync(path.resolve(_dirname, './package.json'));

export default async function cli(args: string[]) {
sade('analytics', true)
sade('spirit-codemods', true)
.version(packageJson.version)
.describe(packageJson.description)
.option('-p, --path', 'Path to the code to be transformed')
Expand All @@ -15,27 +17,32 @@ export default async function cli(args: string[]) {
.example('-c v2/react/buttonText')
.option('-e --extensions', 'Extensions to look for when transforming files, default: ts, tsx, js, jsx')
.example('-e ts, tsx, js, jsx')
.action(async ({ codePath, codemod, extensions }) => {
.action(async ({ path: codePath, codemod, extensions }) => {
const defaultExtensions = 'ts, tsx, js, jsx';

if (!codePath || !codemod) {
errorMessage('Please provide a path and a codemod');

return;
if (codePath && !fs.existsSync(codePath)) {
errorMessage(codePath);
errorMessage('Please provide a valid path');
process.exit(1);
}

if (!fs.existsSync(codePath)) {
errorMessage('Path does not exists');
if (!codemod) {
errorMessage('Please provide a codemod name');
process.exit(1);
}

const codemodPath = path.resolve(_dirname, `./transforms/${codemod}.ts`);
infoMessage(`codePath: ${codePath}`);
infoMessage(`codemod: ${codemod}`);

const codemodPath = path.resolve(_dirname, `./transforms/${codemod}.js`);

infoMessage(`codemodPath: ${codemodPath}`);

if (!fs.existsSync(codemodPath)) {
errorMessage('Codemod does not exists');
process.exit(1);
}

const { default: transform } = await import(codemodPath);

const options = {
dry: false,
print: true,
Expand All @@ -45,7 +52,10 @@ export default async function cli(args: string[]) {

infoMessage(`Running codemod: ${codemod}`);

jscodeshift(codePath, transform, options);
await jscodeshift(codemodPath, [codePath], options).catch((err) => {
errorMessage(err);
process.exit(1);
});

infoMessage('Codemod finished');

Expand Down
3 changes: 1 addition & 2 deletions packages/codemods/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import cli from './cli';
import { buttonTextTransform } from './transforms/v2/react/buttonText';

export { cli, buttonTextTransform };
export { cli };
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ export const buttonTextTransform = (fileInfo: FileInfo, api: API) => {
const j = api.jscodeshift;
const root = j(fileInfo.source);

console.log('Running buttonText transform');

// Find import statements for the specific module and Button specifier
const importStatements = root.find(j.ImportDeclaration, {
source: {
value: '@lmc-eu/spirit-web-react',
value: '@lmc-eu/spirit-web-react' || '@lmc-eu/spirit-web-react/components',
},
});

Expand Down

0 comments on commit aa5576a

Please sign in to comment.