Skip to content

Commit b98244f

Browse files
committed
bug fixes, some enhancement, jest support
1 parent 9bb4c30 commit b98244f

19 files changed

+200
-133
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
# rspec failure tracking
1111
.rspec_status
1212
*.gem
13+
.rubocop-http*

.rubocop.yml

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
1+
inherit_from:
2+
- http://shopify.github.io/ruby-style-guide/rubocop.yml
3+
14
AllCops:
25
TargetRubyVersion: 2.3
36

4-
Metrics/LineLength:
5-
Max: 99
6-
77
Style/FrozenStringLiteralComment:
88
Enabled: false
9-
10-
Style/TrailingCommaInArguments:
11-
EnforcedStyleForMultiline: comma
12-
13-
Style/TrailingCommaInArrayLiteral:
14-
EnforcedStyleForMultiline: comma
15-
16-
Style/TrailingCommaInHashLiteral:
17-
EnforcedStyleForMultiline: comma
18-
19-
Style/Documentation:
20-
Enabled: false

Gemfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
vue_cli-rails (0.1.0)
4+
vue_cli-rails (0.2.0)
55
activesupport (>= 4.2)
66
rack-proxy (>= 0.6)
77
railties (>= 4.2)
@@ -34,7 +34,7 @@ GEM
3434
crass (1.0.4)
3535
diff-lcs (1.3)
3636
erubi (1.8.0)
37-
i18n (1.5.3)
37+
i18n (1.6.0)
3838
concurrent-ruby (~> 1.0)
3939
loofah (2.2.3)
4040
crass (~> 1.0.2)
@@ -96,4 +96,4 @@ DEPENDENCIES
9696
vue_cli-rails!
9797

9898
BUNDLED WITH
99-
1.17.3
99+
1.17.2

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ require 'rspec/core/rake_task'
33

44
RSpec::Core::RakeTask.new(:spec)
55

6-
task default: :spec
6+
task(default: :spec)

bin/console

Lines changed: 0 additions & 14 deletions
This file was deleted.

bin/setup

Lines changed: 0 additions & 8 deletions
This file was deleted.

lib/helpers/lib/input_loop.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ def gets(message, list = 'Yn', **hint)
55
default, list = build_list(list)
66
keys_hint, hint = build_hint(list, hint)
77

8-
print "#{message} (#{hint}) #{keys_hint}"
8+
print("#{message} (#{hint}) #{keys_hint}")
99
wait_valid_input(keys_hint, Set.new(list.map(&:downcase)), default)
1010
end
1111

lib/helpers/scripts/install_rails.rb

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
ver = ENV['RAILS_VERSION']
55
abort('RAILS_VERSION not found!') if ver.blank?
66

7-
versions = `gem list -r -a -e rails`.scan(/\b((\d+\.)+\d+)\b/).map { |m| m[0] }
8-
ver = versions.find { |v| v.start_with? ver }
7+
versions = %x`gem list -r -a -e rails`.scan(/\b((\d+\.)+\d+)\b/).map { |m| m[0] }
8+
ver = versions.find { |v| v.start_with?(ver) }
99
abort("Version #{ver} not found!") if ver.blank?
1010

1111
run("gem install rails -v #{ver}")
@@ -26,23 +26,20 @@
2626
skip-bundle
2727
],
2828
keep_gems: /^\s*gem\s+['"](rails|puma|bootsnap)/,
29-
append: <<~RUBY
30-
gem 'sqlite3', '~> 1.3.10'
31-
RUBY
29+
append: "gem 'sqlite3', '~> 1.3.10'",
3230
},
3331
}
3432

3533
require 'pathname'
3634
require 'fileutils'
3735

