Sha256: 4ccf1783c204a68fe4a718e596df17985ea0cb73b25e1428704c704a4f3f927c

Contents?: true

Size: 998 Bytes

Versions: 6

Compression:

Stored size: 998 Bytes

Contents

command 'write' do |c|
  c.syntax = 'brief write MODEL_TYPE [OPTIONS]'
  c.description = 'Create a new document for a given model type'

  c.action do |args, options|
    schema_map = Brief.case.schema_map(true)
    type_alias = args.first

    model_class = schema_map.fetch(type_alias) do
      raise "Unknown model type: #{ type_alias }. Available types are: #{ schema_map.keys.join(',') }"
    end

    default_example = "---\ntype:#{type_alias}\n---\n\n# Enter some content"

    content = ask_editor(model_class.to_mash.example || default_example)

    file = ask("Enter a filename")

    if file.to_s.length == 0
      rand_token = rand(36**36).to_s(36).slice(0,6)
      file = "new-#{ type_alias }-#{ rand_token }.md"
    end

    folder = Brief.case.docs_path.join(type_alias.pluralize)
    folder = folder.exist? ? folder : Brief.case.docs_path

    folder.join(file).open("w+") do |fh|
      fh.write(content)
    end

    puts "== Successfully created #{ folder.join(file) }"
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
brief-1.9.4 lib/brief/cli/write.rb
brief-1.9.3 lib/brief/cli/write.rb
brief-1.9.2 lib/brief/cli/write.rb
brief-1.9.1 lib/brief/cli/write.rb
brief-1.9.0 lib/brief/cli/write.rb
brief-1.8.12 lib/brief/cli/write.rb