From daba40d47a13999a9c5ca389a4a389afea931e91 Mon Sep 17 00:00:00 2001 From: suojae Date: Sat, 25 Jan 2025 23:48:31 +0900 Subject: [PATCH] refactor: optimize clean command by precomputing paths to clean (#855) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## **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** - [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. --- packages/melos/lib/src/commands/clean.dart | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/melos/lib/src/commands/clean.dart b/packages/melos/lib/src/commands/clean.dart index 7b9f1b63..dc8adb1b 100644 --- a/packages/melos/lib/src/commands/clean.dart +++ b/packages/melos/lib/src/commands/clean.dart @@ -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'); + } } }