Sha256: c6c2bc3925c726dea64480ea6736ded82dc08240c0f755738c4a826e9fec61ec

Contents?: true

Size: 1.27 KB

Versions: 8

Compression:

Stored size: 1.27 KB

Contents

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

  # TODO
  # We could potential query the available model classes we are aware of
  # and determine which CLI arguments those model classes may ask for.
  c.action do |args, options|
    options.default(root: Pathname(Brief.pwd))
    briefcase = Brief.case = Brief::Briefcase.new(root: options.root)
    type_alias = args.first

    model_class = briefcase.model_classes.find {|c| c.type_alias == type_alias }

    if !model_class.nil?
      content = ask_editor model_class.writing_prompt()
    else
      model_class = briefcase.schema_map.fetch(type_alias, nil)
      content = ask_editor(model_class.example)
    end

    raise "Inavlid model class. Run the schema command to see what is available." if model_class.nil?


    file = ask("Enter a filename")

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

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

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

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
brief-1.12.9 lib/brief/cli/write.rb
brief-1.12.8 lib/brief/cli/write.rb
brief-1.12.7 lib/brief/cli/write.rb
brief-1.12.6 lib/brief/cli/write.rb
brief-1.12.5 lib/brief/cli/write.rb
brief-1.12.4 lib/brief/cli/write.rb
brief-1.12.3 lib/brief/cli/write.rb
brief-1.12.2 lib/brief/cli/write.rb