Skip to content

Commit b813d8a

Browse files
authored
Cache: Clear cache option (#1219)
1 parent adb2a7a commit b813d8a

File tree

6 files changed

+59
-5
lines changed

6 files changed

+59
-5
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ ncu "/^(?!react-).*$/" # windows
131131
## Options
132132

133133
```text
134-
--cache Cache versions to the cache file (default: false)
134+
--cache Cache versions to the cache file
135+
--cacheClear Clear the default cache, or the cache file
136+
specified by --cacheFile
135137
--cacheExpiration <min> Cache expiration in minutes (default: 10)
136138
--cacheFile <path> Filepath for the cache file (default:
137139
"~/.ncu-cache.json")

src/cli-options.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,11 @@ const cliOptions: CLIOption[] = [
277277
{
278278
long: 'cache',
279279
description: 'Cache versions to the cache file',
280-
default: false,
280+
type: 'boolean',
281+
},
282+
{
283+
long: 'cacheClear',
284+
description: 'Clear the default cache, or the cache file specified by --cacheFile',
281285
type: 'boolean',
282286
},
283287
{

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import prompts from 'prompts-ncu'
66
import spawn from 'spawn-please'
77
import untildify from 'untildify'
88
import { cliOptionsMap } from './cli-options'
9+
import { cacheClear } from './lib/cache'
910
import chalk, { chalkInit } from './lib/chalk'
1011
import doctor from './lib/doctor'
1112
import exists from './lib/exists'
@@ -176,6 +177,10 @@ export async function run(
176177

177178
print(options, 'Initializing', 'verbose')
178179

180+
if (options.cacheClear) {
181+
await cacheClear(options)
182+
}
183+
179184
if (options.packageManager === 'npm' && !options.prefix) {
180185
options.prefix = await packageManagers.npm.defaultPrefix!(options)
181186
}

src/lib/cache.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,22 @@ export const defaultCacheFilename = '.ncu-cache.json'
2828
export const defaultCacheFile = `~/${defaultCacheFilename}`
2929
export const resolvedDefaultCacheFile = path.join(os.homedir(), defaultCacheFilename)
3030

31+
/** Resolve the cache file path based on os/homedir. */
32+
export function resolveCacheFile(optionsCacheFile: string) {
33+
return optionsCacheFile === defaultCacheFile ? resolvedDefaultCacheFile : optionsCacheFile
34+
}
35+
36+
/** Clear the default cache, or the cache file specified by --cacheFile. */
37+
export async function cacheClear(options: Options) {
38+
if (!options.cacheFile) {
39+
return
40+
}
41+
42+
await fs.promises.rm(resolveCacheFile(options.cacheFile), { force: true })
43+
}
44+
3145
/**
32-
* The cacher stores key (name + version) - value (new version) pairs
46+
* The cacher stores key (name + target) - value (new version) pairs
3347
* for quick updates across `ncu` calls.
3448
*
3549
* @returns
@@ -39,7 +53,7 @@ export default async function cacher(options: Omit<Options, 'cacher'>): Promise<
3953
return
4054
}
4155

42-
const cacheFile = options.cacheFile === defaultCacheFile ? resolvedDefaultCacheFile : options.cacheFile
56+
const cacheFile = resolveCacheFile(options.cacheFile)
4357
let cacheData: CacheData = {}
4458
const cacheUpdates: Record<string, string> = {}
4559

@@ -49,7 +63,7 @@ export default async function cacher(options: Omit<Options, 'cacher'>): Promise<
4963
const expired = checkCacheExpiration(cacheData, options.cacheExpiration)
5064
if (expired) {
5165
// reset cache
52-
fs.promises.rm(cacheFile)
66+
fs.promises.rm(cacheFile, { force: true })
5367
cacheData = {}
5468
}
5569
} catch (error) {

src/types/RunOptions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ export interface RunOptions {
99
/** Cache versions to the cache file */
1010
cache?: boolean
1111

12+
/** Clear the default cache, or the cache file specified by --cacheFile */
13+
cacheClear?: boolean
14+
1215
/** Cache expiration in minutes (default: 10) */
1316
cacheExpiration?: number
1417

test/cache.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,30 @@ describe('cache', () => {
8989
rimraf.sync(resolvedDefaultCacheFile)
9090
}
9191
})
92+
93+
it('clears the cache file', async () => {
94+
const packageData = {
95+
dependencies: {
96+
// major version upgrade → 2.0.0
97+
'ncu-test-v2': '^1.0.0',
98+
// latest: minor version upgrade → 1.1.0
99+
// greatest: prerelease → 1.2.0-dev.0
100+
'ncu-test-tag': '1.0.0',
101+
// latest: no upgrade
102+
// greatest: prerelease → 2.0.0-alpha.2
103+
'ncu-test-alpha': '1.0.0',
104+
},
105+
}
106+
107+
await ncu.run({ packageData, cache: true })
108+
109+
await ncu.run({ packageData, cacheClear: true })
110+
let noCacheFile = false
111+
try {
112+
await fs.stat(resolvedDefaultCacheFile)
113+
} catch (error) {
114+
noCacheFile = true
115+
}
116+
expect(noCacheFile).eq(true)
117+
})
92118
})

0 commit comments

Comments
 (0)