diff --git a/package.json b/package.json index 8ea42361..ee89a03c 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/tools/new-content.mts b/tools/new-content.mts new file mode 100644 index 00000000..c15971d7 --- /dev/null +++ b/tools/new-content.mts @@ -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 +} diff --git a/tools/new-content/new-post.mts b/tools/new-content/new-post.mts new file mode 100644 index 00000000..e0cb3104 --- /dev/null +++ b/tools/new-content/new-post.mts @@ -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))}`) +} diff --git a/tools/new-content/new-til.mts b/tools/new-content/new-til.mts new file mode 100644 index 00000000..64e90020 --- /dev/null +++ b/tools/new-content/new-til.mts @@ -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))}`) +} diff --git a/tools/new-post.mts b/tools/new-post.mts deleted file mode 100644 index 87c8e8a3..00000000 --- a/tools/new-post.mts +++ /dev/null @@ -1,96 +0,0 @@ -import { intro, 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' - -intro(c.bgGreen(c.bold(' Create new post '))) - -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))}`) diff --git a/tools/new-til.mts b/tools/new-til.mts deleted file mode 100644 index 1bbb819d..00000000 --- a/tools/new-til.mts +++ /dev/null @@ -1,68 +0,0 @@ -import { intro, outro, text, spinner } from '@clack/prompts' -import { slug } from 'github-slugger' -import fs from 'fs' -import c from 'picocolors' - -intro(c.bgGreen(c.bold(' Today I Learned... '))) - -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))}`)