generated from mateusfg7/nextjs-setup
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(tools): unify content creator scripts
fix #793
- Loading branch information
Showing
6 changed files
with
192 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))}`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))}`) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.