Sha256: 872ecc7118bcb1f82f434a1ab4449b15bab9c3bb168413ab9546ed3d7fe704c0

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

#!/usr/bin/env ruby

require 'thor'
require 'active_support/inflector'

module Writefully
  class Write < Thor
    desc "new", "creates a new article in the current directory"

    def new type, slug
      create_content_with type, slug
    end

    no_tasks do 
      def create_content_with type, slug
        new_position = get_last_post(type).split(/\//).last.match(/\A\d*/).to_s.to_i + 1
        dir_name = File.join(type.pluralize, [new_position, slug].join('-'))
        Dir.mkdir dir_name
        add_assets dir_name
        add_readme_md dir_name
        add_meta dir_name
      end

      def add_readme_md dir_name
        file = File.new File.join(dir_name, 'README.md'), "w"
        file.puts("Replace with your content")
        file.close
      end

      def add_assets dir_name
        assets_dir_name = File.join(dir_name, 'assets')
        Dir.mkdir assets_dir_name
      end

      def add_meta dir_name
        meta = File.open(File.dirname(__FILE__) + "/../lib/sample/meta.yml").read
        file = File.new File.join(dir_name, 'meta.yml'), "w"
        file.puts meta
        file.close
      end

      def get_last_post type
        Dir.glob("#{type.pluralize}/*").sort_by do |item| 
          item.split(/\//).last.match(/\A\d*/).to_s.to_i
        end.last
      end
    end
  end
end

Writefully::Write.start(ARGV)

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
writefully-0.6.5 bin/wf-write
writefully-0.6.4 bin/wf-write