@@ -4,13 +4,24 @@ namespace :vue do
44 pm = VueCli ::Rails ::NodeEnv . new
55 abort ( 'Cannot find node.js' ) unless pm . node?
66
7- get_input = lambda { |message , list = 'Yn' |
7+ default_hint = { 'y' => 'Yes' , 'n' => 'No' }
8+ get_input = lambda { |message , list = 'Yn' , **hint |
89 list = list . chars
910 default = list . find { |c | c . upcase == c }
1011 list = list . map { |c | c == default ? c : c . downcase } . uniq
1112 valid = "[#{ list . join ( '' ) } ]"
13+
1214 list = list . map ( &:downcase )
13- print "#{ message } #{ valid } "
15+ hint = default_hint . merge ( hint . map { |k , v | [ k . to_s . downcase , v ] } . to_h )
16+ hint = list . map do |c |
17+ h = hint [ c ]
18+ next if h . blank?
19+
20+ "#{ c . upcase } =#{ h } "
21+ end
22+
23+ print "#{ message } (#{ hint . join ( ', ' ) } ) #{ valid } "
24+ default = default . downcase
1425 loop do
1526 r = STDIN . gets . chop . downcase
1627 break default if r == ''
@@ -20,12 +31,17 @@ namespace :vue do
2031 end
2132 }
2233
34+ pwd = FileUtils . pwd
35+ root = ::Rails . root
36+ FileUtils . chdir root
37+
2338 # 1. package manager
2439 yarn = pm . yarn_version
2540 npm = pm . npm_version
2641 if yarn
2742 if npm
28- input = get_input . call ( 'Which package manager to use (Y=Yarn, N=npm)?' )
43+ keys = root . join ( 'package-lock.json' ) . exist? ? 'yN' : 'Yn'
44+ input = get_input . call ( 'Which package manager to use?' , keys , y : 'yarn' , n : 'npm' )
2945 pm . use! ( input == 'n' ? :npm : :yarn )
3046 else
3147 pm . use! ( :yarn )
@@ -43,18 +59,32 @@ namespace :vue do
4359 pm . global_add ( '@vue/cli' )
4460 end
4561
46- src_dir = Pathname . new ( __FILE__ ) . dirname . join ( '..' , 'source' )
47- root = ::Rails . root
48- FileUtils . chdir root
49-
5062 # 2. vue create .
63+ src_dir = Pathname . new ( __FILE__ ) . dirname . join ( '..' , 'source' )
5164 input = 'y'
5265 pack = root . join ( 'package.json' )
5366 if pack . exist?
5467 puts 'Detected `package.json`!'
55- input = get_input . call ( ' Do you want to rerun `vue create?`' , 'yN' )
68+ input = get_input . call (
69+ ' Do you want to rerun `vue create?`' ,
70+ 'ynAk' ,
71+ a : 'Auto' ,
72+ k : 'Keep' ,
73+ )
74+ end
75+
76+ if input != 'n'
77+ src_json = JSON . parse ( pack . read ) unless input == 'y'
78+ pm . exec ( 'vue create' , '' , "-n -m #{ pm . package_manager } ." )
79+
80+ dst_json = JSON . parse ( pack . read ) unless input == 'y'
81+ if input != 'y' && dst_json != src_json
82+ src_json , dst_json = [ dst_json , src_json ] if input == 'a'
83+ dst_json . deep_merge! ( src_json )
84+ pack . write ( JSON . pretty_generate ( dst_json ) )
85+ pm . install
86+ end
5687 end
57- pm . exec ( 'vue create' , '' , "-n -m #{ pm . package_manager } ." ) if input == 'y'
5888
5989 # 3. dev-dependencies
6090 package = JSON . parse ( pack . read )
@@ -76,6 +106,7 @@ namespace :vue do
76106 input = get_input . call ( 'Do you want to copy demo code?' , 'yN' )
77107 FileUtils . cp_r ( src_dir . join ( 'app' ) , root ) if input == 'y'
78108
109+ puts 'Copying configuration files...'
79110 # 6. config files
80111 FileUtils . cp ( src_dir . join ( 'vue.rails.js' ) , "#{ root } /" )
81112 input = 'y'
@@ -96,6 +127,9 @@ namespace :vue do
96127 yml = yml . sub ( '#PACKAGE_MANAGER' , pm . package_manager . to_s )
97128 yml_dest . write ( yml )
98129 end
130+
131+ puts 'vue:create finished!'
132+ FileUtils . chdir pwd
99133 end
100134
101135 desc 'Add pug template support: formats=pug,sass,less,stylus'
0 commit comments