Skip to content

Instantly share code, notes, and snippets.

@mariochavez
Forked from KonnorRogers/commands.thor
Created July 15, 2023 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariochavez/864995be1023e9ce9158e5724cafbb37 to your computer and use it in GitHub Desktop.
Save mariochavez/864995be1023e9ce9158e5724cafbb37 to your computer and use it in GitHub Desktop.
Create post thor command
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