diff --git a/package.json b/package.json index 36eff04fb..883bffa0c 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "copy:icons": "cp -r ./node_modules/@fuel-ui/icons/dist/icons/sprite.svg ./public/icons", "deps:update": "updates -gu", "dev": "./scripts/dev.sh", - "docs:clean": "sh ./scripts/clean-build-files.sh", + "docs:clean": "node ./scripts/clean-build-files.mjs", "docs:sync": "sh ./scripts/sync-repos.sh", "docs:update": "node scripts/update-nightly/index.mjs", "docs:update:nightly": "node scripts/update-nightly/index.mjs --nightly", diff --git a/scripts/clean-build-files.mjs b/scripts/clean-build-files.mjs new file mode 100644 index 000000000..1879c09ba --- /dev/null +++ b/scripts/clean-build-files.mjs @@ -0,0 +1,113 @@ +import fs from 'fs'; +import path from 'path'; + +// List of directories to delete unused files from +const targetDirs = [ + './docs/sway', + './docs/nightly/sway', + './docs/builds/sway', + './docs/nightly/builds/sway', + './docs/fuels-rs', + './docs/nightly/fuels-rs', + './docs/fuels-ts', + './docs/nightly/fuels-ts', + './docs/fuels-wallet', + './docs/nightly/fuels-wallet', + './docs/fuel-graphql-docs', + './docs/nightly/fuel-graphql-docs', + // './docs/fuel-core', +]; + +// Exclusions for each type of directory +const exclusions = { + sway: [ + 'sway/Cargo.toml', + 'sway/forc-pkg', + 'sway/sway-lib-std', + 'sway/docs/book/src', + 'sway/examples', + 'sway/master/book', + 'sway/test/src/sdk-harness/test_projects/run_external_proxy', + 'sway/test/src/sdk-harness/test_projects/run_external_target', + ], + fuels_rs: [ + 'fuels-rs/Cargo.toml', + 'fuels-rs/docs', + 'fuels-rs/examples', + 'fuels-rs/packages', + ], + fuels_ts: [ + 'fuels-ts/apps', + 'fuels-ts/packages', + 'fuels-ts/package.json', + 'fuels-ts/demo-wallet-sdk-react', + ], + fuels_wallet: ['fuels-wallet/package.json', 'fuels-wallet/packages'], + // fuel_core: ['fuel-core/deployment/scripts/chainspec', 'fuel-core/Cargo.toml'], + fuel_graphql_docs: [ + 'fuel-graphql-docs/docs', + 'fuel-graphql-docs/examples', + 'fuel-graphql-docs/src', + ], +}; + +function main() { + for (const targetDir of targetDirs) { + if (!fs.existsSync(targetDir)) { + // console.log(`Directory ${targetDir} does not exist!`); + const basePath = process.cwd(); + // console.log(`Current directory: ${basePath}`); + return; + } + // Change to the target directory + process.chdir(targetDir); + const dirBasename = path.basename(targetDir).replace(/-/g, '_'); + const currentExclusions = exclusions[dirBasename]; + cleanupFiles(currentExclusions, '.'); + // console.log(`Cleanup done for ${targetDir}!`); + // Return to the original directory + let x = '../..'; + if (process.cwd().includes('nightly')) { + x = `${x}/..`; + } + if (process.cwd().includes('builds')) { + x = `${x}/..`; + } + process.chdir(path.resolve(process.cwd(), x)); + } +} + +function cleanupFiles(currentExclusions, dirPath) { + // Read all items in directory + fs.readdirSync(dirPath).forEach((item) => { + let shouldDelete = true; + const basePath = process.cwd().split('/docs/')[1]; + const thisFilePath = path + .join(basePath, dirPath, item) + .replace('builds/', '') + .replace('nightly/', ''); + const subFilePath = `./${thisFilePath.split('/').slice(1).join('/')}`; + + if (currentExclusions?.includes(thisFilePath)) { + shouldDelete = false; + // console.log('Excluding: ', thisFilePath); + } else if ( + fs.existsSync(subFilePath) && + fs.lstatSync(subFilePath).isDirectory() + ) { + shouldDelete = false; + cleanupFiles(currentExclusions, subFilePath); + } + + deleteFolder(shouldDelete, subFilePath); + }); +} + +function deleteFolder(shouldDelete, subFilePath) { + if (shouldDelete) { + // console.log('DELETING: ', subFilePath); + fs.rmSync(subFilePath, { recursive: true, force: true }); + } +} + +main(); diff --git a/scripts/clean-build-files.sh b/scripts/clean-build-files.sh deleted file mode 100644 index 8f5e80db5..000000000 --- a/scripts/clean-build-files.sh +++ /dev/null @@ -1,57 +0,0 @@ -#!/bin/bash - -# List of directories to delete unused files from -TARGET_DIRS=( -"./docs/sway" -"./docs/nightly/sway" -"./docs/builds/sway" -"./docs/nightly/builds/sway" -"./docs/fuelup" -"./docs/fuels-rs" -"./docs/nightly/fuels-rs" -"./docs/fuels-ts" -"./docs/nightly/fuels-ts" -"./docs/fuels-wallet" -"./docs/nightly/fuels-wallet" -) - -# File/folder names to exclude from deletion in each book -EXCLUSIONS_sway=("Cargo.toml" "forc-pkg" "sway-lib-std" "docs" "examples" "master" "test") -EXCLUSIONS_fuels_rs=("Cargo.toml" "docs" "examples" "packages") -EXCLUSIONS_fuels_ts=("apps" "packages" "package.json", "demo-wallet-sdk-react") -EXCLUSIONS_fuels_wallet=("package.json" "packages") - -for TARGET_DIR in "${TARGET_DIRS[@]}"; do - # Check if directory exists - if [[ ! -d "$TARGET_DIR" ]]; then - echo "Warning: Target directory $TARGET_DIR does not exist. Skipping..." - continue - fi - - # Change to the target directory, pushing to directory stack - pushd "$TARGET_DIR" > /dev/null - - for item in *; do - should_delete=true - - dir_basename=$(basename "$TARGET_DIR" | tr '-' '_') - current_exclusions="EXCLUSIONS_$dir_basename[@]" - - for exclusion in "${!current_exclusions}"; do - if [[ "$item" == "$exclusion" ]]; then - should_delete=false - break - fi - done - - if $should_delete; then - rm -rf "$item" - # echo "DELETED: $item" - fi - done - - echo "Cleanup done for $TARGET_DIR!" - - # Return to the original directory, popping from directory stack - popd > /dev/null -done