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}`);
rl.close();
return;
}
const filePath = `${directoryPath}/index.md`;
const frontmatter = `---
title: "${name}"
date: ${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}`);
}
rl.close();
});
});
});
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
---