Class: Brut::CLI::Apps::Scaffold::CustomElementTest

Inherits:
BaseCommand show all
Defined in:
lib/brut/cli/apps/scaffold.rb

Instance Attribute Summary

Attributes inherited from Commands::BaseCommand

#parent_command

Instance Method Summary collapse

Methods inherited from BaseCommand

#bootstrap?, #default_rack_env, #opts

Methods inherited from Commands::BaseCommand

#accepts, #argv, #bootstrap?, #commands, #default_rack_env, #delegate_to_command, #detailed_description, #env, #env_vars, #execute, #name, #options, #opts, #puts, #stdin, #system!, #theme

Instance Method Details

#args_descriptionObject



566
# File 'lib/brut/cli/apps/scaffold.rb', line 566

def args_description = "path_to_js_files..."

#descriptionObject



565
# File 'lib/brut/cli/apps/scaffold.rb', line 565

def description = "Create a test for a custom element in your app"

#runObject



567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
# File 'lib/brut/cli/apps/scaffold.rb', line 567

def run
  if argv.empty?
    error "'custom-element-test' requires one or more files to scaffold a test for"
    return 1
  end

  if argv.any? { |file| Pathname(file).extname != ".js" }
    error "'custom-element-test' must be given only .js files"
    return 1
  end

  files_to_create = argv.map { |arg|
    path = Pathname(arg).expand_path
    relative_path = path.relative_path_from(Brut.container.js_src_dir)
    relative_path_as_spec = relative_path.dirname / (relative_path.basename(relative_path.extname).to_s + ".spec.js")
    spec_path = Brut.container.js_specs_dir / relative_path_as_spec
    [ path, spec_path ]
  }

  existing_files = files_to_create.select { |_,spec|
    spec.exist?
  }

  if existing_files.any? && !options.overwrite?
    relative_paths = existing_files.map { |_,pathname| pathname.relative_path_from(Brut.container.project_root) }
    error "Some files to be generated already exist. Set global option --overwrite to overwrite them:"
    relative_paths.each do |file|
      error file
    end
    return 1
  end

  files_to_create.each do |source_file, spec_file|
    source_class = source_file.basename(source_file.extname)
    tag_name = File.read(source_file).split(/\n/).map { |line|
      if line =~ /static\s+tagName\s*=\s*\"([^"]+)\"/
        "<#{$1}>"
      else
        nil
      end
    }.compact.first
    description = tag_name || source_class
    code =  %{import { withHTML } from "brut-js/testing/index.js"

describe("#{description}", () => {
  withHTML(`
  #{ tag_name ? "#{tag_name}" : "<!-- HTML here -->" }
  #{ tag_name ? "#{tag_name.gsub(/^</,'</')}" : "" }
  `).test("description here", ({document,window,assert}) => {
assert.fail("test goes here")
  })
})}
    if options.dry_run?
      puts "Would generate this code:\n\n#{code}"
    else
      File.open(spec_file, "w") do |file|
        file.puts code
      end
    end
  end

  0
end