Skip to content

Commit

Permalink
Cache: Clear cache option (#1219)
Browse files Browse the repository at this point in the history
  • Loading branch information
ly3xqhl8g9 authored Nov 13, 2022
1 parent adb2a7a commit b813d8a
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 5 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ ncu "/^(?!react-).*$/" # windows
## Options

```text
--cache Cache versions to the cache file (default: false)
--cache Cache versions to the cache file
--cacheClear Clear the default cache, or the cache file
specified by --cacheFile
--cacheExpiration <min> Cache expiration in minutes (default: 10)
--cacheFile <path> Filepath for the cache file (default:
"~/.ncu-cache.json")
Expand Down
6 changes: 5 additions & 1 deletion src/cli-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,11 @@ const cliOptions: CLIOption[] = [
{
long: 'cache',
description: 'Cache versions to the cache file',
default: false,
type: 'boolean',
},
{
long: 'cacheClear',
description: 'Clear the default cache, or the cache file specified by --cacheFile',
type: 'boolean',
},
{
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import prompts from 'prompts-ncu'
import spawn from 'spawn-please'
import untildify from 'untildify'
import { cliOptionsMap } from './cli-options'
import { cacheClear } from './lib/cache'
import chalk, { chalkInit } from './lib/chalk'
import doctor from './lib/doctor'
import exists from './lib/exists'
Expand Down Expand Up @@ -176,6 +177,10 @@ export async function run(

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

if (options.cacheClear) {
await cacheClear(options)
}

if (options.packageManager === 'npm' && !options.prefix) {
options.prefix = await packageManagers.npm.defaultPrefix!(options)
}
Expand Down
20 changes: 17 additions & 3 deletions src/lib/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,22 @@ export const defaultCacheFilename = '.ncu-cache.json'
export const defaultCacheFile = `~/${defaultCacheFilename}`
export const resolvedDefaultCacheFile = path.join(os.homedir(), defaultCacheFilename)

/** Resolve the cache file path based on os/homedir. */
export function resolveCacheFile(optionsCacheFile: string) {
return optionsCacheFile === defaultCacheFile ? resolvedDefaultCacheFile : optionsCacheFile
}

/** Clear the default cache, or the cache file specified by --cacheFile. */
export async function cacheClear(options: Options) {
if (!options.cacheFile) {
return
}

await fs.promises.rm(resolveCacheFile(options.cacheFile), { force: true })
}

/**
* The cacher stores key (name + version) - value (new version) pairs
* The cacher stores key (name + target) - value (new version) pairs
* for quick updates across `ncu` calls.
*
* @returns
Expand All @@ -39,7 +53,7 @@ export default async function cacher(options: Omit<Options, 'cacher'>): Promise<
return
}

const cacheFile = options.cacheFile === defaultCacheFile ? resolvedDefaultCacheFile : options.cacheFile
const cacheFile = resolveCacheFile(options.cacheFile)
let cacheData: CacheData = {}
const cacheUpdates: Record<string, string> = {}

Expand All @@ -49,7 +63,7 @@ export default async function cacher(options: Omit<Options, 'cacher'>): Promise<
const expired = checkCacheExpiration(cacheData, options.cacheExpiration)
if (expired) {
// reset cache
fs.promises.rm(cacheFile)
fs.promises.rm(cacheFile, { force: true })
cacheData = {}
}
} catch (error) {
Expand Down
3 changes: 3 additions & 0 deletions src/types/RunOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export interface RunOptions {
/** Cache versions to the cache file */
cache?: boolean

/** Clear the default cache, or the cache file specified by --cacheFile */
cacheClear?: boolean

/** Cache expiration in minutes (default: 10) */
cacheExpiration?: number

Expand Down
26 changes: 26 additions & 0 deletions test/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,30 @@ describe('cache', () => {
rimraf.sync(resolvedDefaultCacheFile)
}
})

it('clears the cache file', async () => {
const packageData = {
dependencies: {
// major version upgrade → 2.0.0
'ncu-test-v2': '^1.0.0',
// latest: minor version upgrade → 1.1.0
// greatest: prerelease → 1.2.0-dev.0
'ncu-test-tag': '1.0.0',
// latest: no upgrade
// greatest: prerelease → 2.0.0-alpha.2
'ncu-test-alpha': '1.0.0',
},
}

await ncu.run({ packageData, cache: true })

await ncu.run({ packageData, cacheClear: true })
let noCacheFile = false
try {
await fs.stat(resolvedDefaultCacheFile)
} catch (error) {
noCacheFile = true
}
expect(noCacheFile).eq(true)
})
})

0 comments on commit b813d8a

Please sign in to comment.