Skip to content

Commit

Permalink
refactor: optimize clean command by precomputing paths to clean (#855)
Browse files Browse the repository at this point in the history
## **Description**

This PR optimizes the `_cleanPackage` method by precomputing the paths
to clean (`pathsToClean`) before iterating through them. Specifically,
relative paths are joined with the package root using `p.join` ahead of
time, avoiding repeated path computations during the loop.

### **Key Changes**:
**Precomputing Paths**:
- `pathsToClean` now contains fully resolved paths created by combining
relative paths (`cleanablePubFilePaths` and `.dart_tool`) with the
package root.
- This eliminates the need for `p.join` operations inside the loop,
improving both performance and code readability.

---

## **Type of Change**

- [ ] ✨ `feat` -- New feature (non-breaking change which adds
functionality)
- [ ] 🛠️ `fix` -- Bug fix (non-breaking change which fixes an issue)  
- [ ] ❌ `!` -- Breaking change (fix or feature that would cause existing
functionality to change)
- [x] 🧹 `refactor` -- Code refactor  
- [ ] ✅ `ci` -- Build configuration change  
- [ ] 📝 `docs` -- Documentation  
- [ ] 🗑️ `chore` -- Chore  

---

## **Checklist**

<!--- Put an `x` in all the boxes that apply: -->

- [x] I have read the
[[CONTRIBUTING](https://github.com/invertase/melos/blob/main/CONTRIBUTING.md)](https://github.com/invertase/melos/blob/main/CONTRIBUTING.md)
guidelines.
- [x] I have read the [[Invertase's Code of
Conduct](https://github.com/invertase/meta/blob/main/CODE_OF_CONDUCT.md)](https://github.com/invertase/meta/blob/main/CODE_OF_CONDUCT.md).
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added necessary documentation (if applicable).
- [x] I have run `melos bootstrap` and confirmed it is working as
expected locally.
  • Loading branch information
suojae authored Jan 25, 2025
1 parent 4be680c commit daba40d
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions packages/melos/lib/src/commands/clean.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ mixin _CleanMixin on _Melos {
final pathsToClean = [
...cleanablePubFilePaths,
'.dart_tool',
];
].map((relativePath) => p.join(package.path, relativePath));

for (final generatedPubFilePath in pathsToClean) {
deleteEntry(p.join(package.path, generatedPubFilePath));
for (final path in pathsToClean) {
try {
deleteEntry(path);
} catch (error) {
logger.warning('Failed to delete $path: $error');
}
}
}

Expand Down

0 comments on commit daba40d

Please sign in to comment.