|
1 | 1 | namespace :vue do |
2 | 2 | desc 'Run vue-cli create and regenerate configuration' |
3 | | - task :create, [:package_manager] do |_t, args| |
| 3 | + task :create do |
4 | 4 | pm = VueCli::Rails::NodeEnv.new |
5 | | - pm.use!(args.package_manager) |
6 | | - root = ::Rails.root |
| 5 | + abort('Cannot find node.js') unless pm.node? |
7 | 6 |
|
8 | | - # generate config/vue.yml |
9 | | - FileUtils.chdir root |
10 | | - # `vue create .` and dependencies |
11 | | - pm.exec('vue create', '', "-n -m #{pm.package_manager} .") |
12 | | - pm.add '-D webpack-assets-manifest js-yaml' |
13 | | - FileUtils.rm_rf root.join('src') |
| 7 | + get_input = ->(message, list = 'Yn') { |
| 8 | + list = list.chars |
| 9 | + default = list.find { |c| c.upcase == c } |
| 10 | + list = list.map { |c| c == default ? c : c.downcase }.uniq |
| 11 | + valid = "[#{list.join('')}]" |
| 12 | + list = list.map(&:downcase) |
| 13 | + print "#{message} #{valid}" |
| 14 | + loop do |
| 15 | + r = STDIN.gets.chop.downcase |
| 16 | + break default if r == '' |
| 17 | + break r if list.include?(r) |
| 18 | + print " [INVALID!] Please retry: #{valid}:" |
| 19 | + end |
| 20 | + } |
| 21 | + |
| 22 | + # 1. package manager |
| 23 | + yarn = pm.yarn_version |
| 24 | + npm = pm.npm_version |
| 25 | + if yarn |
| 26 | + if npm |
| 27 | + input = get_input.call('Which package manager to use (Y=Yarn, N=npm)?') |
| 28 | + pm.use!(input == 'n' ? :npm : :yarn) |
| 29 | + else |
| 30 | + pm.use!(:yarn) |
| 31 | + end |
| 32 | + elsif npm |
| 33 | + pm.use!(:npm) |
| 34 | + else |
| 35 | + abort('Cannot find npm or yarn') |
| 36 | + end |
| 37 | + puts "Using package manager: #{pm.package_manager}" |
14 | 38 |
|
15 | | - # dirs under `app` |
16 | 39 | src_dir = Pathname.new(__FILE__).dirname.join('..', 'source') |
17 | | - FileUtils.cp_r(src_dir.join('app'), root) |
18 | | - binding.pry |
19 | | - Dir[src_dir.join('vue.*.js')].each do |fn| |
20 | | - FileUtils.cp(fn, "#{root}/") |
| 40 | + root = ::Rails.root |
| 41 | + FileUtils.chdir root |
| 42 | + |
| 43 | + # 2. vue create . |
| 44 | + input = 'y' |
| 45 | + pack = root.join('package.json') |
| 46 | + if pack.exist? |
| 47 | + puts 'Detected `package.json`!' |
| 48 | + input = get_input.call(' Do you want to rerun `vue create?`', 'yN') |
| 49 | + end |
| 50 | + pm.exec('vue create', '', "-n -m #{pm.package_manager} .") if input == 'y' |
| 51 | + |
| 52 | + # 3. dev-dependencies |
| 53 | + package = JSON.parse(pack.read) |
| 54 | + dev_deps = package['devDependencies'] |
| 55 | + dd = %w[webpack-assets-manifest js-yaml].find_all do |dep| |
| 56 | + !dev_deps.key?(dep) |
| 57 | + end |
| 58 | + pm.add "-D #{dd.join(' ')}" if dd.any? |
| 59 | + |
| 60 | + # 4. remove `src` folder |
| 61 | + src = root.join('src') |
| 62 | + if src.exist? && src.directory? |
| 63 | + puts 'Detected `src` folder (should be generated by vue-cli)' |
| 64 | + input = get_input.call(' Do you want to delete src folder?') |
| 65 | + FileUtils.rm_rf root.join('src') if input == 'y' |
| 66 | + end |
| 67 | + |
| 68 | + # 5. copy sample codes |
| 69 | + input = get_input.call('Do you want to copy demo code?', 'yN') |
| 70 | + FileUtils.cp_r(src_dir.join('app'), root) if input == 'y' |
| 71 | + |
| 72 | + # 6. config files |
| 73 | + FileUtils.cp(src_dir.join('vue.rails.js'), "#{root}/") |
| 74 | + input = 'y' |
| 75 | + if root.join('vue.config.js').exist? |
| 76 | + puts 'Detected `vue.config.js`!' |
| 77 | + input = get_input.call(' Do you want to overwrite vue.config.js?', 'yN') |
21 | 78 | end |
| 79 | + FileUtils.cp(src_dir.join('vue.config.js'), "#{root}/") if input == 'y' |
22 | 80 |
|
23 | | - yml = src_dir.join('vue.yml').read |
24 | | - yml = yml.sub('#PACKAGE_MANAGER', pm.package_manager.to_s) |
25 | | - root.join('config', 'vue.yml').write(yml) |
| 81 | + # 7. generate config/vue.yml |
| 82 | + yml_dest = root.join('config', 'vue.yml') |
| 83 | + if yml_dest.exist? |
| 84 | + puts 'Detected `config/vue.yml`!' |
| 85 | + input = get_input.call(' Do you want to overwrite config/vue.yml?') |
| 86 | + end |
| 87 | + if input == 'y' |
| 88 | + yml = src_dir.join('vue.yml').read |
| 89 | + yml = yml.sub('#PACKAGE_MANAGER', pm.package_manager.to_s) |
| 90 | + yml_dest.write(yml) |
| 91 | + end |
26 | 92 | end |
27 | 93 |
|
28 | 94 | desc 'Add pug template support: formats=pug,sass,less,stylus' |
|
0 commit comments