11ty new post node script

May 13, 2023 *

I asked ChatGPT to write a node script to scaffold out new 11ty posts and here's what it gave me.

const fs = require("fs");
const readline = require("readline");
const { kebabCase } = require("lodash");

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,

// Prompt user for the name of the blog post
rl.question("Enter the name of the blog post: ", (name) => {
const folderName = kebabCase(name);
const directoryPath = `./${folderName}`;

// Create the folder with kebab-cased name
fs.mkdir(directoryPath, { recursive: true }, (error) => {
if (error) {
console.error(`An error occurred while creating the directory: ${error}`);

const filePath = `${directoryPath}/index.md`;
const frontmatter = `---
title: "
${new Date().toISOString()}


// Create the index.md file with relevant frontmatter
fs.writeFile(filePath, frontmatter, (error) => {
if (error) {
console.error(`An error occurred while creating the file: ${error}`);
} else {
console.log(`Blog post scaffolded successfully! Folder: ${folderName}`);

This ended up working really well. I customized the line where the directoryPath is set.

const directoryPath = `./posts/${new Date().getFullYear()}/${folderName}`;

Then I just add it as a script to my package.json.

"scripts": {
"post": "node ./new-blog-post.js",

👍 Now I can scaffold a new post by running npm run post. 💥 This puts the post in the right place for my project with the year in it. Example below:

title: "11ty new post node script"
date: 2023-05-13T18:21:59.956Z