3836
scirpt = RAILS_NEW_SCRIPT[ver[0]]
39-
run "rails new test_vcr #{scirpt[:args].map { |a| "--#{a}" }.join(' ')}"
40-
FileUtils.chdir 'test_vcr'
37+
run("rails new test_vcr #{scirpt[:args].map { |a| "--#{a}" }.join(' ')}")
38+
FileUtils.chdir('test_vcr')
4139

4240
root = Pathname.new(FileUtils.pwd)
4341
gemfile = root.join('Gemfile').read.split("\n")
44-
.reject(&:empty?).reject { |s| s =~ /^\s*#/ }
45-
.reject { |s| s =~ /^\s*gem/ && s !~ scirpt[:keep_gems] }
42+
.reject(&:empty?).reject { |s| s =~ /^\s*#/ }
43+
.reject { |s| s =~ /^\s*gem/ && s !~ scirpt[:keep_gems] }
4644
root.join('Gemfile').write("#{(gemfile + [scirpt[:append]]).join("\n")}\n")
47-
run 'bundle install'
48-
# rails new test_vcr --database=sqlite3 --skip-yarn --skip-git --skip-sprockets --skip-spring --skip-listen --skip-turbolinks --skip-javascript --skip-test --skip-bundle
45+
run('bundle install')

lib/helpers/scripts/vue_command.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def install_format_support(formats)
2323
end
2424

2525
STDERR.puts "Unsupported formats #{unknown}" if unknown.any?
26-
@pm.add "-D #{pkgs.join(' ')}"
26+
@pm.add("-D #{pkgs.join(' ')}")
2727
end
2828

2929
def install_node_dev
@@ -32,10 +32,10 @@ def install_node_dev
3232

3333
add_deps(pack_json, %w[cross-env npm-run-all])
3434
add_scripts(pack_json,
35-
dev: 'run-p rails-s serve',
36-
prod: 'cross-env RAILS_ENV=production vue-cli-service build',
37-
serve: 'vue-cli-service serve',
38-
'rails-s' => 'cross-env NO_WEBPACK_DEV_SERVER=1 rails s')
35+
dev: 'run-p rails-s serve',
36+
prod: 'cross-env RAILS_ENV=production vue-cli-service build',
37+
serve: 'vue-cli-service serve',
38+
'rails-s' => 'cross-env NO_WEBPACK_DEV_SERVER=1 rails s')
3939
puts 'Dependencies and scripts have been installed successfully'
4040
cmd = @pm.package_manager == :npm ? 'npm run' : 'yarn'
4141
puts " Please use `#{cmd} dev` to start dev server"
@@ -49,14 +49,14 @@ def add_deps(package_json, *packages, dev: true)
4949
pkgs = [packages].flatten.find_all do |dep|
5050
!(dep.blank? || deps.key?(dep))
5151
end
52-
@pm.add "#{dev ? '-D ' : ''}#{pkgs.join(' ')}" if pkgs.any?
52+
@pm.add("#{dev ? '-D ' : ''}#{pkgs.join(' ')}") if pkgs.any?
5353
end
5454

5555
def add_scripts(package_json, commands = {})
5656
json = JSON.parse(package_json.read)
5757
scripts = json['scripts']
5858
commands.stringify_keys.each do |key, cmd|
59-
scripts[key] = cmd unless scripts.key? key
59+
scripts[key] = cmd unless scripts.key?(key)
6060
end
6161
package_json.write(JSON.pretty_generate(json))
6262
end

lib/helpers/scripts/vue_create.rb

Lines changed: 101 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,22 @@ def self.run!
1515

1616
def start
1717
check_node!
18-
FileUtils.chdir @root
18+
FileUtils.chdir(@root)
19+
@pending_install = false
1920
begin
2021
package_manager?
2122
install_vue_cli
2223
run_vue_create?
2324
install_dev_deps
24-
delete_vue_src?
25+
@pm.install if @pending_install
26+
delete_vue_sample?
2527
copy_demo?
2628
copy_config
2729
generate_vue_yml
30+
fix_jest_config!
2831
puts 'vue:create finished!'
2932
ensure
30-
FileUtils.chdir @pwd
33+
FileUtils.chdir(@pwd)
3134
end
3235
end
3336

@@ -43,14 +46,14 @@ def package_manager?
4346
npm = @pm.npm_version
4447
abort('Cannot find npm or yarn') unless yarn || npm
4548

46-
if yarn && npm
47-
input = @input.gets(
49+
input = if yarn && npm
50+
@input.gets(
4851
'Which package manager to use?',
4952
@root.join('package-lock.json').exist? ? 'yN' : 'Yn',
5053
y: 'yarn', n: 'npm',
5154
)
5255
else
53-
input = npm ? 'n' : 'y'
56+
npm ? 'n' : 'y'
5457
end
5558
@pm.use!(input == 'n' ? :npm : :yarn)
5659
puts "Using package manager: #{@pm.package_manager}"
@@ -64,27 +67,73 @@ def install_vue_cli
6467
end
6568

6669
def run_vue_create?
67-
input = 'y'
70+
pack_input = 'y'
6871
if @pack.exist?
6972
puts 'Detected `package.json`!'
70-
input = @input.gets(
73+
pack_input = @input.gets(
7174
' Do you want `vue create?` to overwrite your package.json',
72-
'ynAk',
73-
a: 'Auto', k: 'Keep',
75+
'yAks',
76+
a: 'Auto', k: 'Keep', s: 'Skip vue create',
77+
)
78+
end
79+
return if pack_input == 's'
80+
81+
gi_input = 'y'
82+
gi = @root.join('.gitignore')
83+
if gi.exist?
84+
puts 'Detected `.gitignore`!'
85+
gi_input = @input.gets(
86+
' Do you want `vue create?` to overwrite your .gitignore',
87+
'yMk',
88+
m: 'Merge', k: 'Keep',
7489
)
7590
end
76-
return if input == 'n'
7791

78-
src_json = JSON.parse(@pack.read) unless input == 'y'
79-
@pm.exec('vue create', '', "-n -m #{@pm.package_manager} .")
92+
# backups
93+
vue_config = @root.join('vue.config.js')
94+
backup_vue_config = vue_config.exist?
95+
src_json = JSON.parse(@pack.read) if pack_input != 'y'
96+
gi_lines = gi.read.split("\n") if gi_input != 'y'
97+
if backup_vue_config
98+
FileUtils.mv(vue_config, "#{vue_config}.backup")
99+
# will be recreated
100+
FileUtils.rm_f(@root.join('vue.rails.js'))
101+
end
102+
pub_dir = @root.join('public')
103+
FileUtils.mv(pub_dir, "#{pub_dir}.backup")
80104

81-
dst_json = JSON.parse(@pack.read) unless input == 'y'
82-
return if input == 'y' || dst_json == src_json
105+
begin
106+
@pm.exec('vue create', '', "-n -m #{@pm.package_manager} .")
107+
ensure
108+
# restore backups
109+
FileUtils.rm_rf(pub_dir) if pub_dir.exist?
110+
FileUtils.mv("#{pub_dir}.backup", pub_dir)
111+
FileUtils.mv("#{vue_config}.backup", vue_config) if backup_vue_config
112+
end
83113

84-
src_json, dst_json = [dst_json, src_json] if input == 'a'
114+
# merge gitignore
115+
if gi_input == 'm'
116+
old_gi_line_count = gi_lines.size
117+
old_gi = Set.new(gi_lines.map(&:strip))
118+
119+
gi.read.split("\n").map(&:strip).find_all(&:present?).each do |ln|
120+
gi_lines << ln unless old_gi.include?(ln)
121+
end
122+
gi.write(gi_lines.map { |ln| "#{ln}\n" }.join('')) if gi_lines.size > old_gi_line_count
123+
elsif gi_input == 'k'
124+
gi.write(gi_lines.join(''))
125+
end
126+
127+
# check packages.json
128+
return if pack_input == 'y'
129+
dst_json = JSON.parse(@pack.read)
130+
return if dst_json == src_json
131+
132+
# merge packages.json
133+
src_json, dst_json = [dst_json, src_json] if pack_input == 'a'
85134
dst_json.deep_merge!(src_json)
86135
@pack.write(JSON.pretty_generate(dst_json))
87-
@pm.install
136+
@pending_install = true
88137
end
89138

90139
def install_dev_deps
@@ -93,15 +142,20 @@ def install_dev_deps
93142
dd = %w[webpack-assets-manifest js-yaml].find_all do |dep|
94143
!dev_deps.key?(dep)
95144
end
96-
@pm.add "-D #{dd.join(' ')}" if dd.any?
145+
if dd.any?
146+
@pm.add("-D #{dd.join(' ')}")
147+
@pending_install = false
148+
end
97149
end
98150

99-
def delete_vue_src?
100-
src = @root.join('src')
101-
return unless src.exist? && src.directory?
151+
def delete_vue_sample?
152+
%w[src dist].each do |fn|
153+
file = @root.join(fn)
154+
next unless file.exist?
102155

103-
puts 'Detected `src` folder (should be generated by vue-cli)'
104-
FileUtils.rm_rf(src.to_s) if @input.gets(' Do you want to delete src folder?') == 'y'
156+
puts "Detected `#{fn}` (should be generated by vue-cli)"
157+
FileUtils.rm_rf(file.to_s) if @input.gets(" Do you want to delete #{file}?") == 'y'
158+
end
105159
end
106160

107161
def copy_demo?
@@ -115,8 +169,8 @@ def copy_demo?
115169
foo = false
116170
bar = false
117171
route_lines.each do |line|
118-
foo ||= line.include? 'vue#foo'
119-
bar ||= line.include? 'vue#bar'
172+
foo ||= line.include?('vue#foo')
173+
bar ||= line.include?('vue#bar')
120174
end
121175
return if foo && bar
122176

@@ -147,7 +201,28 @@ def generate_vue_yml
147201
end
148202

149203
yml = @src_dir.join('vue.yml').read
150-
yml = yml.sub('${PACKAGE_MANAGER}', pm.package_manager.to_s)
204+
yml = yml.sub('${PACKAGE_MANAGER}', @pm.package_manager.to_s)
151205
yml_dest.write(yml)
152206
end
207+
208+
def fix_jest_config!
209+
jest_file = @root.join('jest.config.js')
210+
return unless jest_file.exist?
211+
212+
jest_config = %x`node -e "console.log(JSON.stringify(require('./jest.config.js')))"`
213+
jest_config = JSON.parse(jest_config)
214+
jest_config.delete('moduleNameMapper')
215+
jest = <<~JS
216+
const { jestModuleNameMapper: moduleNameMapper } = require('./vue.rails');
217+
218+
module.exports = #{JSON.pretty_generate(jest_config)}_MODULE_NAME_MAPPER_;
219+
JS
220+
jest_file.write(jest.sub(/\n?}_MODULE_NAME_MAPPER_/, ",\n moduleNameMapper\n}"))
221+
222+
dev_deps = JSON.parse(@pack.read)['devDependencies']
223+
return unless dev_deps['eslint'].present?
224+
@pm.exec('eslint', jest_file.to_s, '--fix')
225+
rescue => e
226+
STDERR.puts e.message
227+
end
153228
end

0 commit comments

Comments
 (0)