Commit 99eed460 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Added CLI options for configuring jsduck script.

--output now specifies output directory.  The tree is now written to
  file instead of stdio.
--verbose turns on printing of parsing progress.
parent 0bd5ca9f
Loading
Loading
Loading
Loading
+47 −7
Original line number Diff line number Diff line
@@ -7,6 +7,8 @@ require 'jsduck/merger'
require 'jsduck/tree'
require 'json'

require 'optparse'
require 'fileutils'
require 'pp'

module JsDuck
@@ -34,10 +36,10 @@ module JsDuck

  # Given array of filenames, parses all files and returns array of
  # documented items in all of those files.
  def JsDuck.parse_files(filenames)
  def JsDuck.parse_files(filenames, verbose)
    docs = []
    filenames.each do |name|
      puts "Parsing #{name} ..."
      puts "Parsing #{name} ..." if verbose
      JsDuck.parse(IO.read(name)).each { |d| docs << d }
    end
    docs
@@ -56,15 +58,53 @@ module JsDuck
    end
  end

  # Given array of doc-objects, generates namespace tree that can be
  # later printed out in JSON.
  def JsDuck.print_tree(docs)
    puts "Docs.classData = " + JSON.generate( Tree.new.create(docs) ) + ";"
  # Given array of doc-objects, generates namespace tree and writes in
  # in JSON form into a file.
  def JsDuck.write_tree(filename, docs)
    js = "Docs.classData = " + JSON.generate( Tree.new.create(docs) ) + ";"
    File.open(filename, 'w') {|f| f.write(js) }
  end
end


if __FILE__ == $0 then
  JsDuck.print_tree( JsDuck.parse_files(ARGV) )
  output_dir = nil
  verbose = false

  opts = OptionParser.new do | opts |
    opts.banner = "Usage: ruby jsduck.rb [options] files..."

    opts.on('-o', '--output=PATH', "Directory to output all this amazing documentation.") do |path|
      output_dir = path
    end

    opts.on('--verbose', "This will fill up your console.") do
      verbose = true
    end

    opts.on('-h', '--help', "Prints this help message") do
      puts opts
      exit
    end
  end

  input_files = opts.parse!(ARGV)

  if input_files.length == 0
    puts "You should specify some input files, otherwise there's nothing I can do :("
    exit(1)
  elsif !output_dir
    puts "You should also specify an output directory, where I could write all this amazing documentation."
    exit(1)
  elsif !File.exists?(output_dir)
    puts "Output directory doesn't exist.  I'll take the pride of creating it..."
    FileUtils.mkdir(output_dir)
  elsif !File.directory?(output_dir)
    puts "Oh noes!  The output directory is not really a directory at all :("
    exit(1)
  end

  docs = JsDuck.parse_files(input_files, verbose)
  JsDuck.write_tree(output_dir+"/tree.js", docs)
end