-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
createPhotoPost.ts
76 lines (66 loc) · 2.29 KB
/
createPhotoPost.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { promises as fs, existsSync, mkdirSync, readFileSync } from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import type { Ora } from 'ora'
import { readOutExif } from '../../src/lib/exif/readOutExif.js'
import { slugify } from '../../src/lib/slugify/slugify.js'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const templatePathPhoto = path.join(__dirname, 'new-photo.md')
export async function createPhotoPost(
dest: string,
spinner: Ora,
photo: string,
photoTitle?: string
) {
let title: string | undefined
let titleSlug: string
let date: string
let postPhotoFile = ''
try {
const templatePhoto = readFileSync(templatePathPhoto).toString()
const exifData = await readOutExif(photo)
if (!exifData) throw new Error('No exif data found in image')
const { iptc, exif } = exifData
title = iptc?.object_name || iptc?.caption || photoTitle
if (!title)
throw new Error(
'No title given. Add to IPTC, or use the format `npm run new photo path/to/photo.jpg "Title of post"'
)
spinner.text = `Adding '${title}'.`
titleSlug = slugify(title)
date = new Date(exif?.date).toISOString()
const dateShort = date.slice(0, 10)
const description = iptc?.caption
const keywords = (iptc?.keywords as string[])?.join('\n - ')
const folderName = `${dateShort}-${titleSlug}`
const destination = `${dest}/${folderName}`
postPhotoFile = `${destination}/index.md`
const newContentsPhoto = templatePhoto
.split('TITLE')
.join(title)
.split('SLUG')
.join(titleSlug)
.split('DATE_LONG')
.join(date)
.split('DATE_SHORT')
.join(dateShort)
.split('DESCRIPTION')
.join(description)
.split('TAGS')
.join(keywords)
// Create the destination folder if it doesn't exist
if (!existsSync(destination)) {
mkdirSync(destination, { recursive: true })
}
// copy photo file in place
await fs.copyFile(photo, `${destination}/${folderName}.jpg`)
// create photo post file
await fs.appendFile(postPhotoFile, newContentsPhoto)
spinner.succeed(
`New photo post '${title}' under '${postPhotoFile}' created.`
)
} catch (error: unknown) {
spinner.fail((error as Error).message)
}
return postPhotoFile
}