@dickpy/dsh-imagegen 1.5.6 → 1.5.7

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.
@@ -1,275 +1,275 @@
1
- /**
2
- * Host-persisted generation history: images are stored as individual files
3
- * under ~/.dsh/dsh-imagegen/images/ and an index.json keeps the metadata +
4
- * file names. This makes the history survive across browsers/devices that
5
- * connect to the same DSH host, and keeps list responses small (the browser
6
- * loads image bytes lazily through the history image route).
7
- *
8
- * Framework-free (node:fs only) so the route layer can drive it directly.
9
- */
10
-
11
- import { promises as fs } from 'node:fs'
12
- import path from 'node:path'
13
- import { HISTORY_MAX, type GenerateMode, type HistoryEntry, type HistoryEntryInput } from './protocol.ts'
14
- import { notifyImageSaved } from './storage-sync.ts'
15
- import { imageDataRoot } from './image-storage-path.ts'
16
-
17
- function historyDir(): string { return imageDataRoot() }
18
- function indexPath(): string { return path.join(historyDir(), 'index.json') }
19
- function imagesDir(): string { return path.join(historyDir(), 'images') }
20
-
21
- // History mutations read and replace one shared index. Serialize them so
22
- // overlapping requests cannot each read an old index and lose the other's row.
23
- let pendingMutation: Promise<void> = Promise.resolve()
24
-
25
- function mutateHistory<T>(operation: () => Promise<T>): Promise<T> {
26
- const next = pendingMutation.then(operation, operation)
27
- pendingMutation = next.then(() => undefined, () => undefined)
28
- return next
29
- }
30
-
31
- /** One image's on-disk record (file name + mime, never base64). */
32
- interface StoredImage {
33
- file: string
34
- mime: string
35
- revisedPrompt?: string
36
- }
37
-
38
- /** One entry's on-disk record. */
39
- interface StoredEntry {
40
- id: string
41
- createdAt: number
42
- mode: GenerateMode
43
- model: string
44
- prompt: string
45
- size: string
46
- quality: string
47
- detail: string
48
- n: number
49
- images: StoredImage[]
50
- refName?: string
51
- channelId?: string
52
- channel?: string
53
- comparisonId?: string
54
- comparisonModels?: string[]
55
- workflow?: 'ecommerce'
56
- projectId?: string
57
- projectName?: string
58
- slotKey?: string
59
- slotLabel?: string
60
- canvas?: HistoryEntryInput['canvas']
61
- }
62
-
63
- /** The index.json shape. */
64
- interface IndexFile {
65
- entries: StoredEntry[]
66
- }
67
-
68
- /** File extension for a MIME type (image file names). */
69
- function extensionOf(mime: string): string {
70
- switch (mime.split(';')[0]!.trim()) {
71
- case 'image/jpeg': return 'jpg'
72
- case 'image/webp': return 'webp'
73
- case 'image/gif': return 'gif'
74
- default: return 'png'
75
- }
76
- }
77
-
78
- /** MIME type for a stored image file name (image route responses). */
79
- function mimeOfFile(file: string): string {
80
- const ext = path.extname(file).toLowerCase()
81
- switch (ext) {
82
- case '.jpg':
83
- case '.jpeg': return 'image/jpeg'
84
- case '.webp': return 'image/webp'
85
- case '.gif': return 'image/gif'
86
- default: return 'image/png'
87
- }
88
- }
89
-
90
- /** Sanitize an entry id for use as a file-name prefix. */
91
- function safeId(id: string): string {
92
- const cleaned = id.replace(/[^a-zA-Z0-9-]/g, '-')
93
- return cleaned === '' ? 'entry' : cleaned
94
- }
95
-
96
- /** Ensure the storage directories exist. */
97
- async function ensureDirs(): Promise<void> {
98
- await fs.mkdir(imagesDir(), { recursive: true })
99
- }
100
-
101
- /** Read the index, tolerating a missing/corrupt file. */
102
- async function readIndex(): Promise<StoredEntry[]> {
103
- try {
104
- const raw = await fs.readFile(indexPath(), 'utf8')
105
- const parsed: unknown = JSON.parse(raw)
106
- if (parsed === null || typeof parsed !== 'object') return []
107
- const entries = (parsed as { entries?: unknown }).entries
108
- if (!Array.isArray(entries)) return []
109
- return entries.filter(isStoredEntry)
110
- } catch {
111
- return []
112
- }
113
- }
114
-
115
- /** Persist the index. */
116
- async function writeIndex(entries: StoredEntry[]): Promise<void> {
117
- await ensureDirs()
118
- const payload: IndexFile = { entries }
119
- const tmp = `${indexPath()}.tmp-${process.pid}`
120
- await fs.writeFile(tmp, JSON.stringify(payload), 'utf8')
121
- await fs.rename(tmp, indexPath())
122
- }
123
-
124
- /** Structural guard for a stored entry. */
125
- function isStoredEntry(value: unknown): value is StoredEntry {
126
- if (value === null || typeof value !== 'object') return false
127
- const entry = value as Record<string, unknown>
128
- return typeof entry.id === 'string'
129
- && typeof entry.createdAt === 'number'
130
- && (entry.mode === 'text' || entry.mode === 'edit')
131
- && (entry.workflow === undefined || entry.workflow === 'ecommerce')
132
- && (entry.projectId === undefined || typeof entry.projectId === 'string')
133
- && (entry.projectName === undefined || typeof entry.projectName === 'string')
134
- && (entry.slotKey === undefined || typeof entry.slotKey === 'string')
135
- && (entry.slotLabel === undefined || typeof entry.slotLabel === 'string')
136
- && typeof entry.prompt === 'string'
137
- && Array.isArray(entry.images)
138
- && entry.images.every(image => {
139
- if (image === null || typeof image !== 'object') return false
140
- const record = image as Record<string, unknown>
141
- return typeof record.file === 'string' && typeof record.mime === 'string'
142
- })
143
- }
144
-
145
- /** Remove one entry's image files (best effort). */
146
- async function removeEntryFiles(entry: StoredEntry): Promise<void> {
147
- for (const image of entry.images) {
148
- try { await fs.rm(path.join(imagesDir(), image.file), { force: true }) } catch { /* ignore */ }
149
- }
150
- }
151
-
152
- /** Project a stored entry onto the wire shape (image URLs). */
153
- function toWire(entry: StoredEntry): HistoryEntry {
154
- return {
155
- id: entry.id,
156
- createdAt: entry.createdAt,
157
- mode: entry.mode,
158
- model: entry.model,
159
- prompt: entry.prompt,
160
- size: entry.size,
161
- quality: entry.quality,
162
- detail: entry.detail,
163
- n: entry.n,
164
- images: entry.images.map(image => ({
165
- url: `/api/dsh-imagegen/history/image/${image.file}`,
166
- mime: image.mime,
167
- ...image.revisedPrompt === undefined ? {} : { revisedPrompt: image.revisedPrompt },
168
- })),
169
- ...entry.refName === undefined ? {} : { refName: entry.refName },
170
- ...entry.channel === undefined ? {} : { channel: entry.channel },
171
- ...entry.channelId === undefined ? {} : { channelId: entry.channelId },
172
- ...entry.comparisonId === undefined ? {} : { comparisonId: entry.comparisonId },
173
- ...entry.comparisonModels === undefined ? {} : { comparisonModels: entry.comparisonModels },
174
- ...entry.workflow === undefined ? {} : { workflow: entry.workflow },
175
- ...entry.projectId === undefined ? {} : { projectId: entry.projectId },
176
- ...entry.projectName === undefined ? {} : { projectName: entry.projectName },
177
- ...entry.slotKey === undefined ? {} : { slotKey: entry.slotKey },
178
- ...entry.slotLabel === undefined ? {} : { slotLabel: entry.slotLabel },
179
- ...entry.canvas === undefined ? {} : { canvas: entry.canvas },
180
- }
181
- }
182
-
183
- /** List the persisted history, newest first, as wire entries. */
184
- export async function listHistory(): Promise<HistoryEntry[]> {
185
- const entries = await readIndex()
186
- return entries.map(toWire)
187
- }
188
-
189
- /** Append one generation, evicting the oldest beyond HISTORY_MAX. */
190
- export async function appendHistory(input: HistoryEntryInput): Promise<HistoryEntry[]> {
191
- return mutateHistory(async () => {
192
- await ensureDirs()
193
- const prefix = safeId(input.id)
194
- const storedImages: StoredImage[] = []
195
- try {
196
- for (let index = 0; index < input.images.length; index++) {
197
- const image = input.images[index]!
198
- const file = `${prefix}-${index}.${extensionOf(image.mime)}`
199
- await fs.writeFile(path.join(imagesDir(), file), Buffer.from(image.b64, 'base64'))
200
- notifyImageSaved('history', path.join(imagesDir(), file))
201
- storedImages.push({
202
- file,
203
- mime: image.mime,
204
- ...image.revisedPrompt === undefined ? {} : { revisedPrompt: image.revisedPrompt },
205
- })
206
- }
207
- } catch (error) {
208
- await removeEntryFiles({ images: storedImages } as StoredEntry)
209
- throw error
210
- }
211
- const entry: StoredEntry = {
212
- id: input.id,
213
- createdAt: input.createdAt,
214
- mode: input.mode,
215
- model: input.model,
216
- prompt: input.prompt,
217
- size: input.size,
218
- quality: input.quality,
219
- detail: input.detail,
220
- n: input.n,
221
- images: storedImages,
222
- ...input.refName === undefined ? {} : { refName: input.refName },
223
- ...input.channelId === undefined ? {} : { channelId: input.channelId },
224
- ...input.channel === undefined ? {} : { channel: input.channel },
225
- ...input.comparisonId === undefined ? {} : { comparisonId: input.comparisonId },
226
- ...input.comparisonModels === undefined ? {} : { comparisonModels: input.comparisonModels },
227
- ...input.workflow === undefined ? {} : { workflow: input.workflow },
228
- ...input.projectId === undefined ? {} : { projectId: input.projectId },
229
- ...input.projectName === undefined ? {} : { projectName: input.projectName },
230
- ...input.slotKey === undefined ? {} : { slotKey: input.slotKey },
231
- ...input.slotLabel === undefined ? {} : { slotLabel: input.slotLabel },
232
- ...input.canvas === undefined ? {} : { canvas: input.canvas },
233
- }
234
- const merged = [entry, ...await readIndex()]
235
- const kept = merged.slice(0, HISTORY_MAX)
236
- for (const dropped of merged.slice(HISTORY_MAX)) await removeEntryFiles(dropped)
237
- await writeIndex(kept)
238
- return kept.map(toWire)
239
- })
240
- }
241
-
242
- /** Remove one entry (and its image files). */
243
- export async function removeHistory(id: string): Promise<HistoryEntry[]> {
244
- return mutateHistory(async () => {
245
- const previous = await readIndex()
246
- const target = previous.find(entry => entry.id === id)
247
- if (target !== undefined) await removeEntryFiles(target)
248
- const kept = previous.filter(entry => entry.id !== id)
249
- await writeIndex(kept)
250
- return kept.map(toWire)
251
- })
252
- }
253
-
254
- /** Remove every entry (and all image files). */
255
- export async function clearHistory(): Promise<HistoryEntry[]> {
256
- return mutateHistory(async () => {
257
- const previous = await readIndex()
258
- for (const entry of previous) await removeEntryFiles(entry)
259
- await writeIndex([])
260
- return []
261
- })
262
- }
263
-
264
- /** Read one stored image file by its (validated) file name. */
265
- export async function readHistoryImage(file: string): Promise<{ data: Buffer; mime: string } | undefined> {
266
- // Only accept <id>-<index>.<png|jpg|jpeg|webp|gif> — the exact names this
267
- // store writes — so the route can never escape the images directory.
268
- if (!/^[a-zA-Z0-9][a-zA-Z0-9-]*-[0-9]+\.(png|jpg|jpeg|webp|gif)$/.test(file)) return undefined
269
- try {
270
- const data = await fs.readFile(path.join(imagesDir(), file))
271
- return { data, mime: mimeOfFile(file) }
272
- } catch {
273
- return undefined
274
- }
275
- }
1
+ /**
2
+ * Host-persisted generation history: images are stored as individual files
3
+ * under ~/.dsh/dsh-imagegen/images/ and an index.json keeps the metadata +
4
+ * file names. This makes the history survive across browsers/devices that
5
+ * connect to the same DSH host, and keeps list responses small (the browser
6
+ * loads image bytes lazily through the history image route).
7
+ *
8
+ * Framework-free (node:fs only) so the route layer can drive it directly.
9
+ */
10
+
11
+ import { promises as fs } from 'node:fs'
12
+ import path from 'node:path'
13
+ import { HISTORY_MAX, type GenerateMode, type HistoryEntry, type HistoryEntryInput } from './protocol.ts'
14
+ import { notifyImageSaved } from './storage-sync.ts'
15
+ import { imageDataRoot } from './image-storage-path.ts'
16
+
17
+ function historyDir(): string { return imageDataRoot() }
18
+ function indexPath(): string { return path.join(historyDir(), 'index.json') }
19
+ function imagesDir(): string { return path.join(historyDir(), 'images') }
20
+
21
+ // History mutations read and replace one shared index. Serialize them so
22
+ // overlapping requests cannot each read an old index and lose the other's row.
23
+ let pendingMutation: Promise<void> = Promise.resolve()
24
+
25
+ function mutateHistory<T>(operation: () => Promise<T>): Promise<T> {
26
+ const next = pendingMutation.then(operation, operation)
27
+ pendingMutation = next.then(() => undefined, () => undefined)
28
+ return next
29
+ }
30
+
31
+ /** One image's on-disk record (file name + mime, never base64). */
32
+ interface StoredImage {
33
+ file: string
34
+ mime: string
35
+ revisedPrompt?: string
36
+ }
37
+
38
+ /** One entry's on-disk record. */
39
+ interface StoredEntry {
40
+ id: string
41
+ createdAt: number
42
+ mode: GenerateMode
43
+ model: string
44
+ prompt: string
45
+ size: string
46
+ quality: string
47
+ detail: string
48
+ n: number
49
+ images: StoredImage[]
50
+ refName?: string
51
+ channelId?: string
52
+ channel?: string
53
+ comparisonId?: string
54
+ comparisonModels?: string[]
55
+ workflow?: 'ecommerce'
56
+ projectId?: string
57
+ projectName?: string
58
+ slotKey?: string
59
+ slotLabel?: string
60
+ canvas?: HistoryEntryInput['canvas']
61
+ }
62
+
63
+ /** The index.json shape. */
64
+ interface IndexFile {
65
+ entries: StoredEntry[]
66
+ }
67
+
68
+ /** File extension for a MIME type (image file names). */
69
+ function extensionOf(mime: string): string {
70
+ switch (mime.split(';')[0]!.trim()) {
71
+ case 'image/jpeg': return 'jpg'
72
+ case 'image/webp': return 'webp'
73
+ case 'image/gif': return 'gif'
74
+ default: return 'png'
75
+ }
76
+ }
77
+
78
+ /** MIME type for a stored image file name (image route responses). */
79
+ function mimeOfFile(file: string): string {
80
+ const ext = path.extname(file).toLowerCase()
81
+ switch (ext) {
82
+ case '.jpg':
83
+ case '.jpeg': return 'image/jpeg'
84
+ case '.webp': return 'image/webp'
85
+ case '.gif': return 'image/gif'
86
+ default: return 'image/png'
87
+ }
88
+ }
89
+
90
+ /** Sanitize an entry id for use as a file-name prefix. */
91
+ function safeId(id: string): string {
92
+ const cleaned = id.replace(/[^a-zA-Z0-9-]/g, '-')
93
+ return cleaned === '' ? 'entry' : cleaned
94
+ }
95
+
96
+ /** Ensure the storage directories exist. */
97
+ async function ensureDirs(): Promise<void> {
98
+ await fs.mkdir(imagesDir(), { recursive: true })
99
+ }
100
+
101
+ /** Read the index, tolerating a missing/corrupt file. */
102
+ async function readIndex(): Promise<StoredEntry[]> {
103
+ try {
104
+ const raw = await fs.readFile(indexPath(), 'utf8')
105
+ const parsed: unknown = JSON.parse(raw)
106
+ if (parsed === null || typeof parsed !== 'object') return []
107
+ const entries = (parsed as { entries?: unknown }).entries
108
+ if (!Array.isArray(entries)) return []
109
+ return entries.filter(isStoredEntry)
110
+ } catch {
111
+ return []
112
+ }
113
+ }
114
+
115
+ /** Persist the index. */
116
+ async function writeIndex(entries: StoredEntry[]): Promise<void> {
117
+ await ensureDirs()
118
+ const payload: IndexFile = { entries }
119
+ const tmp = `${indexPath()}.tmp-${process.pid}`
120
+ await fs.writeFile(tmp, JSON.stringify(payload), 'utf8')
121
+ await fs.rename(tmp, indexPath())
122
+ }
123
+
124
+ /** Structural guard for a stored entry. */
125
+ function isStoredEntry(value: unknown): value is StoredEntry {
126
+ if (value === null || typeof value !== 'object') return false
127
+ const entry = value as Record<string, unknown>
128
+ return typeof entry.id === 'string'
129
+ && typeof entry.createdAt === 'number'
130
+ && (entry.mode === 'text' || entry.mode === 'edit')
131
+ && (entry.workflow === undefined || entry.workflow === 'ecommerce')
132
+ && (entry.projectId === undefined || typeof entry.projectId === 'string')
133
+ && (entry.projectName === undefined || typeof entry.projectName === 'string')
134
+ && (entry.slotKey === undefined || typeof entry.slotKey === 'string')
135
+ && (entry.slotLabel === undefined || typeof entry.slotLabel === 'string')
136
+ && typeof entry.prompt === 'string'
137
+ && Array.isArray(entry.images)
138
+ && entry.images.every(image => {
139
+ if (image === null || typeof image !== 'object') return false
140
+ const record = image as Record<string, unknown>
141
+ return typeof record.file === 'string' && typeof record.mime === 'string'
142
+ })
143
+ }
144
+
145
+ /** Remove one entry's image files (best effort). */
146
+ async function removeEntryFiles(entry: StoredEntry): Promise<void> {
147
+ for (const image of entry.images) {
148
+ try { await fs.rm(path.join(imagesDir(), image.file), { force: true }) } catch { /* ignore */ }
149
+ }
150
+ }
151
+
152
+ /** Project a stored entry onto the wire shape (image URLs). */
153
+ function toWire(entry: StoredEntry): HistoryEntry {
154
+ return {
155
+ id: entry.id,
156
+ createdAt: entry.createdAt,
157
+ mode: entry.mode,
158
+ model: entry.model,
159
+ prompt: entry.prompt,
160
+ size: entry.size,
161
+ quality: entry.quality,
162
+ detail: entry.detail,
163
+ n: entry.n,
164
+ images: entry.images.map(image => ({
165
+ url: `/api/dsh-imagegen/history/image/${image.file}`,
166
+ mime: image.mime,
167
+ ...image.revisedPrompt === undefined ? {} : { revisedPrompt: image.revisedPrompt },
168
+ })),
169
+ ...entry.refName === undefined ? {} : { refName: entry.refName },
170
+ ...entry.channel === undefined ? {} : { channel: entry.channel },
171
+ ...entry.channelId === undefined ? {} : { channelId: entry.channelId },
172
+ ...entry.comparisonId === undefined ? {} : { comparisonId: entry.comparisonId },
173
+ ...entry.comparisonModels === undefined ? {} : { comparisonModels: entry.comparisonModels },
174
+ ...entry.workflow === undefined ? {} : { workflow: entry.workflow },
175
+ ...entry.projectId === undefined ? {} : { projectId: entry.projectId },
176
+ ...entry.projectName === undefined ? {} : { projectName: entry.projectName },
177
+ ...entry.slotKey === undefined ? {} : { slotKey: entry.slotKey },
178
+ ...entry.slotLabel === undefined ? {} : { slotLabel: entry.slotLabel },
179
+ ...entry.canvas === undefined ? {} : { canvas: entry.canvas },
180
+ }
181
+ }
182
+
183
+ /** List the persisted history, newest first, as wire entries. */
184
+ export async function listHistory(): Promise<HistoryEntry[]> {
185
+ const entries = await readIndex()
186
+ return entries.map(toWire)
187
+ }
188
+
189
+ /** Append one generation, evicting the oldest beyond HISTORY_MAX. */
190
+ export async function appendHistory(input: HistoryEntryInput): Promise<HistoryEntry[]> {
191
+ return mutateHistory(async () => {
192
+ await ensureDirs()
193
+ const prefix = safeId(input.id)
194
+ const storedImages: StoredImage[] = []
195
+ try {
196
+ for (let index = 0; index < input.images.length; index++) {
197
+ const image = input.images[index]!
198
+ const file = `${prefix}-${index}.${extensionOf(image.mime)}`
199
+ await fs.writeFile(path.join(imagesDir(), file), Buffer.from(image.b64, 'base64'))
200
+ notifyImageSaved('history', path.join(imagesDir(), file))
201
+ storedImages.push({
202
+ file,
203
+ mime: image.mime,
204
+ ...image.revisedPrompt === undefined ? {} : { revisedPrompt: image.revisedPrompt },
205
+ })
206
+ }
207
+ } catch (error) {
208
+ await removeEntryFiles({ images: storedImages } as StoredEntry)
209
+ throw error
210
+ }
211
+ const entry: StoredEntry = {
212
+ id: input.id,
213
+ createdAt: input.createdAt,
214
+ mode: input.mode,
215
+ model: input.model,
216
+ prompt: input.prompt,
217
+ size: input.size,
218
+ quality: input.quality,
219
+ detail: input.detail,
220
+ n: input.n,
221
+ images: storedImages,
222
+ ...input.refName === undefined ? {} : { refName: input.refName },
223
+ ...input.channelId === undefined ? {} : { channelId: input.channelId },
224
+ ...input.channel === undefined ? {} : { channel: input.channel },
225
+ ...input.comparisonId === undefined ? {} : { comparisonId: input.comparisonId },
226
+ ...input.comparisonModels === undefined ? {} : { comparisonModels: input.comparisonModels },
227
+ ...input.workflow === undefined ? {} : { workflow: input.workflow },
228
+ ...input.projectId === undefined ? {} : { projectId: input.projectId },
229
+ ...input.projectName === undefined ? {} : { projectName: input.projectName },
230
+ ...input.slotKey === undefined ? {} : { slotKey: input.slotKey },
231
+ ...input.slotLabel === undefined ? {} : { slotLabel: input.slotLabel },
232
+ ...input.canvas === undefined ? {} : { canvas: input.canvas },
233
+ }
234
+ const merged = [entry, ...await readIndex()]
235
+ const kept = merged.slice(0, HISTORY_MAX)
236
+ for (const dropped of merged.slice(HISTORY_MAX)) await removeEntryFiles(dropped)
237
+ await writeIndex(kept)
238
+ return kept.map(toWire)
239
+ })
240
+ }
241
+
242
+ /** Remove one entry (and its image files). */
243
+ export async function removeHistory(id: string): Promise<HistoryEntry[]> {
244
+ return mutateHistory(async () => {
245
+ const previous = await readIndex()
246
+ const target = previous.find(entry => entry.id === id)
247
+ if (target !== undefined) await removeEntryFiles(target)
248
+ const kept = previous.filter(entry => entry.id !== id)
249
+ await writeIndex(kept)
250
+ return kept.map(toWire)
251
+ })
252
+ }
253
+
254
+ /** Remove every entry (and all image files). */
255
+ export async function clearHistory(): Promise<HistoryEntry[]> {
256
+ return mutateHistory(async () => {
257
+ const previous = await readIndex()
258
+ for (const entry of previous) await removeEntryFiles(entry)
259
+ await writeIndex([])
260
+ return []
261
+ })
262
+ }
263
+
264
+ /** Read one stored image file by its (validated) file name. */
265
+ export async function readHistoryImage(file: string): Promise<{ data: Buffer; mime: string } | undefined> {
266
+ // Only accept <id>-<index>.<png|jpg|jpeg|webp|gif> — the exact names this
267
+ // store writes — so the route can never escape the images directory.
268
+ if (!/^[a-zA-Z0-9][a-zA-Z0-9-]*-[0-9]+\.(png|jpg|jpeg|webp|gif)$/.test(file)) return undefined
269
+ try {
270
+ const data = await fs.readFile(path.join(imagesDir(), file))
271
+ return { data, mime: mimeOfFile(file) }
272
+ } catch {
273
+ return undefined
274
+ }
275
+ }
@@ -1,12 +1,12 @@
1
- import { homedir } from 'node:os'
2
- import path from 'node:path'
3
-
4
- const DEFAULT_ROOT = path.join(process.env.DSH_HOME?.trim() || path.join(homedir(), '.dsh'), 'dsh-imagegen')
5
- let root = DEFAULT_ROOT
6
-
7
- export function imageDataRoot(): string { return root }
8
-
9
- export function setImageDataRoot(value: string | undefined): void {
10
- const trimmed = value?.trim()
11
- root = trimmed === undefined || trimmed === '' ? DEFAULT_ROOT : path.resolve(trimmed)
12
- }
1
+ import { homedir } from 'node:os'
2
+ import path from 'node:path'
3
+
4
+ const DEFAULT_ROOT = path.join(process.env.DSH_HOME?.trim() || path.join(homedir(), '.dsh'), 'dsh-imagegen')
5
+ let root = DEFAULT_ROOT
6
+
7
+ export function imageDataRoot(): string { return root }
8
+
9
+ export function setImageDataRoot(value: string | undefined): void {
10
+ const trimmed = value?.trim()
11
+ root = trimmed === undefined || trimmed === '' ? DEFAULT_ROOT : path.resolve(trimmed)
12
+ }