1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-11-25 03:14:01 +01:00
blog/scripts/new.ts

120 lines
3.2 KiB
TypeScript
Raw Normal View History

2023-01-29 22:58:19 +01:00
import fastExif from 'fast-exif'
2018-11-01 19:35:18 +01:00
import fs from 'fs-extra'
2023-01-29 22:58:19 +01:00
import iptc from 'node-iptc'
import ora from 'ora'
2018-09-24 01:09:50 +02:00
import path from 'path'
import slugify from 'slugify'
2021-02-28 04:09:56 +01:00
const templatePath = path.join(__dirname, 'new-article.md')
2018-11-05 23:32:02 +01:00
const templatePathPhoto = path.join(__dirname, 'new-photo.md')
2018-09-24 01:09:50 +02:00
const template = fs.readFileSync(templatePath).toString()
2018-11-05 23:32:02 +01:00
const templatePhoto = fs.readFileSync(templatePathPhoto).toString()
2018-09-24 01:09:50 +02:00
2018-11-05 23:32:02 +01:00
const spinner = ora('Adding new post').start()
2018-09-24 01:09:50 +02:00
if (!process.argv[2]) {
2018-09-29 23:05:14 +02:00
spinner.fail('Use the format `npm run new "Title of post"`')
2018-09-24 01:09:50 +02:00
}
2019-10-05 00:30:38 +02:00
let title = process.argv[2]
const isPhoto = process.argv[2] === 'photo'
2018-11-05 23:32:02 +01:00
2018-09-24 01:09:50 +02:00
spinner.text = `Adding '${title}'.`
2019-10-05 00:30:38 +02:00
let titleSlug = slugify(title, { lower: true })
2021-02-28 04:09:56 +01:00
const postsPath = path.join('.', 'content', 'articles')
2018-11-05 23:32:02 +01:00
const photosPath = path.join('.', 'content', 'photos')
2018-11-06 00:28:52 +01:00
let date = new Date().toISOString()
2018-09-29 23:05:14 +02:00
2021-02-28 04:09:56 +01:00
async function getIptc(imagePath: string) {
2019-10-05 00:30:38 +02:00
return new Promise((resolve, reject) => {
fs.readFile(imagePath, (err, data) => {
if (err) reject(err)
const iptcData = iptc(data)
return resolve(iptcData)
})
})
}
2021-02-28 04:09:56 +01:00
async function getExif(imagePath: string) {
2019-10-05 00:30:38 +02:00
let exifData
try {
exifData = await fastExif.read(imagePath, true)
} catch (error) {
return null
2018-11-06 00:28:52 +01:00
}
2021-08-29 14:59:21 +02:00
let iptcData: any = {}
2019-10-05 00:30:38 +02:00
try {
iptcData = await getIptc(imagePath)
} catch (error) {
return null
}
return { ...exifData, iptc: { ...iptcData } }
}
async function createPhotoPost() {
const photo = process.argv[3]
2020-03-04 19:54:17 +01:00
try {
const exifData = await getExif(photo)
title = exifData.iptc.object_name || exifData.iptc.title
titleSlug = slugify(title, { lower: true })
date = new Date(exifData.exif.DateTimeOriginal).toISOString()
const dateShort = date.slice(0, 10)
const description = exifData.iptc.caption
const fileName = `${dateShort}-${titleSlug}`
const postPhoto = `${photosPath}/${fileName}.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)
// copy photo file in place
2020-05-08 13:27:40 +02:00
fs.copyFile(photo, `${photosPath}/${fileName}.jpg`, (err) => {
2020-03-04 19:54:17 +01:00
if (err) spinner.fail(`Error copying photo file: ${err}`)
})
2018-11-06 00:28:52 +01:00
2020-03-04 19:54:17 +01:00
// create photo post file
2020-05-08 13:27:40 +02:00
fs.appendFile(postPhoto, newContentsPhoto, (err) => {
2020-03-04 19:54:17 +01:00
if (err) spinner.fail(`Error creating photo post: ${err}`)
spinner.succeed(`New photo post '${title}' as '${fileName}.md' created.`)
})
} catch (error) {
console.error(error.message)
}
2019-10-05 00:30:38 +02:00
}
if (isPhoto) {
createPhotoPost()
2018-11-05 23:32:02 +01:00
} else {
2018-11-06 00:28:52 +01:00
if (process.argv[3]) {
date = new Date(process.argv[3]).toISOString()
}
const dateShort = date.slice(0, 10)
const file = `${postsPath}/${dateShort}-${titleSlug}/index.md`
const newContents = template
.split('TITLE')
.join(title)
.split('SLUG')
.join(titleSlug)
.split('DATE')
.join(date)
2018-11-05 23:32:02 +01:00
fs.outputFile(file, newContents)
.then(() => fs.readFile(file, 'utf8'))
.then(() => spinner.succeed(`New post '${title}' created.`))
2021-02-28 04:09:56 +01:00
.catch((err: Error) => spinner.fail(`Error creating post: ${err.message}`))
2018-11-05 23:32:02 +01:00
}