Sha256: 9152f1e5233cdbd13563613ca4777b4ead0bbec27ee5462c053502bab94323c1

Contents?: true

Size: 1.52 KB

Versions: 52

Compression:

Stored size: 1.52 KB

Contents

require 'jsduck/util/parallel'
require 'jsduck/util/stdout'
require 'jsduck/util/json'
require 'jsduck/logger'
require 'fileutils'

module JsDuck

  # Writes guide data into files in JSON or JSONP format or to STDOUT.
  class GuideWriter
    def initialize(exporter_class, guides, opts)
      @guides = guides
      @exporter = exporter_class.new(guides, opts)
    end

    # Writes guide data into given directory or STDOUT when dir == :stdout.
    #
    # Extension is either ".json" for normal JSON output
    # or ".js" for JsonP output.
    def write(dir, extension)
      dir == :stdout ? write_stdout : write_dir(dir, extension)
    end

    private

    def write_stdout
      json = ParallelWrap.map(all_guides) {|guide| @exporter.export_guide(guide) }.compact
      Util::Stdout.add(json)
    end

    def write_dir(dir, extension)
      FileUtils.mkdir(dir) unless File.exists?(dir)
      Util::Parallel.each(all_guides) do |guide|
        filename = dir + "/" + guide["name"] + extension
        Logger.log("Writing guide", filename)
        json = @exporter.export_guide(guide)
        # skip file if exporter returned nil
        if json
          if extension == ".json"
            Util::Json.write_json(filename, json)
          elsif extension == ".js"
            Util::Json.write_jsonp(filename, guide["name"], json)
          else
            throw "Unexpected file extension: #{extension}"
          end
        end
      end
    end

    def all_guides
      arr = []
      @guides.each_item {|g| arr << g }
      arr
    end

  end

end

Version data entries

52 entries across 52 versions & 3 rubygems

Version Path
jsduck-4.6.1 lib/jsduck/guide_writer.rb
jsduck-4.6.0 lib/jsduck/guide_writer.rb
jsduck-4.5.1 lib/jsduck/guide_writer.rb
jsduck-4.5.0 lib/jsduck/guide_writer.rb
jsduck-4.4.1 lib/jsduck/guide_writer.rb
jsduck-4.4.0 lib/jsduck/guide_writer.rb
jsduck-4.3.2 lib/jsduck/guide_writer.rb
jsduck-4.3.1 lib/jsduck/guide_writer.rb
jsduck-4.3.0 lib/jsduck/guide_writer.rb
jsduck-4.2.1 lib/jsduck/guide_writer.rb
jsduck-4.2.0 lib/jsduck/guide_writer.rb
jsduck-4.1.1 lib/jsduck/guide_writer.rb