import_js 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/import-js +18 -0
- data/lib/import_js/command_line_editor.rb +107 -0
- data/lib/import_js/configuration.rb +0 -51
- data/lib/import_js/importer.rb +4 -10
- data/lib/import_js/vim_editor.rb +60 -0
- data/lib/import_js.rb +1 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 80847dd047d3098f2375708a53acbe8fe3b788d3
|
4
|
+
data.tar.gz: 63db9742a0a4bdb87b019020cf1f20a2fb8f82dd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1c6631a83e5bc9498db3d1c39a41c5f56df7e2a032b1624a7aa4b3d64b6215cb9990369bec4bbabe4406001245ab921228b1298cc75c5935383694316a3a8469
|
7
|
+
data.tar.gz: dcaf4142fb7e906c8cdb466c14a421bc8cd570c38bf240b7b93591dfc554deb025d135dedd45726252fa630133101c39da176987efdd9b09e8dd5d613094ecf7
|
data/bin/import-js
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'import_js'
|
4
|
+
|
5
|
+
word = ARGV.delete_at(0)
|
6
|
+
file_contents = STDIN.read.split("\n")
|
7
|
+
|
8
|
+
# Reset stdin so that we can use it to ask users to resolve imports
|
9
|
+
STDIN.reopen('/dev/tty')
|
10
|
+
|
11
|
+
editor = ImportJS::CommandLineEditor.new(word, file_contents)
|
12
|
+
importer = ImportJS::Importer.new(editor)
|
13
|
+
if word
|
14
|
+
importer.import
|
15
|
+
else
|
16
|
+
importer.import_all
|
17
|
+
end
|
18
|
+
puts editor.current_file_content
|
@@ -0,0 +1,107 @@
|
|
1
|
+
module ImportJS
|
2
|
+
class CommandLineEditor
|
3
|
+
def initialize(word, lines)
|
4
|
+
@word = word
|
5
|
+
@lines = lines
|
6
|
+
end
|
7
|
+
|
8
|
+
# @return [String]
|
9
|
+
def current_word
|
10
|
+
@word
|
11
|
+
end
|
12
|
+
|
13
|
+
# @param file_path [String]
|
14
|
+
def open_file(_file_path)
|
15
|
+
fail 'not supported'
|
16
|
+
end
|
17
|
+
|
18
|
+
# @param str [String]
|
19
|
+
def message(str)
|
20
|
+
puts str
|
21
|
+
end
|
22
|
+
|
23
|
+
# @return [String]
|
24
|
+
def current_file_content
|
25
|
+
@lines.join("\n")
|
26
|
+
end
|
27
|
+
|
28
|
+
# Reads a line from the file.
|
29
|
+
#
|
30
|
+
# Lines are one-indexed, so 1 means the first line in the file.
|
31
|
+
# @return [String]
|
32
|
+
def read_line(line_number)
|
33
|
+
@lines[line_number - 1]
|
34
|
+
end
|
35
|
+
|
36
|
+
# Get the cursor position.
|
37
|
+
#
|
38
|
+
# @return [Array(Number, Number)]
|
39
|
+
def cursor
|
40
|
+
# not relevant for this editor
|
41
|
+
[0, 0]
|
42
|
+
end
|
43
|
+
|
44
|
+
# Place the cursor at a specified position.
|
45
|
+
#
|
46
|
+
# @param _position_tuple [Array(Number, Number)] the row and column to place
|
47
|
+
# the cursor at.
|
48
|
+
def cursor=(_position_tuple)
|
49
|
+
# no-op
|
50
|
+
end
|
51
|
+
|
52
|
+
# Delete a line.
|
53
|
+
#
|
54
|
+
# @param line_number [Number] One-indexed line number.
|
55
|
+
# 1 is the first line in the file.
|
56
|
+
def delete_line(line_number)
|
57
|
+
@lines.delete_at(line_number - 1)
|
58
|
+
end
|
59
|
+
|
60
|
+
# Append a line right after the specified line.
|
61
|
+
#
|
62
|
+
# Lines are one-indexed, but you need to support appending to line 0 (add
|
63
|
+
# content at top of file).
|
64
|
+
# @param line_number [Number]
|
65
|
+
def append_line(line_number, str)
|
66
|
+
@lines.insert(line_number, str)
|
67
|
+
end
|
68
|
+
|
69
|
+
# Count the number of lines in the file.
|
70
|
+
#
|
71
|
+
# @return [Number] the number of lines in the file
|
72
|
+
def count_lines
|
73
|
+
@lines.length
|
74
|
+
end
|
75
|
+
|
76
|
+
# Ask the user to select something from a list of alternatives.
|
77
|
+
#
|
78
|
+
# @param heading [String] A heading text
|
79
|
+
# @param alternatives [Array<String>] A list of alternatives
|
80
|
+
# @return [Number, nil] the index of the selected alternative, or nil if
|
81
|
+
# 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}"
|
86
|
+
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
|
+
end
|
94
|
+
|
95
|
+
# Get the preferred max length of a line
|
96
|
+
# @return [Number?]
|
97
|
+
def max_line_length
|
98
|
+
80
|
99
|
+
end
|
100
|
+
|
101
|
+
# @return [String] shiftwidth number of spaces if expandtab is not set,
|
102
|
+
# otherwise `\t`
|
103
|
+
def tab
|
104
|
+
' '
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -48,23 +48,6 @@ module ImportJS
|
|
48
48
|
nil
|
49
49
|
end
|
50
50
|
|
51
|
-
# @return [Number?]
|
52
|
-
def columns
|
53
|
-
get_number('&columns')
|
54
|
-
end
|
55
|
-
|
56
|
-
# @return [Number?]
|
57
|
-
def text_width
|
58
|
-
get_number('&textwidth')
|
59
|
-
end
|
60
|
-
|
61
|
-
# @return [String] shiftwidth number of spaces if expandtab is not set,
|
62
|
-
# otherwise `\t`
|
63
|
-
def tab
|
64
|
-
return "\t" unless expand_tab?
|
65
|
-
' ' * (shift_width || 2)
|
66
|
-
end
|
67
|
-
|
68
51
|
# @return [Array<String>]
|
69
52
|
def package_dependencies
|
70
53
|
return [] unless File.exist?('package.json')
|
@@ -90,39 +73,5 @@ module ImportJS
|
|
90
73
|
def config_file_last_modified
|
91
74
|
File.exist?(CONFIG_FILE) ? File.mtime(CONFIG_FILE) : nil
|
92
75
|
end
|
93
|
-
|
94
|
-
# Check for the presence of a setting such as:
|
95
|
-
#
|
96
|
-
# - g:CommandTSmartCase (plug-in setting)
|
97
|
-
# - &wildignore (Vim setting)
|
98
|
-
# - +cursorcolumn (Vim setting, that works)
|
99
|
-
#
|
100
|
-
# @param str [String]
|
101
|
-
# @return [Boolean]
|
102
|
-
def exists?(str)
|
103
|
-
VIM.evaluate(%{exists("#{str}")}).to_i != 0
|
104
|
-
end
|
105
|
-
|
106
|
-
# @param name [String]
|
107
|
-
# @return [Number?]
|
108
|
-
def get_number(name)
|
109
|
-
exists?(name) ? VIM.evaluate("#{name}").to_i : nil
|
110
|
-
end
|
111
|
-
|
112
|
-
# @param name [String]
|
113
|
-
# @return [Boolean?]
|
114
|
-
def get_bool(name)
|
115
|
-
exists?(name) ? VIM.evaluate("#{name}").to_i != 0 : nil
|
116
|
-
end
|
117
|
-
|
118
|
-
# @return [Boolean?]
|
119
|
-
def expand_tab?
|
120
|
-
get_bool('&expandtab')
|
121
|
-
end
|
122
|
-
|
123
|
-
# @return [Number?]
|
124
|
-
def shift_width
|
125
|
-
get_number('&shiftwidth')
|
126
|
-
end
|
127
76
|
end
|
128
77
|
end
|
data/lib/import_js/importer.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
1
|
require 'json'
|
3
2
|
require 'open3'
|
4
3
|
|
@@ -58,12 +57,7 @@ module ImportJS
|
|
58
57
|
private
|
59
58
|
|
60
59
|
def message(str)
|
61
|
-
|
62
|
-
if str.length > @config.columns - 1
|
63
|
-
str = str[0...(@config.columns - 2)] + '…'
|
64
|
-
end
|
65
|
-
|
66
|
-
@editor.message(str)
|
60
|
+
@editor.message("[import-js] #{str}")
|
67
61
|
end
|
68
62
|
|
69
63
|
# @return [Array]
|
@@ -112,7 +106,7 @@ module ImportJS
|
|
112
106
|
|
113
107
|
modified_imports = old_imports[:imports] # Array
|
114
108
|
|
115
|
-
# Add new import to the block of imports, wrapping at
|
109
|
+
# Add new import to the block of imports, wrapping at the max line length
|
116
110
|
unless js_module.is_destructured && inject_destructured_variable(
|
117
111
|
variable_name, js_module, modified_imports)
|
118
112
|
modified_imports << generate_import(variable_name, js_module)
|
@@ -191,8 +185,8 @@ module ImportJS
|
|
191
185
|
end
|
192
186
|
value = "require('#{js_module.import_path}');"
|
193
187
|
|
194
|
-
if @
|
195
|
-
"#{declaration}\n#{@
|
188
|
+
if @editor.max_line_length && "#{declaration} #{value}".length > @editor.max_line_length
|
189
|
+
"#{declaration}\n#{@editor.tab}#{value}"
|
196
190
|
else
|
197
191
|
"#{declaration} #{value}"
|
198
192
|
end
|
data/lib/import_js/vim_editor.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
module ImportJS
|
2
3
|
# This is the implementation of the VIM integration in Import-JS. It can be
|
3
4
|
# used as a template for other editor integrations.
|
@@ -20,6 +21,11 @@ module ImportJS
|
|
20
21
|
#
|
21
22
|
# @param str [String]
|
22
23
|
def message(str)
|
24
|
+
# To prevent having to press enter to dismiss, we ellipsize the message
|
25
|
+
if str.length > available_columns - 1
|
26
|
+
str = str[0...(available_columns - 2)] + '…'
|
27
|
+
end
|
28
|
+
|
23
29
|
VIM.command(":call importjs#WideMsg('#{str}')")
|
24
30
|
end
|
25
31
|
|
@@ -94,5 +100,59 @@ module ImportJS
|
|
94
100
|
return if selected_index < 1
|
95
101
|
selected_index - 1
|
96
102
|
end
|
103
|
+
|
104
|
+
# Get the preferred max length of a line
|
105
|
+
# @return [Number?]
|
106
|
+
def max_line_length
|
107
|
+
get_number('&textwidth')
|
108
|
+
end
|
109
|
+
|
110
|
+
# @return [String] shiftwidth number of spaces if expandtab is not set,
|
111
|
+
# otherwise `\t`
|
112
|
+
def tab
|
113
|
+
return "\t" unless expand_tab?
|
114
|
+
' ' * (shift_width || 2)
|
115
|
+
end
|
116
|
+
|
117
|
+
private
|
118
|
+
|
119
|
+
# Check for the presence of a setting such as:
|
120
|
+
#
|
121
|
+
# - g:CommandTSmartCase (plug-in setting)
|
122
|
+
# - &wildignore (Vim setting)
|
123
|
+
# - +cursorcolumn (Vim setting, that works)
|
124
|
+
#
|
125
|
+
# @param str [String]
|
126
|
+
# @return [Boolean]
|
127
|
+
def exists?(str)
|
128
|
+
VIM.evaluate(%{exists("#{str}")}).to_i != 0
|
129
|
+
end
|
130
|
+
|
131
|
+
# @return [Number?]
|
132
|
+
def available_columns
|
133
|
+
get_number('&columns')
|
134
|
+
end
|
135
|
+
|
136
|
+
# @param name [String]
|
137
|
+
# @return [Number?]
|
138
|
+
def get_number(name)
|
139
|
+
exists?(name) ? VIM.evaluate("#{name}").to_i : nil
|
140
|
+
end
|
141
|
+
|
142
|
+
# @param name [String]
|
143
|
+
# @return [Boolean?]
|
144
|
+
def get_bool(name)
|
145
|
+
exists?(name) ? VIM.evaluate("#{name}").to_i != 0 : nil
|
146
|
+
end
|
147
|
+
|
148
|
+
# @return [Boolean?]
|
149
|
+
def expand_tab?
|
150
|
+
get_bool('&expandtab')
|
151
|
+
end
|
152
|
+
|
153
|
+
# @return [Number?]
|
154
|
+
def shift_width
|
155
|
+
get_number('&shiftwidth')
|
156
|
+
end
|
97
157
|
end
|
98
158
|
end
|
data/lib/import_js.rb
CHANGED
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.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Henric Trotzig
|
@@ -12,11 +12,14 @@ date: 2015-11-15 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
13
13
|
description: A tool to simplify importing javascript modules
|
14
14
|
email: henric.trotzig@gmail.com
|
15
|
-
executables:
|
15
|
+
executables:
|
16
|
+
- import-js
|
16
17
|
extensions: []
|
17
18
|
extra_rdoc_files: []
|
18
19
|
files:
|
20
|
+
- bin/import-js
|
19
21
|
- lib/import_js.rb
|
22
|
+
- lib/import_js/command_line_editor.rb
|
20
23
|
- lib/import_js/configuration.rb
|
21
24
|
- lib/import_js/importer.rb
|
22
25
|
- lib/import_js/js_module.rb
|