|
1 | | -import { pathExists, stat, remove, readFile } from "fs-extra"; |
| 1 | +import { pathExists, remove, readFile } from "fs-extra"; |
2 | 2 | import { EOL } from "os"; |
3 | 3 | import { join } from "path"; |
4 | 4 | import { Disposable, ExtensionContext } from "vscode"; |
@@ -132,46 +132,53 @@ async function scrubDirectory( |
132 | 132 | errorMsg?: string; |
133 | 133 | deleted: boolean; |
134 | 134 | }> { |
135 | | - const timestampFile = join(dir, "timestamp"); |
136 | 135 | try { |
137 | | - let deleted = true; |
138 | | - if (!(await stat(dir)).isDirectory()) { |
139 | | - void extLogger.log(` ${dir} is not a directory. Deleting.`); |
140 | | - await remove(dir); |
141 | | - } else if (!(await pathExists(timestampFile))) { |
142 | | - void extLogger.log(` ${dir} has no timestamp file. Deleting.`); |
143 | | - await remove(dir); |
144 | | - } else if (!(await stat(timestampFile)).isFile()) { |
145 | | - void extLogger.log(` ${timestampFile} is not a file. Deleting.`); |
| 136 | + if (await shouldScrubDirectory(dir, now, maxQueryTime)) { |
146 | 137 | await remove(dir); |
| 138 | + return { deleted: true }; |
147 | 139 | } else { |
148 | | - const timestampText = await readFile(timestampFile, "utf8"); |
149 | | - const timestamp = parseInt(timestampText, 10); |
150 | | - |
151 | | - if (Number.isNaN(timestamp)) { |
152 | | - void extLogger.log( |
153 | | - ` ${dir} has invalid timestamp '${timestampText}'. Deleting.`, |
154 | | - ); |
155 | | - await remove(dir); |
156 | | - } else if (now - timestamp > maxQueryTime) { |
157 | | - void extLogger.log( |
158 | | - ` ${dir} is older than ${maxQueryTime / 1000} seconds. Deleting.`, |
159 | | - ); |
160 | | - await remove(dir); |
161 | | - } else { |
162 | | - void extLogger.log( |
163 | | - ` ${dir} is not older than ${maxQueryTime / 1000} seconds. Keeping.`, |
164 | | - ); |
165 | | - deleted = false; |
166 | | - } |
| 140 | + return { deleted: false }; |
167 | 141 | } |
168 | | - return { |
169 | | - deleted, |
170 | | - }; |
171 | 142 | } catch (err) { |
172 | 143 | return { |
173 | 144 | errorMsg: ` Could not delete '${dir}': ${err}`, |
174 | 145 | deleted: false, |
175 | 146 | }; |
176 | 147 | } |
177 | 148 | } |
| 149 | + |
| 150 | +async function shouldScrubDirectory( |
| 151 | + dir: string, |
| 152 | + now: number, |
| 153 | + maxQueryTime: number, |
| 154 | +): Promise<boolean> { |
| 155 | + const timestamp = await getTimestamp(join(dir, "timestamp")); |
| 156 | + if (timestamp === undefined || Number.isNaN(timestamp)) { |
| 157 | + void extLogger.log(` ${dir} timestamp is missing or invalid. Deleting.`); |
| 158 | + return true; |
| 159 | + } else if (now - timestamp > maxQueryTime) { |
| 160 | + void extLogger.log( |
| 161 | + ` ${dir} is older than ${maxQueryTime / 1000} seconds. Deleting.`, |
| 162 | + ); |
| 163 | + return true; |
| 164 | + } else { |
| 165 | + void extLogger.log( |
| 166 | + ` ${dir} is not older than ${maxQueryTime / 1000} seconds. Keeping.`, |
| 167 | + ); |
| 168 | + return false; |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +async function getTimestamp( |
| 173 | + timestampFile: string, |
| 174 | +): Promise<number | undefined> { |
| 175 | + try { |
| 176 | + const timestampText = await readFile(timestampFile, "utf8"); |
| 177 | + return parseInt(timestampText, 10); |
| 178 | + } catch (err) { |
| 179 | + void extLogger.log( |
| 180 | + ` Could not read timestamp file '${timestampFile}': ${err}`, |
| 181 | + ); |
| 182 | + return undefined; |
| 183 | + } |
| 184 | +} |
0 commit comments