1+ import { rm } from 'node:fs/promises' ;
12import path from 'node:path' ;
23import { performance } from 'node:perf_hooks' ;
34import { color , logger } from 'rslog' ;
45import { parseArgs } from '../cli/args.ts' ;
56import { printCommandHelp } from '../cli/help.ts' ;
67import type { LoadedRstackConfig } from '../config.ts' ;
7- import { loadCliRstackConfig } from '../cli/config.ts' ;
8- import { ensureProjectCacheDir } from '../projectCache.ts' ;
8+ import { getConfigState , loadCliRstackConfig } from '../cli/config.ts' ;
9+ import { ensureProjectCacheDir , getProjectCacheDir } from '../projectCache.ts' ;
910import { fmtCacheFileName } from './cacheStore.ts' ;
1011import { resolveFmtConfig } from './config.ts' ;
1112import { discoverFmtFiles } from './discovery.ts' ;
@@ -17,6 +18,7 @@ import type { FmtMode, FmtRunResult, ResolvedFmtConfig } from './types.ts';
1718interface ParsedFmtCLIArgs {
1819 cache : boolean ;
1920 cacheLocation ?: string ;
21+ clearCache : boolean ;
2022 mode : FmtMode ;
2123 patterns : string [ ] ;
2224 ignorePaths : string [ ] ;
@@ -57,7 +59,7 @@ const parseMaxWorkers = (value: string | undefined): number | undefined => {
5759 return maxWorkers ;
5860} ;
5961
60- /** Rejects the mode flags and file arguments that a server-like option replaces . */
62+ /** Rejects formatting modes and file arguments for standalone operations . */
6163const assertExclusiveMode = (
6264 option : string ,
6365 hasMode : boolean ,
@@ -85,6 +87,7 @@ const parseFmtArgs = (args: string[]): ParsedFmtCLIArgs => {
8587 'ignore-unknown' : { type : 'boolean' , short : 'u' } ,
8688 'no-cache' : { type : 'boolean' } ,
8789 'cache-location' : { type : 'string' } ,
90+ 'clear-cache' : { type : 'boolean' } ,
8891 'no-error-on-unmatched-pattern' : { type : 'boolean' } ,
8992 'with-node-modules' : { type : 'boolean' } ,
9093 'parallel-workers' : { type : 'string' } ,
@@ -122,6 +125,16 @@ const parseFmtArgs = (args: string[]): ParsedFmtCLIArgs => {
122125 const help = values . help ?? false ;
123126 const stdinFilepath = values . stdinFilepath ;
124127 const lsp = values . lsp ?? false ;
128+ const clearCache = values . clearCache ?? false ;
129+
130+ if ( clearCache ) {
131+ assertExclusiveMode ( '--clear-cache' , modes . length > 0 , positionals ) ;
132+ if ( lsp || stdinFilepath !== undefined || ! cache ) {
133+ throw new Error (
134+ 'The --clear-cache option cannot be used with --lsp, --stdin-filepath, or --no-cache.' ,
135+ ) ;
136+ }
137+ }
125138
126139 if ( lsp ) {
127140 assertExclusiveMode ( '--lsp' , modes . length > 0 , positionals ) ;
@@ -138,6 +151,7 @@ const parseFmtArgs = (args: string[]): ParsedFmtCLIArgs => {
138151 return {
139152 cache,
140153 cacheLocation,
154+ clearCache,
141155 mode,
142156 patterns : positionals ,
143157 ignorePaths,
@@ -151,6 +165,12 @@ const parseFmtArgs = (args: string[]): ParsedFmtCLIArgs => {
151165 } ;
152166} ;
153167
168+ const getFmtCacheFilePath = ( rootPath : string , cacheDirPath ?: string ) : string =>
169+ path . join (
170+ cacheDirPath ?? path . join ( getProjectCacheDir ( rootPath ) , 'fmt' ) ,
171+ fmtCacheFileName ,
172+ ) ;
173+
154174const createDisplayPathResolver = (
155175 cwd : string ,
156176) : ( ( filePath : string ) => string ) => {
@@ -273,6 +293,7 @@ const runFmtCLI = async (
273293 const {
274294 cache,
275295 cacheLocation,
296+ clearCache,
276297 help,
277298 ignorePaths,
278299 ignoreUnknown,
@@ -335,6 +356,18 @@ const runFmtCLI = async (
335356 }
336357 }
337358
359+ if ( clearCache ) {
360+ // Config discovery stays in cwd unless --config points elsewhere.
361+ // Clearing the cache only needs that location, not the config contents.
362+ const configPath = loadedConfig ?. filePath ?? getConfigState ( ) . configPath ;
363+ const rootPath = configPath ? path . dirname ( configPath ) : cwd ;
364+ const cacheFilePath = getFmtCacheFilePath ( rootPath , cacheDirPath ) ;
365+ // Custom cache directories may contain files that do not belong to us.
366+ await rm ( cacheFilePath , { force : true } ) ;
367+ logger . success ( `Formatting cache cleared: ${ color . dim ( cacheFilePath ) } ` ) ;
368+ return ;
369+ }
370+
338371 const config = await loadFmtConfig ( cwd , loadedConfig ) ;
339372 const files = await discoverFmtFiles ( {
340373 cwd,
@@ -359,14 +392,14 @@ const runFmtCLI = async (
359392 let cacheContext ;
360393 if ( cacheDirPath ) {
361394 cacheContext = {
362- filePath : path . join ( cacheDirPath , fmtCacheFileName ) ,
395+ filePath : getFmtCacheFilePath ( config . rootPath , cacheDirPath ) ,
363396 rootPath : config . rootPath ,
364397 } ;
365398 } else if ( cache ) {
366399 const cacheDir = await ensureProjectCacheDir ( config . rootPath ) ;
367400 if ( cacheDir . status === 'available' ) {
368401 cacheContext = {
369- filePath : path . join ( cacheDir . path , 'fmt' , fmtCacheFileName ) ,
402+ filePath : getFmtCacheFilePath ( config . rootPath ) ,
370403 rootPath : config . rootPath ,
371404 } ;
372405 }
0 commit comments