@wise/wds-codemods 0.0.1-experimental-2eb5228 → 0.0.1-experimental-0d8d466

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -12,15 +12,19 @@
12
12
  - [The Repository](#-the-repository)
13
13
  - [Getting started](#-getting-started)
14
14
  - [Commands](#commands)
15
+ - [Key Features](#-key-features)
16
+ - [Available Transforms](#-available-transforms)
15
17
  - [Working with the Project Locally](#-working-with-the-project-locally)
16
18
  - [Writing Codemod Transforms](#-writing-codemod-transforms)
19
+ - [Developer Documentation](#-developer-documentation)
17
20
  - [Notes on Key Tools](#-notes-on-key-tools)
18
21
  - [Feedback](#-feedback)
19
22
 
20
23
  ## 👨‍💻 The Repository
21
24
 
22
25
  The project provides a flexible CLI interface that allows you to run codemods either interactively
23
- via prompts or directly through command-line arguments.
26
+ via prompts or directly through command-line arguments. It includes intelligent package validation,
27
+ monorepo support, and comprehensive reporting for manual review cases.
24
28
 
25
29
  ## 🚀 Getting started
26
30
 
@@ -30,7 +34,27 @@ arguments. Here's how to do both:
30
34
  ### To get started, install the package
31
35
 
32
36
  ```bash
37
+ # Using npm
33
38
  npm i -g @wise/wds-codemods
39
+
40
+ # Using pnpm
41
+ pnpm add -g @wise/wds-codemods
42
+
43
+ # Using yarn
44
+ yarn global add @wise/wds-codemods
45
+ ```
46
+
47
+ Or, if you prefer, you can run it directly without installing globally:
48
+
49
+ ```bash
50
+ # Using npx
51
+ npx @wise/wds-codemods
52
+
53
+ # Using pnpm
54
+ pnpm dlx @wise/wds-codemods
55
+
56
+ # Using yarn
57
+ yarn dlx @wise/wds-codemods
34
58
  ```
35
59
 
36
60
  ### Using Interactive Prompts
@@ -53,36 +77,83 @@ You will be prompted to:
53
77
  - Enter the target directory or file path to apply the codemod.
54
78
  - Choose whether to run in dry mode (no files are modified).
55
79
  - Choose whether to print the transformed source code to the console.
80
+ - Configure monorepo detection (automatically detected in most cases).
56
81
 
57
82
  ### Using CLI Arguments
58
83
 
59
84
  You can also run codemods directly by providing arguments:
60
85
 
61
86
  ```bash
62
- wds-codemods <transformFile> <targetPath> [--dry] [--print]
87
+ wds-codemods <transform> <targetPath> [--dry] [--print] [--monorepo]
63
88
  ```
64
89
 
65
- Or using `npx`:
90
+ Or using package runners:
66
91
 
67
92
  ```bash
68
- npx wds-codemods <transformFile> <targetPath> [--dry] [--print]
93
+ npx @wise/wds-codemods <transform> <targetPath> [--dry] [--print] [--monorepo]
69
94
  ```
70
95
 
71
96
  ## Commands
72
97
 
73
98
  - `npx wds-codemods <transform> <targetPath>`: Run a specific codemod transform on the target path.
74
- - `--dry` or `--dry-run`: Run in dry mode without writing changes to files.
99
+ - `--dry` or `--dry-run`: Run in dry mode without writing changes to files. This is useful for previewing what changes would be made before actually applying them, allowing you to review the transformations safely.
75
100
  - `--print`: Print transformed source to the console.
76
- - `--ignore-pattern=GLOB`: Ignore files matching the provided glob pattern(s). Multiple patterns can be comma separated.
101
+ - `--ignore-pattern=GLOB`: Ignore files matching the provided [glob pattern(s)](https://code.visualstudio.com/docs/editor/glob-patterns). Multiple patterns can be comma separated.
77
102
  - `--gitignore`: Respect `.gitignore` files to ignore files/folders during codemod runs.
78
103
  - `--no-gitignore`: Do not respect `.gitignore` files.
104
+ - `--monorepo`: Enable monorepo package checking across multiple workspace folders.
79
105
 
80
- Example:
106
+ Examples:
81
107
 
82
108
  ```bash
83
- wds-codemods simple-rename ./src --dry
109
+ # Basic transform with dry run
110
+ wds-codemods button ./src --dry
111
+
112
+ # Transform with pattern exclusions
113
+ wds-codemods button ./src --ignore-pattern="*.test.ts,*.stories.ts"
114
+
115
+ # Ignore multiple directories and file types
116
+ wds-codemods button ./src --ignore-pattern="**/node_modules/**,**/*.test.ts,**/stories/**"
117
+
118
+ # Ignore specific directories
119
+ wds-codemods button ./src --ignore-pattern="dist/**,build/**,coverage/**"
120
+
121
+ # Monorepo transformation
122
+ wds-codemods button ./packages --monorepo
123
+
124
+ # Print output without writing files
125
+ wds-codemods button ./src --print --dry
84
126
  ```
85
127
 
128
+ ## 🔧 Key Features
129
+
130
+ ### Package Requirements Validation
131
+
132
+ - **Automatic Dependency Checking**: Validates required packages and versions before running transforms
133
+ - **Multi-Package Manager Support**: Works with npm, pnpm, and yarn
134
+ - **Smart Detection**: Checks package.json, lockfiles, and node_modules directories
135
+ - **Comprehensive Reporting**: Clear feedback when dependencies are missing or incompatible
136
+
137
+ ### Monorepo Support
138
+
139
+ - **Auto-Detection**: Automatically identifies monorepo structures (packages/, apps/, libs/, etc.)
140
+ - **Cross-Package Validation**: Checks dependencies across all workspace packages
141
+ - **Summary Reports**: Provides detailed breakdown of which packages have required dependencies
142
+ - **Flexible Configuration**: Manual monorepo mode for custom structures
143
+
144
+ ### Manual Review Reports
145
+
146
+ - **Automated Report Generation**: Creates `codemod-report.txt` for issues requiring manual attention
147
+ - **Detailed Context**: Includes file paths, line numbers, and specific issue descriptions
148
+ - **Smart Cleanup**: Automatically removes old reports and provides fresh summaries
149
+ - **Issue Categories**: Organised reporting for spread props, dynamic expressions, and unsupported values
150
+
151
+ ### Intelligent Processing
152
+
153
+ - **Selective Execution**: Only runs transforms on projects with compatible dependencies
154
+ - **Performance Optimisation**: Caching and efficient directory traversal
155
+ - **Robust Error Handling**: Graceful handling of edge cases and invalid configurations
156
+
86
157
  ---
87
158
 
88
159
  ## 👨‍💻 Working with the Project Locally
@@ -126,9 +197,18 @@ standalone TypeScript file exporting a default function that follows the [jscode
126
197
  ### Example: Simple Rename Transform
127
198
 
128
199
  ```ts
129
- import type { API, FileInfo } from 'jscodeshift';
200
+ import type { API, FileInfo, Options } from 'jscodeshift';
201
+ import { validatePackageRequirements } from '../helpers';
202
+
203
+ // Define package requirements for this transform
204
+ export const packageRequirements = [{ name: '@wise/components', version: '>=2.0.0' }];
205
+
206
+ const transformer = (file: FileInfo, api: API, options: Options) => {
207
+ // Validate package requirements before running
208
+ if (!validatePackageRequirements(options, packageRequirements)) {
209
+ return file.source;
210
+ }
130
211
 
131
- const transformer = (file: FileInfo, api: API) => {
132
212
  const j = api.jscodeshift;
133
213
  const root = j(file.source);
134
214
 
@@ -144,15 +224,68 @@ export default transformer;
144
224
  ### Adding a New Transform
145
225
 
146
226
  1. Create a new `.ts` file in `src/transforms/your-transform-name/`.
147
- 2. Export a default function following the jscodeshift transformer signature.
148
- 3. Write unit tests for your transform using the `createTestTransform` utility found in `src/utils/createTestTransform.ts`.
149
- 4. Build the project to compile your transform.
150
- 5. Run the codemod runner and select your new transform.
227
+ 2. Export a `packageRequirements` array defining required dependencies.
228
+ 3. Export a default function following the jscodeshift transformer signature.
229
+ 4. Use helper utilities from `src/transforms/helpers/` for common operations.
230
+ 5. Write unit tests for your transform using the `createTestTransform` utility.
231
+ 6. Build the project to compile your transform.
232
+ 7. Run the codemod runner and select your new transform.
233
+
234
+ ### Package Requirements
235
+
236
+ Each transform should export a `packageRequirements` array that specifies which packages and versions are required:
237
+
238
+ ```ts
239
+ export const packageRequirements = [
240
+ { name: '@wise/components', version: '>=2.0.0' },
241
+ { name: '@wise/icons', version: '>=1.0.0' },
242
+ ];
243
+ ```
244
+
245
+ The system will automatically validate these requirements before running the transform, supporting:
246
+
247
+ - Semantic version ranges (`>=`, `^`, `~`, etc.)
248
+ - Multiple package managers (npm, pnpm, yarn)
249
+ - Monorepo structures
250
+ - Comprehensive dependency checking (dependencies, devDependencies, peerDependencies)
251
+
252
+ ### Helper Utilities
253
+
254
+ The codemod provides several helper utilities in `src/transforms/helpers/`:
255
+
256
+ - **`hasImport`** - Check for and manipulate import statements
257
+ - **`processIconChildren`** - Handle icon component transformations
258
+ - **`createReporter`** - Generate manual review reports with detailed context
259
+ - **`validatePackageRequirements`** - Check package dependencies
260
+ - **JSX Element Utils** - Utilities for manipulating JSX elements and attributes
261
+ - **JSX Reporting Utils** - Standardised reporting for common manual review scenarios
262
+
263
+ Additional utilities are available in `src/utils/` for common operations like file handling, AST manipulation, and reporting. As this project evolves, we encourage contributors to add new utilities that can benefit the broader codemod ecosystem.
151
264
 
152
265
  #### Writing Unit Tests for Transforms
153
266
 
154
267
  It is important that all codemod transforms have corresponding unit tests to ensure correctness and prevent regressions. Use the `createTestTransform` utility to simplify writing tests for your transforms. This utility helps set up the testing environment and provides helpers to run your transform against sample input and verify the output.
155
268
 
269
+ **Basic test setup:**
270
+
271
+ ```ts
272
+ import { createTestTransform } from '../../helpers/createTestTransform';
273
+ import transform from '../my-transform';
274
+
275
+ const testTransform = createTestTransform(transform, [
276
+ { name: '@wise/components', version: '2.0.0' },
277
+ ]);
278
+
279
+ describe('my-transform', () => {
280
+ it('should transform component', () => {
281
+ const input = `<OldComponent prop="value" />`;
282
+ const expected = `<NewComponent newProp="value" />`;
283
+
284
+ expect(testTransform({ source: input })).toBe(expected);
285
+ });
286
+ });
287
+ ```
288
+
156
289
  Example usage of `createTestTransform` can be found in the existing tests under `src/transforms/simple-rename/__tests__/simple-rename.test.ts`.
157
290
 
158
291
  Make sure to run your tests regularly using:
@@ -163,6 +296,12 @@ pnpm test
163
296
 
164
297
  ---
165
298
 
299
+ ## 📚 Developer Documentation
300
+
301
+ For comprehensive development details, including transform architecture, helper function documentation, testing patterns, and best practices, see our [Developer Documentation](./DEVELOPER.md).
302
+
303
+ ---
304
+
166
305
  ## 📝 Notes on Key Tools
167
306
 
168
307
  ### jscodeshift
@@ -177,6 +316,10 @@ This project uses jscodeshift as the core engine to perform code transformations
177
316
 
178
317
  This project uses @inquirer/prompts to provide a user-friendly interactive experience when running codemods without CLI arguments, allowing you to select transforms and options easily.
179
318
 
319
+ ### semver
320
+
321
+ [semver](https://github.com/npm/node-semver) is used for semantic version parsing and comparison when validating package requirements. This ensures accurate dependency checking across different version specification formats.
322
+
180
323
  ---
181
324
 
182
325
  ## ✍️ Feedback