@@ -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
153228end
0 commit comments