@@ -4,13 +4,24 @@ namespace :vue do
4
4
pm = VueCli ::Rails ::NodeEnv . new
5
5
abort ( 'Cannot find node.js' ) unless pm . node?
6
6
7
- get_input = lambda { |message , list = 'Yn' |
7
+ default_hint = { 'y' => 'Yes' , 'n' => 'No' }
8
+ get_input = lambda { |message , list = 'Yn' , **hint |
8
9
list = list . chars
9
10
default = list . find { |c | c . upcase == c }
10
11
list = list . map { |c | c == default ? c : c . downcase } . uniq
11
12
valid = "[#{ list . join ( '' ) } ]"
13
+
12
14
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
14
25
loop do
15
26
r = STDIN . gets . chop . downcase
16
27
break default if r == ''
@@ -20,12 +31,17 @@ namespace :vue do
20
31
end
21
32
}
22
33
34
+ pwd = FileUtils . pwd
35
+ root = ::Rails . root
36
+ FileUtils . chdir root
37
+
23
38
# 1. package manager
24
39
yarn = pm . yarn_version
25
40
npm = pm . npm_version
26
41
if yarn
27
42
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' )
29
45
pm . use! ( input == 'n' ? :npm : :yarn )
30
46
else
31
47
pm . use! ( :yarn )
@@ -43,18 +59,32 @@ namespace :vue do
43
59
pm . global_add ( '@vue/cli' )
44
60
end
45
61
46
- src_dir = Pathname . new ( __FILE__ ) . dirname . join ( '..' , 'source' )
47
- root = ::Rails . root
48
- FileUtils . chdir root
49
-
50
62
# 2. vue create .
63
+ src_dir = Pathname . new ( __FILE__ ) . dirname . join ( '..' , 'source' )
51
64
input = 'y'
52
65
pack = root . join ( 'package.json' )
53
66
if pack . exist?
54
67
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
56
87
end
57
- pm . exec ( 'vue create' , '' , "-n -m #{ pm . package_manager } ." ) if input == 'y'
58
88
59
89
# 3. dev-dependencies
60
90
package = JSON . parse ( pack . read )
@@ -76,6 +106,7 @@ namespace :vue do
76
106
input = get_input . call ( 'Do you want to copy demo code?' , 'yN' )
77
107
FileUtils . cp_r ( src_dir . join ( 'app' ) , root ) if input == 'y'
78
108
109
+ puts 'Copying configuration files...'
79
110
# 6. config files
80
111
FileUtils . cp ( src_dir . join ( 'vue.rails.js' ) , "#{ root } /" )
81
112
input = 'y'
@@ -96,6 +127,9 @@ namespace :vue do
96
127
yml = yml . sub ( '#PACKAGE_MANAGER' , pm . package_manager . to_s )
97
128
yml_dest . write ( yml )
98
129
end
130
+
131
+ puts 'vue:create finished!'
132
+ FileUtils . chdir pwd
99
133
end
100
134
101
135
desc 'Add pug template support: formats=pug,sass,less,stylus'
0 commit comments