|
1 | | -import { window } from "vscode"; |
| 1 | +import { Uri, window, workspace } from "vscode"; |
2 | 2 | import { downloadPglt, getDownloadedBinary } from "./downloader"; |
3 | 3 | import { restart, start, stop } from "./lifecycle"; |
4 | 4 | import { logger } from "./logger"; |
5 | 5 | import { state } from "./state"; |
6 | 6 | import { |
7 | 7 | clearGlobalBinaries, |
8 | 8 | clearTemporaryBinaries, |
| 9 | + dirExists, |
9 | 10 | getVersion, |
10 | 11 | } from "./utils"; |
11 | 12 | import { Releases } from "./releases"; |
| 13 | +import { join } from "path"; |
| 14 | +import os from "node:os"; |
12 | 15 |
|
13 | 16 | /** |
14 | 17 | * These commands are exposed to the user via the Command Palette. |
@@ -75,4 +78,131 @@ export class UserFacingCommands { |
75 | 78 | ); |
76 | 79 | } |
77 | 80 | } |
| 81 | + |
| 82 | + static async copyLatestLogfile() { |
| 83 | + logger.info("Starting to copy latest log file…"); |
| 84 | + |
| 85 | + let logdir; |
| 86 | + |
| 87 | + if (process.env.PGT_LOG_PATH) { |
| 88 | + logdir = Uri.file(process.env.PGT_LOG_PATH); |
| 89 | + } else { |
| 90 | + /* |
| 91 | + * Looks are placed at different locations based on the platform. |
| 92 | + * Linux: /home/alice/.cache/pgt |
| 93 | + * Win: C:\Users\Alice\AppData\Local\supabase-community\pgt\cache |
| 94 | + * Mac: /Users/Alice/Library/Caches/dev.supabase-community.pgt |
| 95 | + */ |
| 96 | + switch (process.platform) { |
| 97 | + case "darwin": { |
| 98 | + logdir = Uri.file( |
| 99 | + join( |
| 100 | + os.homedir(), |
| 101 | + "Library", |
| 102 | + "Caches", |
| 103 | + "dev.supabase-community.pgt" |
| 104 | + ) |
| 105 | + ); |
| 106 | + break; |
| 107 | + } |
| 108 | + case "linux": { |
| 109 | + logdir = Uri.file(join(os.homedir(), ".cache", "pgt")); |
| 110 | + break; |
| 111 | + } |
| 112 | + case "win32": { |
| 113 | + logdir = Uri.file( |
| 114 | + join( |
| 115 | + os.homedir(), |
| 116 | + "AppData", |
| 117 | + "Local", |
| 118 | + "supabase-community", |
| 119 | + "pgt", |
| 120 | + "cache" |
| 121 | + ) |
| 122 | + ); |
| 123 | + break; |
| 124 | + } |
| 125 | + default: { |
| 126 | + window.showErrorMessage( |
| 127 | + `Unsupported Platform: ${process.platform}. PostgresTools only runs on linux, darwin and windows.` |
| 128 | + ); |
| 129 | + return; |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + logger.info("Determined log directory location", { |
| 135 | + logdir: logdir.fsPath, |
| 136 | + }); |
| 137 | + |
| 138 | + if (!(await dirExists(logdir))) { |
| 139 | + window.showErrorMessage(`Did not find expected log directory.`); |
| 140 | + return; |
| 141 | + } |
| 142 | + |
| 143 | + const logFiles = await workspace.fs |
| 144 | + .readDirectory(logdir) |
| 145 | + .then((files) => files.filter(([name]) => name.startsWith("server.log"))); |
| 146 | + |
| 147 | + if (logFiles.length === 0) { |
| 148 | + window.showErrorMessage(`No log files found in log directory.`); |
| 149 | + return; |
| 150 | + } |
| 151 | + |
| 152 | + logger.info(`Found ${logFiles.length} log files.`); |
| 153 | + |
| 154 | + if (!state.activeProject) { |
| 155 | + window.showErrorMessage( |
| 156 | + `No active project, can't determine a location to copy the log file.` |
| 157 | + ); |
| 158 | + return; |
| 159 | + } |
| 160 | + |
| 161 | + logFiles.sort((a, b) => { |
| 162 | + const [dateA, idxA] = getDatePartsFromLogFile(a[0]); |
| 163 | + const [dateB, idxB] = getDatePartsFromLogFile(b[0]); |
| 164 | + |
| 165 | + if (dateA.getTime() === dateB.getTime()) { |
| 166 | + return idxB - idxA; |
| 167 | + } else { |
| 168 | + return dateB.getTime() - dateA.getTime(); |
| 169 | + } |
| 170 | + }); |
| 171 | + |
| 172 | + const [latestFilename] = logFiles[0]; |
| 173 | + |
| 174 | + logger.info(`Identified latest log file.`, { |
| 175 | + filename: latestFilename, |
| 176 | + }); |
| 177 | + |
| 178 | + try { |
| 179 | + const target = Uri.joinPath(state.activeProject.path, "server.log"); |
| 180 | + |
| 181 | + await workspace.fs.copy(Uri.joinPath(logdir, latestFilename), target); |
| 182 | + |
| 183 | + window.showInformationMessage(`Copied log file to ${target.fsPath}.`); |
| 184 | + } catch (err) { |
| 185 | + logger.error(`Error copying log file: ${err}`); |
| 186 | + window.showErrorMessage( |
| 187 | + `Error copying log file. View Output for more information.` |
| 188 | + ); |
| 189 | + } |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +/** Expected format is server.log.2025-01-01-17 */ |
| 194 | +function getDatePartsFromLogFile(filename: string): [Date, number] { |
| 195 | + try { |
| 196 | + const last = filename.split(".").pop()!; |
| 197 | + const [year, month, day, idx] = last.split("-"); |
| 198 | + |
| 199 | + if (!year || !month || !day || !idx || Number.isNaN(+idx)) { |
| 200 | + throw new Error(); |
| 201 | + } |
| 202 | + |
| 203 | + return [new Date(`${year}-${month}-${day}`), +idx]; |
| 204 | + } catch { |
| 205 | + logger.warn(`Unexpected log file name: ${filename}`); |
| 206 | + return [new Date(0), 999]; |
| 207 | + } |
78 | 208 | } |
0 commit comments