lib/brief/cli/write.rb in brief-1.8.11 vs lib/brief/cli/write.rb in brief-1.8.12
- old
+ new
@@ -1,23 +1,33 @@
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|
- string_args = args.select { |a| a.is_a?(String) }
- model_class = Brief::Model.lookup_class_from_args(string_args)
+ c.action do |args, options|
+ schema_map = Brief.case.schema_map(true)
+ type_alias = args.first
- base_content = ''
+ model_class = schema_map.fetch(type_alias) do
+ raise "Unknown model type: #{ type_alias }. Available types are: #{ schema_map.keys.join(',') }"
+ end
- if model_class && model_class.example_body.to_s.length > 0
- base_content = model_class.example_body
- else
- # document_contents = model_class.inspect + model_class.example_body.to_s
+ 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
- document_contents = ask_editor(base_content)
- file = ask('enter a name for this file:', String)
+ folder = Brief.case.docs_path.join(type_alias.pluralize)
+ folder = folder.exist? ? folder : Brief.case.docs_path
- puts file
- puts document_contents
+ folder.join(file).open("w+") do |fh|
+ fh.write(content)
+ end
+
+ puts "== Successfully created #{ folder.join(file) }"
end
end