From ec59aa39c5619cfc0c8a26e76b53e3a7056eba20 Mon Sep 17 00:00:00 2001 From: Chris Raible Date: Tue, 17 Dec 2024 21:00:38 -0500 Subject: [PATCH] Rewrote clean.sh script in javascript (#21909) ref https://linear.app/ghost/issue/ENG-1958/rewrite-cleansh-in-js - This script removes all node_modules, clears the yarn cache, nx cache, and deletes all build artifacts to provide a "fresh start". It's particularly useful for switching back and forth between local vs docker development environments because some of the node_modules (and therefore the caches) are built specifically for the architecture of whatever "host" they are built on. - The script works fine, but it's written in bash which isn't super easy to extend/modify and most of this repo is in JavaScript, so this commit just rewrites the bash script in JS for easier maintainability. --- .github/scripts/clean.js | 63 ++++++++++++++++++++++++++++++++++++++++ .github/scripts/clean.sh | 26 ----------------- package.json | 2 +- 3 files changed, 64 insertions(+), 27 deletions(-) create mode 100644 .github/scripts/clean.js delete mode 100755 .github/scripts/clean.sh diff --git a/.github/scripts/clean.js b/.github/scripts/clean.js new file mode 100644 index 0000000000..e6909bd9f8 --- /dev/null +++ b/.github/scripts/clean.js @@ -0,0 +1,63 @@ +// NOTE: this file can't use any NPM dependencies because it needs to run even if dependencies aren't installed yet or are corrupted +const {execSync} = require('child_process'); + +const isDevContainer = process.env.DEVCONTAINER === 'true'; + +cleanYarnCache(); +resetNxCache(); +deleteNodeModules(); +deleteBuildArtifacts(); +console.log('Cleanup complete!'); + +function deleteBuildArtifacts() { + console.log('Deleting all build artifacts...'); + try { + execSync('find ./ghost -type d -name "build" -exec rm -rf \'{}\' +', { + stdio: 'inherit' + }); + execSync('find ./ghost -type f -name "tsconfig.tsbuildinfo" -delete', { + stdio: 'inherit' + }); + } catch (error) { + console.error('Failed to delete build artifacts:', error); + process.exit(1); + } +} + +function deleteNodeModules() { + console.log('Deleting all node_modules directories...'); + try { + execSync('find . -name "node_modules" -type d -prune -exec rm -rf \'{}\' +', { + stdio: 'inherit' + }); + } catch (error) { + console.error('Failed to delete node_modules directories:', error); + process.exit(1); + } +} + +function resetNxCache() { + console.log('Resetting NX cache...'); + try { + execSync('rm -rf .nxcache .nx'); + } catch (error) { + console.error('Failed to reset NX cache:', error); + process.exit(1); + } +} + +function cleanYarnCache() { + console.log('Cleaning yarn cache...'); + try { + if (isDevContainer) { + // In devcontainer, these directories are mounted from the host so we can't delete them — `yarn cache clean` will fail + // so we delete the contents of the directories instead + execSync('rm -rf .yarncache/* .yarncachecopy/*'); + } else { + execSync('yarn cache clean'); + } + } catch (error) { + console.error('Failed to clean yarn cache:', error); + process.exit(1); + } +} diff --git a/.github/scripts/clean.sh b/.github/scripts/clean.sh deleted file mode 100755 index 87b17f7483..0000000000 --- a/.github/scripts/clean.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/bash - -set -e - -# Clean yarn cache -echo "Cleaning yarn cache..." -if [ "$DEVCONTAINER" = "true" ]; then - # In devcontainer, these directories are mounted from the host so we can't delete them — only their contents - rm -rf .yarncache/* .yarncachecopy/* -else - yarn cache clean -fi - -# Reset Nx -echo "Resetting NX cache..." -rm -rf .nxcache .nx - -# Recursively delete all node_modules directories -echo "Deleting all node_modules directories..." -find . -name "node_modules" -type d -prune -exec rm -rf '{}' + - -echo "Deleting all build artifacts..." -find ./ghost -type d -name "build" -exec rm -rf '{}' + -find ./ghost -type f -name "tsconfig.tsbuildinfo" -delete - -echo "Cleanup complete!" diff --git a/package.json b/package.json index 83461cef6b..7853226724 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "archive": "nx run ghost:archive", "build": "nx run-many -t build", "build:clean": "nx reset && rimraf -g 'ghost/*/build' && rimraf -g 'ghost/*/tsconfig.tsbuildinfo'", - "clean:hard": "./.github/scripts/clean.sh", + "clean:hard": "node ./.github/scripts/clean.js", "dev:debug": "DEBUG_COLORS=true DEBUG=@tryghost*,ghost:* yarn dev", "dev:admin": "node .github/scripts/dev.js --admin", "dev:ghost": "node .github/scripts/dev.js --ghost",