|
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"; |
5 | 5 | import { extLogger } from "../common/logging/vscode"; |
6 | 6 | import { readDirFullPaths } from "../common/files"; |
7 | 7 | import { QueryHistoryDirs } from "./query-history-dirs"; |
8 | 8 | import { QueryHistoryManager } from "./query-history-manager"; |
| 9 | +import { getErrorMessage } from "../common/helpers-pure"; |
9 | 10 |
|
10 | 11 | const LAST_SCRUB_TIME_KEY = "lastScrubTime"; |
11 | 12 |
|
@@ -132,46 +133,55 @@ async function scrubDirectory( |
132 | 133 | errorMsg?: string; |
133 | 134 | deleted: boolean; |
134 | 135 | }> { |
135 | | - const timestampFile = join(dir, "timestamp"); |
136 | 136 | 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.`); |
| 137 | + if (await shouldScrubDirectory(dir, now, maxQueryTime)) { |
146 | 138 | await remove(dir); |
| 139 | + return { deleted: true }; |
147 | 140 | } 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 | | - } |
| 141 | + return { deleted: false }; |
167 | 142 | } |
168 | | - return { |
169 | | - deleted, |
170 | | - }; |
171 | 143 | } catch (err) { |
172 | 144 | return { |
173 | | - errorMsg: ` Could not delete '${dir}': ${err}`, |
| 145 | + errorMsg: ` Could not delete '${dir}': ${getErrorMessage(err)}`, |
174 | 146 | deleted: false, |
175 | 147 | }; |
176 | 148 | } |
177 | 149 | } |
| 150 | + |
| 151 | +async function shouldScrubDirectory( |
| 152 | + dir: string, |
| 153 | + now: number, |
| 154 | + maxQueryTime: number, |
| 155 | +): Promise<boolean> { |
| 156 | + const timestamp = await getTimestamp(join(dir, "timestamp")); |
| 157 | + if (timestamp === undefined || Number.isNaN(timestamp)) { |
| 158 | + void extLogger.log(` ${dir} timestamp is missing or invalid. Deleting.`); |
| 159 | + return true; |
| 160 | + } else if (now - timestamp > maxQueryTime) { |
| 161 | + void extLogger.log( |
| 162 | + ` ${dir} is older than ${maxQueryTime / 1000} seconds. Deleting.`, |
| 163 | + ); |
| 164 | + return true; |
| 165 | + } else { |
| 166 | + void extLogger.log( |
| 167 | + ` ${dir} is not older than ${maxQueryTime / 1000} seconds. Keeping.`, |
| 168 | + ); |
| 169 | + return false; |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +async function getTimestamp( |
| 174 | + timestampFile: string, |
| 175 | +): Promise<number | undefined> { |
| 176 | + try { |
| 177 | + const timestampText = await readFile(timestampFile, "utf8"); |
| 178 | + return parseInt(timestampText, 10); |
| 179 | + } catch (err) { |
| 180 | + void extLogger.log( |
| 181 | + ` Could not read timestamp file '${timestampFile}': ${getErrorMessage( |
| 182 | + err, |
| 183 | + )}`, |
| 184 | + ); |
| 185 | + return undefined; |
| 186 | + } |
| 187 | +} |
0 commit comments