lib/synvert/cli.rb in synvert-0.10.1 vs lib/synvert/cli.rb in synvert-0.11.0

- old
+ new

@@ -26,10 +26,13 @@ case @options[:command] when 'list' load_rewriters list_available_rewriters + when 'list-all' + load_rewriters + list_all_rewriters_in_json when 'open' open_rewriter when 'query' load_rewriters query_available_rewriters @@ -75,10 +78,13 @@ opts.on '-d', '--load SNIPPET_PATHS', 'load custom snippets, snippet paths can be local file path or remote http url' do |snippet_paths| @options[:custom_snippet_paths] = snippet_paths.split(',').map(&:strip) end + opts.on '--list-all', 'list all available snippets name and description in json format' do + @options[:command] = 'list-all' + end opts.on '-l', '--list', 'list all available snippets' do @options[:command] = 'list' end opts.on '-o', '--open SNIPPET_NAME', 'Open a snippet' do |snippet_name| @options[:command] = 'open' @@ -151,9 +157,31 @@ puts ' ' + name end end puts end + end + + def list_all_rewriters_in_json + if Core::Rewriter.availables.empty? + puts 'There is no snippet under ~/.synvert, please run `synvert --sync` to fetch snippets.' + return + end + + output = [] + Core::Rewriter.availables.each do |group, rewriters| + rewriters.each do |name, rewriter| + rewriter.process_with_sandbox + output << { + group: group, + name: name, + description: rewriter.description, + sub_snippets: rewriter.sub_snippets.map(&:name) + } + end + end + + puts JSON.generate(output) end # Open one rewriter. def open_rewriter editor = [ENV['SYNVERT_EDITOR'], ENV['EDITOR']].find { |e| !e.nil? && !e.empty? }