namespace :vue do desc 'Run vue-cli create and regenerate configuration' task :create do pm = VueCli::Rails::NodeEnv.new abort('Cannot find node.js') unless pm.node? get_input = ->(message, list = 'Yn') { list = list.chars default = list.find { |c| c.upcase == c } list = list.map { |c| c == default ? c : c.downcase }.uniq valid = "[#{list.join('')}]" list = list.map(&:downcase) print "#{message} #{valid}" loop do r = STDIN.gets.chop.downcase break default if r == '' break r if list.include?(r) print " [INVALID!] Please retry: #{valid}:" end } # 1. package manager yarn = pm.yarn_version npm = pm.npm_version if yarn if npm input = get_input.call('Which package manager to use (Y=Yarn, N=npm)?') pm.use!(input == 'n' ? :npm : :yarn) else pm.use!(:yarn) end elsif npm pm.use!(:npm) else abort('Cannot find npm or yarn') end puts "Using package manager: #{pm.package_manager}" # install vue-cli unless pm.vue? puts "@vue/cli haven't been installed" pm.global_add('@vue/cli') end src_dir = Pathname.new(__FILE__).dirname.join('..', 'source') root = ::Rails.root FileUtils.chdir root # 2. vue create . input = 'y' pack = root.join('package.json') if pack.exist? puts 'Detected `package.json`!' input = get_input.call(' Do you want to rerun `vue create?`', 'yN') end pm.exec('vue create', '', "-n -m #{pm.package_manager} .") if input == 'y' # 3. dev-dependencies package = JSON.parse(pack.read) dev_deps = package['devDependencies'] dd = %w[webpack-assets-manifest js-yaml].find_all do |dep| !dev_deps.key?(dep) end pm.add "-D #{dd.join(' ')}" if dd.any? # 4. remove `src` folder src = root.join('src') if src.exist? && src.directory? puts 'Detected `src` folder (should be generated by vue-cli)' input = get_input.call(' Do you want to delete src folder?') FileUtils.rm_rf src if input == 'y' end # 5. copy sample codes input = get_input.call('Do you want to copy demo code?', 'yN') FileUtils.cp_r(src_dir.join('app'), root) if input == 'y' # 6. config files FileUtils.cp(src_dir.join('vue.rails.js'), "#{root}/") input = 'y' if root.join('vue.config.js').exist? puts 'Detected `vue.config.js`!' input = get_input.call(' Do you want to overwrite vue.config.js?', 'yN') end FileUtils.cp(src_dir.join('vue.config.js'), "#{root}/") if input == 'y' # 7. generate config/vue.yml yml_dest = root.join('config', 'vue.yml') if yml_dest.exist? puts 'Detected `config/vue.yml`!' input = get_input.call(' Do you want to overwrite config/vue.yml?') end if input == 'y' yml = src_dir.join('vue.yml').read yml = yml.sub('#PACKAGE_MANAGER', pm.package_manager.to_s) yml_dest.write(yml) end end desc 'Add pug template support: formats=pug,sass,less,stylus' task :support, [:formats] do |_t, args| pkgs = [] args.formats.split(/\W/).each do |fmt| pkgs += case fmt when 'pug' %w[pug-plain-loader pug] when 'sass', 'scss' %w[sass-loader node-sass] when 'less' %w[less-loader less] when 'stylus' %w[stylus-loader stylus] else [] end end throw(StandardError, '') if pkgs.empty? pm = VueCli::Rails::Configuration.instance.node_env pm.add "-D #{pkgs.join(' ')}" end desc 'Dump config/vue.yml to_json' task :json_config => :environment do config = VueCli::Rails::Configuration.new puts config.to_json end desc 'Bundle assets for production' task :compile => :environment do pm = VueCli::Rails::Configuration.instance.node_env pm.exec('vue-cli-service build', env: { 'RAILS_ENV' => 'production' }) end end