import '@johnlindquist/kit'
let imagePath: string = await getSelectedFile()
if (!imagePath) { imagePath = await selectFile(`Choose an image:`)
}
if (!imagePath) {
exit()
}
const extension: string = path.extname(imagePath)
const allowImageExtensions: string[] = ['.png', '.jpg', '.jpeg', '.gif']
if (!allowImageExtensions.includes(extension)) {
await div(md(`Selected file is not a supported image type.`))
exit()
}
let imageBuffer: Buffer
try {
imageBuffer = await readFile(imagePath)
} catch (error) {
console.error(`Error reading file: ${imagePath}`, error)
await div(md(`Failed to read image file.`))
exit()
throw error
}
const base64Image: string = `data:image/${extension.slice(1)};base64,${imageBuffer.toString('base64')}`
const altText: string = await arg('Enter alt text for the image:')
const markdown: string = ``
const journalDir: string = await env('IMAGE_JOURNAL_DIR', async () => {
return await path({
hint: 'Select a directory to store image journal entries',
})
})
if (!journalDir) {
exit()
}
const journalPath = createPathResolver(journalDir)
await ensureDir(journalPath())
const dashedDate: string = formatDate(new Date(), 'yyyy-MM-dd')
const journalFilePath: string = journalPath(`${dashedDate}.md`)
const addEntry = async (filePath: string, content: string): Promise<void> => {
const timestamp: string = formatDate(new Date(), 'hh:mmaa')
const entry: string = `\n## ${timestamp}\n${content}\n`
try {
await appendFile(filePath, entry)
} catch (error) {
console.error(`Error appending to file: ${filePath}`, error)
await div(md(`Failed to add entry to journal.`))
exit()
throw error
}
}
await addEntry(journalFilePath, markdown)
await div(md(`Added image to journal entry: [${dashedDate}](${journalFilePath})`))
await clipboard.writeText(markdown)