From 3971ac49789ba132e10394c43267c32f571bbd87 Mon Sep 17 00:00:00 2001 From: 43081j <43081j@users.noreply.github.com> Date: Wed, 7 Aug 2024 20:23:40 +0100 Subject: [PATCH] feat: cleanup spinner logic Moves various spinners/tasks around to ensure we show an error status when we detect replacements. --- src/main.ts | 61 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 16 deletions(-) diff --git a/src/main.ts b/src/main.ts index 81f88a2..22288af 100644 --- a/src/main.ts +++ b/src/main.ts @@ -425,6 +425,10 @@ const packageScanSpinner = cl.spinner(); packageScanSpinner.start('Scanning `package.json` dependencies'); +const dependenciesToRemove: string[] = []; +const devDependenciesToRemove: string[] = []; +let packageJsonFailed = false; + if (isDependenciesLike(packageDependencies)) { const traverseResults = traverseDependencies( packageDependencies, @@ -433,14 +437,12 @@ if (isDependenciesLike(packageDependencies)) { ); if (options.autoUninstall) { - const args = [ - 'rm', - '-S', - ...traverseResults.map((result) => result.match.moduleName) - ]; - - await x('npm', args); + for (const result of traverseResults) { + dependenciesToRemove.push(result.match.moduleName); + } } + + packageJsonFailed = true; } if ( @@ -454,17 +456,40 @@ if ( ); if (options.autoUninstall) { - const args = [ - 'rm', - '-D', - ...traverseResults.map((result) => result.match.moduleName) - ]; - - await x('npm', args); + for (const result of traverseResults) { + devDependenciesToRemove.push(result.match.moduleName); + } } + + packageJsonFailed = true; } -packageScanSpinner.stop('`package.json` dependencies scanned successfully.'); +if (packageJsonFailed) { + packageScanSpinner.stop('`package.json` dependencies scanned successfully.'); +} else { + packageScanSpinner.stop( + '`package.json` contained replaceable dependencies.', + 2 + ); +} + +if ( + options.autoUninstall && + (dependenciesToRemove.length > 0 || devDependenciesToRemove.length > 0) +) { + const npmSpinner = cl.spinner(); + + npmSpinner.start('Removing npm dependencies'); + + if (dependenciesToRemove.length > 0) { + await x('npm', ['rm', '-S', ...dependenciesToRemove]); + } + if (devDependenciesToRemove.length > 0) { + await x('npm', ['rm', '-D', ...devDependenciesToRemove]); + } + + npmSpinner.stop('npm dependencies removed'); +} const fileScanSpinner = cl.spinner(); @@ -478,7 +503,11 @@ const scanFilesResult = await scanFiles( fileScanSpinner ); -fileScanSpinner.stop('Scanned files successfully.'); +if (scanFilesResult.length > 0) { + fileScanSpinner.stop('Detected files with replaceable modules!', 2); +} else { + fileScanSpinner.stop('All files scanned'); +} if (options.fix) { await fixFiles(scanFilesResult);