import_js 0.0.3 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3b23358bb0d61ef8fac8b2e8789f2bfffa092fb9
4
- data.tar.gz: 132643d9f5029de7cf5087d496fedb91e53d903b
3
+ metadata.gz: b70c1d5fa29773c4f00513bb4826ec35b4e5cbae
4
+ data.tar.gz: 235340096abac84c1e4b8d80cd15bb29522bb928
5
5
  SHA512:
6
- metadata.gz: 51cd0b9b042fe549668137d6635a68589d031a72ab40fae39908604101fa632363b701546c194d87b0a3140714c10234434127fe4bd679f40c8d8d56855a26df
7
- data.tar.gz: b7ccd7a213c9dbd9cd80db90b70421dd16e1b4a9d88a79b67b63ea210611ea72f66c805d07c698baf4dae78dd854292012e56b89e07db04f21e9aed627813cfc
6
+ metadata.gz: 029f4a067d7e9e924842aa047f4308113ee8309a01871ad797585da2dd658f3a9b4093fbc3a82df3ab64e96a2746945ed3ac968f9149c0e8e1e20a20864d51ac
7
+ data.tar.gz: 58f4f6b597977e7712a2755f5b7753144727228ddae6b92e01ade3380aa9887df2f688aed993fab7b77d11892deca84816e0f13cec03b4b3a8656f0e1e29dc83
@@ -1,18 +1,47 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'import_js'
4
+ require 'slop'
5
+ require 'json'
6
+
7
+ opts = Slop.parse do |o|
8
+ o.string '-w', '--word', 'a word/variable to import'
9
+ o.array '--selections', 'a list of resolved selections, e.g. Foo:0,Bar:1'
10
+ o.on '-v', '--version', 'print the current version' do
11
+ puts ImportJS::VERSION
12
+ exit
13
+ end
14
+ o.on '-h', '--help', 'prints help' do
15
+ puts o
16
+ exit
17
+ end
18
+ end
4
19
 
5
- word = ARGV.delete_at(0)
6
20
  file_contents = STDIN.read.split("\n")
7
21
 
8
- # Reset stdin so that we can use it to ask users to resolve imports
9
- STDIN.reopen('/dev/tty')
22
+ if opts[:selections]
23
+ # Convert array of string tuples to hash, `word` => `selectedIndex`
24
+ opts[:selections] = Hash[opts[:selections].map do |str|
25
+ tuple = str.split(':')
26
+ [tuple.first, tuple[1].to_i]
27
+ end]
28
+ end
10
29
 
11
- editor = ImportJS::CommandLineEditor.new(word, file_contents)
30
+ editor = ImportJS::CommandLineEditor.new(file_contents, opts)
12
31
  importer = ImportJS::Importer.new(editor)
13
- if word
32
+ if opts[:word]
14
33
  importer.import
15
34
  else
16
35
  importer.import_all
17
36
  end
37
+
38
+ # Print resulting file to stdout
18
39
  puts editor.current_file_content
40
+
41
+ # Print messages to stderr
42
+ meta = {
43
+ messages: editor.messages
44
+ }
45
+ ask = editor.ask_for_selections
46
+ meta[:ask_for_selections] = ask unless ask.empty?
47
+ STDERR.puts meta.to_json
@@ -8,10 +8,11 @@ module ImportJS
8
8
  end
9
9
  end
10
10
 
11
- require_relative 'import_js/js_module'
11
+ require_relative 'import_js/command_line_editor'
12
+ require_relative 'import_js/configuration'
13
+ require_relative 'import_js/emacs_editor'
12
14
  require_relative 'import_js/import_statement'
13
15
  require_relative 'import_js/importer'
16
+ require_relative 'import_js/js_module'
17
+ require_relative 'import_js/version'
14
18
  require_relative 'import_js/vim_editor'
15
- require_relative 'import_js/emacs_editor'
16
- require_relative 'import_js/command_line_editor'
17
- require_relative 'import_js/configuration'
@@ -1,8 +1,11 @@
1
1
  module ImportJS
2
2
  class CommandLineEditor
3
- def initialize(word, lines)
4
- @word = word
3
+ def initialize(lines, opts)
5
4
  @lines = lines
5
+ @messages = []
6
+ @ask_for_selections = []
7
+ @selections = opts[:selections] unless opts[:selections].empty?
8
+ @word = opts[:word]
6
9
  end
7
10
 
8
11
  # @return [String]
@@ -10,6 +13,12 @@ module ImportJS
10
13
  @word
11
14
  end
12
15
 
16
+ # @return [String?]
17
+ def path_to_current_file
18
+ # Not yet implemented.
19
+ nil
20
+ end
21
+
13
22
  # @param file_path [String]
14
23
  def open_file(_file_path)
15
24
  fail 'not supported'
@@ -17,7 +26,12 @@ module ImportJS
17
26
 
18
27
  # @param str [String]
19
28
  def message(str)
