├── .gitignore ├── CHANGELOG.md ├── .npmignore ├── bin └── clear-npx-cache.js ├── .eslintrc ├── tsconfig.prod.json ├── tsconfig.dev.json ├── package.json ├── README.md └── src └── index.ts /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | package-lock.json 3 | lib 4 | types -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ### v1.0.1 2 | 3 | Added `cli` keyword. 4 | 5 | ### v1.0.0 6 | 7 | Initial release. -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | src 2 | package-lock.json 3 | .gitignore 4 | .npmignore 5 | node_modules 6 | tsconfig.*.json 7 | .eslintrc -------------------------------------------------------------------------------- /bin/clear-npx-cache.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | var clearCache = require('../lib/index.js').default; 3 | 4 | clearCache(); -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "parser": "@typescript-eslint/parser", 3 | "rules": { 4 | "semi": ["warn", "always"], 5 | "no-extra-semi": "warn", 6 | "quotes": ["warn", "single"] 7 | } 8 | } -------------------------------------------------------------------------------- /tsconfig.prod.json: -------------------------------------------------------------------------------- 1 | { 2 | "src": ["src/**/*.ts"], 3 | "compilerOptions": { 4 | "outDir": "lib", 5 | "target": "es5", 6 | "module": "commonjs", 7 | "moduleResolution": "node", 8 | "lib": ["es2020", "dom"], 9 | "strict": true, 10 | "esModuleInterop": true 11 | } 12 | } -------------------------------------------------------------------------------- /tsconfig.dev.json: -------------------------------------------------------------------------------- 1 | { 2 | "src": ["src/**/*.ts"], 3 | "compilerOptions": { 4 | "outDir": "lib", 5 | "target": "es5", 6 | "module": "commonjs", 7 | "moduleResolution": "node", 8 | "lib": ["es2020", "dom"], 9 | "declaration": true, 10 | "declarationDir": "types", 11 | "strict": true, 12 | "sourceMap": true, 13 | "esModuleInterop": true 14 | } 15 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "private": true, 3 | "name": "clear-npx-cache", 4 | "version": "1.1.2", 5 | "main": "lib/index.js", 6 | "types": "types/index.d.ts", 7 | "description": "Clears your NPX cache.", 8 | "keywords": [ 9 | "cli", 10 | "npm", 11 | "npx", 12 | "cache", 13 | "management", 14 | "utility" 15 | ], 16 | "author": "retueze", 17 | "license": "ISC", 18 | "repository": { 19 | "type": "git", 20 | "url": "https://github.com/return-0x0/node-clear-npx-cache" 21 | }, 22 | "bin": { 23 | "clear-npx-cache": "bin/clear-npx-cache.js" 24 | }, 25 | "scripts": { 26 | "lint": "eslint", 27 | "build:dev": "tsc -p tsconfig.dev.json", 28 | "clean": "rm -rf lib && rm -rf types", 29 | "build:prod": "npm run clean && tsc -p tsconfig.prod.json", 30 | "prepublish": " npm run lint && npm run build:prod" 31 | }, 32 | "devDependencies": { 33 | "@types/cli-progress": "^3.11.4", 34 | "@types/node": "^20.8.9", 35 | "@typescript-eslint/parser": "^4.22.0", 36 | "typescript": "^4.2.4" 37 | }, 38 | "dependencies": { 39 | "cli-progress": "^3.12.0" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Why you should clear your NPX cache? 2 | 3 | NPX has a bug. When you call, for example, one of this: 4 | 5 | ```sh 6 | npx create-react-app 7 | ``` 8 | ```sh 9 | npx cowsay "I'm a cow!" 10 | ``` 11 | 12 | …then you will do it successfully. But NPX will install it to your NPX cache, and NPX will *not* be watching lifetime of its cache. NPX package will be stored in the cache forever. When NPX-utility was updated, the cache will *not* be updated. I'll repeat, it's a bug. 13 | 14 | ### For users 15 | 16 | If you sometimes use a NPX-utility, you will store an old version of this package in your cache. And every package update will *not* be seen by NPX. Cache clearing will solve this trouble. 17 | 18 | ### For developers 19 | 20 | If you are developing a new NPX-package, you will test it. But you can publish a bugful package, and after a fix and re-publishing you will see same bug. I don't want you repeating my expirience in bug-finding and source-code-jumping. 21 | 22 | ## Usage 23 | 24 | ```sh 25 | npx clear-npx-cache 26 | ``` 27 | 28 | Expected behavior: 29 | 30 | - Take a cache directory from NPM configuration (`npm config get cache`) 31 | - If in this directory exists `_npx` 32 | - Then remove it recursely 33 | - Otherwise create a new `_npx` directory -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import { execSync } from 'child_process'; 2 | import { existsSync, mkdirSync, rmSync, readdirSync, readFileSync } from 'fs'; 3 | import path from 'path'; 4 | import cliProgress from 'cli-progress'; 5 | 6 | export default function clearCache(): void { 7 | const npmCacheDirectory = execSync('npm config get cache').toString().trimEnd(); 8 | const npxCacheDirectory = path.join(npmCacheDirectory, '_npx'); 9 | 10 | const bar1 = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic); 11 | 12 | console.log(' Starting cleaning ... \n'); 13 | 14 | if (existsSync(npxCacheDirectory)) { 15 | const subdirectoryNames = readdirSync(npxCacheDirectory); 16 | const subdirectories = subdirectoryNames.map((subdirectoryName) => 17 | path.join(npxCacheDirectory, subdirectoryName)); 18 | 19 | bar1.start(subdirectories.length, 0); 20 | 21 | for (let i = 0; i < subdirectories.length; i += 1) { 22 | const subdirectory = subdirectories[i]; 23 | const packagePath = path.join(subdirectory, 'package.json'); 24 | 25 | bar1.update(i + 1); 26 | 27 | if (existsSync(packagePath)) { 28 | const package_json = JSON.parse(readFileSync(packagePath).toString()); 29 | 30 | if (package_json.name === 'clear-npx-cache') continue; 31 | } 32 | 33 | rmSync(subdirectory, { recursive: true }); 34 | } 35 | 36 | bar1.stop(); 37 | 38 | console.log('\n Cache cleared successfully! Thanks for using our lib.'); 39 | } else { 40 | mkdirSync(npxCacheDirectory); 41 | console.log('\n Everything up to date, cache already clean!'); 42 | } 43 | } --------------------------------------------------------------------------------