Poaster/poaster.rb

56 lines
1.8 KiB
Ruby

#!/usr/bin/env ruby
require 'json'
require 'fileutils'
require './lib/spawn_input_box'
require './lib/spawn_radio_list'
require './lib/spawn_toast'
require './lib/ensure_dir_exists'
require './lib/write_file'
config_data = JSON.parse(File.read('./config/config.json'))
dialog_title_prefix = 'Poaster'
# Populate types_arr with post types
post_types_arr = []
config_data['postTypes'].each do |type|
post_types_arr.push(type['name'])
end
# Display post list dialog to user
post_type = config_data['postTypes'][Integer(spawn_radio_list(dialog_title_prefix, 'Select a post type:', post_types_arr))]
# Set the word we will use to refer to the post
post_unit = post_type['postUnitName']
# Collect frontmatter from user
frontmatter = []
post_type['frontMatter'].each do |item|
frontmatter.push({ item['name'] => spawn_input_box(%(#{dialog_title_prefix} - Enter Frontmatter'), %(Enter #{post_unit} #{item['name']}:)) })
end
# Collect post content from user
post_content = spawn_input_box %(#{dialog_title_prefix} - Enter Content), %(Enter #{post_unit} content:)
# Make sure the output folder exists
post_directory = post_type['postDirectory']
ensure_dir_exists(post_directory)
# Create post string
post = %(---\n)
post_id = ''
frontmatter.each_with_index do |item, i|
post += %(#{item.keys[0]}: #{item[item.keys[0]]})
post_id += %(#{item[item.keys[0]].chomp}#{i == frontmatter.length - 1 ? '' : '_'})
end
post += %(---\n#{post_content})
# Write post string to file and notify user
post_file_name = %(#{post_type['name']}_#{post_id.chomp})
post_extension = 'md'
write_file post_directory, post_file_name, post_extension, post
spawn_toast 'File Created', %(Poaster created #{post_file_name}#{post_extension} at #{post_directory}.), 10
# Run build and upload commands
`cd #{config_data['siteDirectory']} && #{config_data['buildCommand']} && #{config_data['uploadCommand']}`