20
- puts str
29
+ @messages << str
30
+ end
31
+
32
+ # @return [Array]
33
+ def ask_for_selections
34
+ @ask_for_selections
21
35
  end
22
36
 
23
37
  # @return [String]
@@ -25,6 +39,11 @@ module ImportJS
25
39
  @lines.join("\n")
26
40
  end
27
41
 
42
+ # @return [String]
43
+ def messages
44
+ @messages.join("\n")
45
+ end
46
+
28
47
  # Reads a line from the file.
29
48
  #
30
49
  # Lines are one-indexed, so 1 means the first line in the file.
@@ -75,31 +94,31 @@ module ImportJS
75
94
 
76
95
  # Ask the user to select something from a list of alternatives.
77
96
  #
78
- # @param heading [String] A heading text
97
+ # @param word [String] The word/variable to import
79
98
  # @param alternatives [Array<String>] A list of alternatives
80
99
  # @return [Number, nil] the index of the selected alternative, or nil if
81
100
  # nothing was selected.
82
- def ask_for_selection(heading, alternatives)
83
- puts heading
84
- alternatives.each_with_index do |alt, i|
85
- puts "#{i + 1}. #{alt}"
101
+ def ask_for_selection(word, alternatives)
102
+ if @selections
103
+ # this is a re-run, where selections have already been made
104
+ @selections[word]
105
+ else
106
+ @ask_for_selections << {
107
+ word: word,
108
+ alternatives: alternatives
109
+ }
110
+ nil
86
111
  end
87
- print 'Select number: '
88
- selected = gets.strip
89
- selected_index = selected.to_i - 1
90
- return nil if selected_index < 0
91
- return nil if selected_index >= alternatives.length
92
- selected_index
93
112
  end
94
113
 
95
- # Get the preferred max length of a line
114
+ # Get the preferred max length of a line.
96
115
  # @return [Number?]
97
116
  def max_line_length
98
117
  80
99
118
  end
100
119
 
101
120
  # @return [String] shiftwidth number of spaces if expandtab is not set,
102
- # otherwise `\t`
121
+ # otherwise `\t`.
103
122
  def tab
104
123
  ' '
105
124
  end
@@ -113,13 +113,13 @@ class ImportJS::EmacsEditor
113
113
 
114
114
  # Ask the user to select something from a list of alternatives.
115
115
  #
116
- # @param heading [String] A heading text
116
+ # @param word [String] The word/variable to import
117
117
  # @param alternatives [Array<String>] A list of alternatives
118
118
  # @return [Number, nil] the index of the selected alternative, or nil if
119
119
  # nothing was selected.
120
- def ask_for_selection(heading, alternatives)
120
+ def ask_for_selection(word, alternatives)
121
121
  puts "asking for selection"
122
- puts heading
122
+ puts "ImportJS: Pick JS module to import for '#{word}':"
123
123
  puts JSON.pretty_generate(alternatives)
124
124
  return
125
125
 
@@ -288,7 +288,7 @@ module ImportJS
288
288
  end
289
289
 
290
290
  selected_index = @editor.ask_for_selection(
291
- "\"ImportJS: Pick JS module to import for '#{variable_name}': #{timing}\"",
291
+ variable_name,
292
292
  js_modules.map(&:display_name)
293
293
  )
294
294
  return unless selected_index
@@ -0,0 +1,4 @@
1
+ # Defines the gem version.
2
+ module ImportJS
3
+ VERSION = '0.1.0'
4
+ end
@@ -93,12 +93,13 @@ module ImportJS
93
93
 
94
94
  # Ask the user to select something from a list of alternatives.
95
95
  #
96
- # @param heading [String] A heading text
96
+ # @param word [String] The word/variable to import
97
97
  # @param alternatives [Array<String>] A list of alternatives
98
98
  # @return [Number, nil] the index of the selected alternative, or nil if
99
99
  # nothing was selected.
100
- def ask_for_selection(heading, alternatives)
101
- escaped_list = [heading]
100
+ def ask_for_selection(word, alternatives)
101
+ escaped_list =
102
+ ["\"ImportJS: Pick JS module to import for '#{word}':\""]
102
103
  escaped_list << alternatives.each_with_index.map do |alternative, i|
103
104
  "\"#{i + 1}: #{alternative}\""
104
105
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: import_js
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henric Trotzig
@@ -9,7 +9,27 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2015-11-15 00:00:00.000000000 Z
12
- dependencies: []
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: slop
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 4.2.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '4.2'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 4.2.1
13
33
  description: A tool to simplify importing javascript modules
14
34
  email: henric.trotzig@gmail.com
15
35
  executables:
@@ -25,6 +45,7 @@ files:
25
45
  - lib/import_js/import_statement.rb
26
46
  - lib/import_js/importer.rb
27
47
  - lib/import_js/js_module.rb
48
+ - lib/import_js/version.rb
28
49
  - lib/import_js/vim_editor.rb
29
50
  homepage: http://rubygems.org/gems/import_js
30
51
  licenses: