|
| 1 | +## The Command (PowerShell) |
| 2 | + |
| 3 | +I ran this specific script in your terminal. It uses "piping" (`|`) to pass the result of one command to the next, like a bucket brigade. |
| 4 | + |
| 5 | +```powershell |
| 6 | +Get-ChildItem -Path src -Recurse -Filter *.js | |
| 7 | + Where-Object { $_.Name -notin @('index.js', 'reportWebVitals.js', 'setupTests.js') } | |
| 8 | + ForEach-Object { |
| 9 | + if (Select-String -Pattern "<[a-zA-Z]" -Path $_.FullName -Quiet) { |
| 10 | + Write-Host "Renaming $($_.Name) to .jsx"; |
| 11 | + Rename-Item -LiteralPath $_.FullName -NewName ($_.Name -replace '\.js$', '.jsx') |
| 12 | + } |
| 13 | + } |
| 14 | +``` |
| 15 | + |
| 16 | +### Deep Dive: Commands & Arguments |
| 17 | + |
| 18 | +Here is exactly what every part of that spell does: |
| 19 | + |
| 20 | +**1. Finding the files** |
| 21 | +`Get-ChildItem -Path src -Recurse -Filter *.js` |
| 22 | +* `Get-ChildItem`: The standard command to list files (like `ls` or `dir`). |
| 23 | +* `-Path src`: We only look inside the `src` folder. |
| 24 | +* `-Recurse`: We dig deep into every subfolder, not just the top level. |
| 25 | +* `-Filter *.js`: We ignore everything except files ending in `.js`. |
| 26 | + |
| 27 | +**2. Filtering the list** |
| 28 | +`Where-Object { $_.Name -notin @(...) }` |
| 29 | +* `Where-Object`: Acts like a bouncer; only lets items through that match the condition. |
| 30 | +* `$_`: Represents the "current file" being checked. |
| 31 | +* `-notin`: The condition operator. We are saying "The name must NOT be in this list". |
| 32 | +* `@('index.js', ...)`: The list of system files we want to skip (leave as `.js`). |
| 33 | + |
| 34 | +**3. Processing each file** |
| 35 | +`ForEach-Object { ... }` |
| 36 | +* `ForEach-Object`: Runs the code block inside `{ ... }` for every single file that made it past the filter. |
| 37 | + |
| 38 | +**4. Checking for React Code (JSX)** |
| 39 | +`if (Select-String -Pattern "<[a-zA-Z]" -Path $_.FullName -Quiet)` |
| 40 | +* `Select-String`: Searches for text inside a file (like `grep`). |
| 41 | +* `-Pattern "<[a-zA-Z]"`: A Regex pattern. It looks for a `<` followed by a letter. This catches HTML tags like `<div>` or React components like `<App>`. |
| 42 | +* `-Path $_.FullName`: The full path to the file we are currently reading. |
| 43 | +* `-Quiet`: Important! This tells the command "Don't print the matching text, just tell me True or False." |
| 44 | + |
| 45 | +**5. Renaming the file** |
| 46 | +`Rename-Item -LiteralPath $_.FullName -NewName (...)` |
| 47 | +* `Rename-Item`: The command to change a file's name. |
| 48 | +* `-LiteralPath $_.FullName`: We use the full path to ensure we target the exact right file. |
| 49 | +* `-NewName ($_.Name -replace '\.js$', '.jsx')`: We calculate the new name by taking the old name and swapping the ending `.js` with `.jsx`. |
| 50 | + |
| 51 | +**The Result** |
| 52 | + |
| 53 | +Now your code editor knows exactly which files contain UI components. You'll get better autocomplete, better color highlighting, and generally a much happier development experience. |
0 commit comments