Skip to content

Commit

Permalink
chore(tools): unify content creator scripts
Browse files Browse the repository at this point in the history
fix #793
  • Loading branch information
mateusfg7 committed Mar 1, 2024
1 parent 89c338b commit 06705a8
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 166 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
"start": "next start",
"lint": "next lint",
"bump-tag": "tsx tools/bump-tag-version.ts",
"new:post": "tsx tools/new-post.mts",
"new:til": "tsx tools/new-til.mts",
"new": "tsx tools/new-content.mts",
"prepare": "husky",
"check-unused": "npx depcheck"
},
Expand Down
27 changes: 27 additions & 0 deletions tools/new-content.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { intro, select } from '@clack/prompts'
import c from 'picocolors'

import { newPost } from './new-content/new-post.mjs'
import { newTil } from './new-content/new-til.mjs'

intro(c.bgGreen(c.bold(' Create new content ')))

const content = await select({
message: 'What type of content do you want to create?',
options: [
{ value: 'post', label: 'New post' },
{ value: 'til', label: 'Today I Learned...' }
]
})

switch (content) {
case 'post':
newPost()
break
case 'til':
newTil()
break

default:
break
}
96 changes: 96 additions & 0 deletions tools/new-content/new-post.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { outro, text, select, spinner } from '@clack/prompts'
import { slug } from 'github-slugger'
import fs from 'fs'
import { confirm } from '@clack/prompts'
import c from 'picocolors'

export async function newPost() {
const title = await text({
message: 'Title',
placeholder: 'Advantages of eating',
validate(value) {
if (value.length === 0) return `Value is required!`
}
})
const description = await text({
message: 'Description',
placeholder: 'Concise resume of the post',
validate(value) {
if (value.length === 0) return `Value is required!`
}
})
const category = await select({
message: 'Category',
options: [
{ value: 'Article', label: 'Article' },
{ value: 'How To', label: 'How To' },
{ value: 'List', label: 'List' },
{ value: 'Notes', label: 'Notes' }
]
})
const tags = await text({
message: 'Tags (separated by ",")',
placeholder: 'javascript, rust, css, life'
})
const author = await text({
message: 'Author username',
placeholder: 'mateusfg7',
defaultValue: 'mateusfg7',
initialValue: 'mateusfg7'
})
const status = await select({
message: 'Initial status',
options: [
{ value: 'draft', label: 'Draft' },
{ value: 'planned', label: 'Planned' }
]
})

const haveAssets = await confirm({
message: 'Your posts will have assets?'
})

const s = spinner()

s.start(c.italic('Creating post'))

const CONTENT_DIR_PATH = 'content/posts'
const SLUGGED_TITLE = slug(title.toString())

let postPath: string

if (haveAssets) {
fs.mkdirSync(`${CONTENT_DIR_PATH}/${SLUGGED_TITLE}/assets`, {
recursive: true
})
postPath = `${CONTENT_DIR_PATH}/${SLUGGED_TITLE}/${SLUGGED_TITLE}.mdx`
} else {
postPath = `${CONTENT_DIR_PATH}/${SLUGGED_TITLE}.mdx`
}

const parsedTags = tags
? tags
.toString()
.split(',')
.map(tag => tag.trim())
.toString()
: ''

fs.writeFile(
postPath,
`---
title: '${title.toString()}'
date: '${new Date().toISOString()}'
description: '${description.toString()}'
category: '${category}'
tags: '${parsedTags}'
author: '${author.toString()}'
status: '${status}'
---`,
() => {}
)

s.stop(c.bold('Post created!'))

outro(`📝 The post was saved to ${c.bold(c.green(postPath))}`)
}
68 changes: 68 additions & 0 deletions tools/new-content/new-til.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { intro, outro, text, spinner } from '@clack/prompts'
import { slug } from 'github-slugger'
import fs from 'fs'
import c from 'picocolors'

export async function newTil() {
const title = await text({
message: 'Title',
placeholder: 'Advantages of eating',
validate(value) {
if (value.length === 0) return `Value is required!`
}
})
const description = await text({
message: 'Description',
placeholder: 'Concise resume of the T.I.L',
validate(value) {
if (value.length === 0) return `Value is required!`
}
})

const tags = await text({
message: 'Tags (separated by ",")',
placeholder: 'javascript, rust, css, life'
})

const s = spinner()

s.start(c.italic('Creating T.I.L'))

const padZero = (n: string | number) => (Number(n) < 10 ? `0${n}` : n)

const currDate = new Date()

const TIL_DIR_PATH = 'content/til'
const SLUGGED_TITLE = slug(title.toString()).replaceAll('-', '_')
const DATE_STR = `${padZero(currDate.getFullYear())}-${padZero(
currDate.getMonth() + 1
)}-${padZero(currDate.getDate())}`

const tilPath = `${TIL_DIR_PATH}/${DATE_STR.replaceAll(
'-',
'_'
)}-${SLUGGED_TITLE}.mdx`

const parsedTags = tags
? tags
.toString()
.split(',')
.map(tag => tag.trim())
.toString()
: ''

fs.writeFile(
tilPath,
`---
title: '${title.toString()}'
description: '${description.toString()}'
date: '${DATE_STR}'
tags: [${parsedTags}]
---`,
() => {}
)

s.stop(c.bold('T.I.L created!'))

outro(`📝 The T.I.L was saved to ${c.bold(c.green(tilPath))}`)
}
96 changes: 0 additions & 96 deletions tools/new-post.mts

This file was deleted.

68 changes: 0 additions & 68 deletions tools/new-til.mts

This file was deleted.

0 comments on commit 06705a8

Please sign in to comment.