@@ -14,6 +14,7 @@ import { dartCompile } from "../dart-sass.ts";
1414import { TempContext } from "../temp.ts" ;
1515import { safeRemoveIfExists } from "../path.ts" ;
1616import * as log from "../../deno_ral/log.ts" ;
17+ import { onCleanup } from "../cleanup.ts" ;
1718
1819class SassCache {
1920 kv : Deno . Kv ;
@@ -155,6 +156,21 @@ class SassCache {
155156 compressed ,
156157 ) ;
157158 }
159+
160+ // add a cleanup method to register a cleanup handler
161+ cleanup ( temp : TempContext | undefined ) {
162+ const registerCleanup = temp ? temp . onCleanup : onCleanup ;
163+ registerCleanup ( ( ) => {
164+ try {
165+ this . kv . close ( ) ;
166+ if ( temp ) safeRemoveIfExists ( this . path ) ;
167+ } catch ( error ) {
168+ log . info (
169+ `Error occurred during sass cache cleanup for ${ this . path } : ${ error } ` ,
170+ ) ;
171+ }
172+ } ) ;
173+ }
158174}
159175
160176const currentSassCacheVersion = 1 ;
@@ -192,14 +208,20 @@ async function checkVersion(kv: Deno.Kv, path: string) {
192208}
193209
194210const _sassCache : Record < string , SassCache > = { } ;
195- export async function sassCache ( path : string ) : Promise < SassCache > {
211+
212+ export async function sassCache (
213+ path : string ,
214+ temp : TempContext | undefined ,
215+ ) : Promise < SassCache > {
196216 if ( ! _sassCache [ path ] ) {
197217 log . debug ( `Creating SassCache at ${ path } ` ) ;
198218 ensureDirSync ( path ) ;
199219 const kvFile = join ( path , "sass.kv" ) ;
200220 const kv = await Deno . openKv ( kvFile ) ;
201221 await checkVersion ( kv , kvFile ) ;
202222 _sassCache [ path ] = new SassCache ( kv , path ) ;
223+ // register cleanup for this cache
224+ _sassCache [ path ] . cleanup ( temp ) ;
203225 }
204226 log . debug ( `Returning SassCache at ${ path } ` ) ;
205227 const result = _sassCache [ path ] ;
0 commit comments