pi-image-tools 1.3.1 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -37,6 +37,17 @@ interface RecentImageSource {
37
37
  filterScreenshotNames: boolean;
38
38
  }
39
39
 
40
+ function source(
41
+ homeDirectory: string,
42
+ segments: readonly string[],
43
+ filterScreenshotNames: boolean,
44
+ ): RecentImageSource {
45
+ return {
46
+ path: join(homeDirectory, ...segments),
47
+ filterScreenshotNames,
48
+ };
49
+ }
50
+
40
51
  export interface RecentImageCandidate {
41
52
  path: string;
42
53
  name: string;
@@ -106,64 +117,27 @@ function parseConfiguredSources(environment: NodeJS.ProcessEnv, homeDirectory: s
106
117
  .filter((value) => value.length > 0);
107
118
  }
108
119
 
109
- function getDefaultWindowsSources(homeDirectory: string): RecentImageSource[] {
110
- return [
111
- {
112
- path: join(homeDirectory, "Pictures", "Screenshots"),
113
- filterScreenshotNames: false,
114
- },
115
- {
116
- path: join(homeDirectory, "OneDrive", "Pictures", "Screenshots"),
117
- filterScreenshotNames: false,
118
- },
119
- {
120
- path: join(homeDirectory, "Desktop"),
121
- filterScreenshotNames: true,
122
- },
123
- ];
124
- }
125
-
126
- function getDefaultLinuxSources(homeDirectory: string): RecentImageSource[] {
127
- return [
128
- {
129
- path: join(homeDirectory, "Pictures", "Screenshots"),
130
- filterScreenshotNames: false,
131
- },
132
- {
133
- path: join(homeDirectory, "Pictures"),
134
- filterScreenshotNames: true,
135
- },
136
- {
137
- path: join(homeDirectory, "Downloads"),
138
- filterScreenshotNames: true,
139
- },
140
- {
141
- path: join(homeDirectory, "Desktop"),
142
- filterScreenshotNames: true,
143
- },
144
- ];
145
- }
146
-
147
- function getDefaultMacSources(homeDirectory: string): RecentImageSource[] {
148
- return [
149
- {
150
- path: join(homeDirectory, "Desktop"),
151
- filterScreenshotNames: true,
152
- },
153
- {
154
- path: join(homeDirectory, "Downloads"),
155
- filterScreenshotNames: true,
156
- },
157
- {
158
- path: join(homeDirectory, "Pictures", "Screenshots"),
159
- filterScreenshotNames: false,
160
- },
161
- {
162
- path: join(homeDirectory, "Pictures"),
163
- filterScreenshotNames: true,
164
- },
165
- ];
166
- }
120
+ type DefaultSourceSpec = readonly [segments: readonly string[], filterScreenshotNames: boolean];
121
+
122
+ const DEFAULT_PLATFORM_SOURCES: Readonly<Partial<Record<NodeJS.Platform, readonly DefaultSourceSpec[]>>> = {
123
+ win32: [
124
+ [["Pictures", "Screenshots"], false],
125
+ [["OneDrive", "Pictures", "Screenshots"], false],
126
+ [["Desktop"], true],
127
+ ],
128
+ linux: [
129
+ [["Pictures", "Screenshots"], false],
130
+ [["Pictures"], true],
131
+ [["Downloads"], true],
132
+ [["Desktop"], true],
133
+ ],
134
+ darwin: [
135
+ [["Desktop"], true],
136
+ [["Downloads"], true],
137
+ [["Pictures", "Screenshots"], false],
138
+ [["Pictures"], true],
139
+ ],
140
+ };
167
141
 
168
142
  function dedupeSources(
169
143
  sources: readonly RecentImageSource[],
@@ -219,24 +193,22 @@ function listRecentImagesFromSource(source: RecentImageSource): RecentImageCandi
219
193
 
220
194
  const fullPath = join(source.path, name);
221
195
 
222
- let stat;
223
196
  try {
224
- stat = statSync(fullPath);
225
- } catch {
226
- continue;
227
- }
197
+ const stat = statSync(fullPath);
198
+ if (!stat.isFile()) {
199
+ continue;
200
+ }
228
201
 
229
- if (!stat.isFile()) {
202
+ candidates.push({
203
+ path: fullPath,
204
+ name,
205
+ mimeType,
206
+ modifiedAtMs: stat.mtimeMs,
207
+ sizeBytes: stat.size,
208
+ });
209
+ } catch {
230
210
  continue;
231
211
  }
232
-
233
- candidates.push({
234
- path: fullPath,
235
- name,
236
- mimeType,
237
- modifiedAtMs: stat.mtimeMs,
238
- sizeBytes: stat.size,
239
- });
240
212
  }
241
213
 
242
214
  return candidates;
@@ -246,16 +218,14 @@ function getPlatformDefaultSources(
246
218
  platform: NodeJS.Platform,
247
219
  homeDirectory: string,
248
220
  ): RecentImageSource[] {
249
- switch (platform) {
250
- case "win32":
251
- return getDefaultWindowsSources(homeDirectory);
252
- case "linux":
253
- return getDefaultLinuxSources(homeDirectory);
254
- case "darwin":
255
- return getDefaultMacSources(homeDirectory);
256
- default:
257
- return [];
221
+ const specs = DEFAULT_PLATFORM_SOURCES[platform];
222
+ if (!specs) {
223
+ return [];
258
224
  }
225
+
226
+ return specs.map(([segments, filterScreenshotNames]) =>
227
+ source(homeDirectory, segments, filterScreenshotNames),
228
+ );
259
229
  }
260
230
 
261
231
  function buildSources(options: DiscoverRecentImagesOptions): RecentImageSource[] {
@@ -368,8 +338,9 @@ function pruneCacheDirectory(cacheDirectory: string, maxCacheFiles: number): voi
368
338
  for (const staleFile of imageFiles.slice(maxCacheFiles)) {
369
339
  try {
370
340
  unlinkSync(staleFile.fullPath);
371
- } catch {
372
- // Intentionally ignore cache cleanup failures.
341
+ } catch (error) {
342
+ // Cache cleanup is best-effort; a stale file is retried on the next prune.
343
+ void error;
373
344
  }
374
345
  }
375
346
  }