-
-
Save mariochavez/864995be1023e9ce9158e5724cafbb37 to your computer and use it in GitHub Desktop.
Create post thor command
This file contains hidden or 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
require "fileutils" | |
class Commands < Thor | |
# @example | |
# thor commands:create_post wrapping-lit-react-components turbo-vs-htmx | |
desc "create_post file", "creates a file or files based on a filepath name and prefills data." | |
def create_post(*files) | |
base_dir = "src/_posts/" | |
files.flatten(1).each do |file| | |
date = Time.now.to_s.split(" ")[0] | |
post_name = base_dir + date + "-" + file + ".md" | |
if File.exist?(post_name) | |
puts "#{post_name} already exists. Skipping..." | |
next | |
end | |
puts "Creating: ", post_name | |
title = file.split("-").map(&:capitalize).join(" ") | |
data = <<~MD | |
--- | |
title: #{title} | |
categories: [] | |
date: #{date} | |
description: | | |
#{title} | |
--- | |
MD | |
File.write(post_name, data) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment