Skip to content

Commit a8332fd

Browse files
committed
better rake task and fixed syntax for ruby 2.2
1 parent db4d51e commit a8332fd

File tree

8 files changed

+122
-56
lines changed

8 files changed

+122
-56
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# VueCli::Rails
22

3-
Get `vue-cli` working on Rails
3+
Get `vue-cli` working on Rails.
4+
5+
Currently, I am still actively developing this gem. It's usable but lacks of documentation. I will update it once the main features are ready and well-tested.
46

57
## Installation
68

@@ -15,18 +17,16 @@ And then execute:
1517
$ bundle install
1618
$ bundle exec rake vue:create
1719

18-
Add those lines to your `config/routes.rb`:
20+
Follow the steps and copy demo codes, than you can add lines below to your `config/routes.rb`:
1921

2022
```ruby
2123
get 'vue/foo' => 'vue#foo'
2224
get 'vue/bar' => 'vue#bar'
2325
```
2426

25-
> Currently `rake vue:create` will overwrite all files, please be careful!
26-
2727
## Usage
2828

29-
This gem is fully depends on `vue-cli`. You can do everything with [`vue.config.js`](https://cli.vuejs.org/config/) just don't break `manifest` plugin which required by `vue_cli-rails`.
29+
This gem is fully depends on `vue-cli`. You can do anything with [`vue.config.js`](https://cli.vuejs.org/config/) just don't break `manifest` plugin which required by `vue_cli-rails`.
3030

3131
When you starting `rails server` with development mode, `vue-cli-service serve` will be running at the same time.
3232

lib/source/vue.config.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
// Please do NOT edit settings required by vue_cli-rails
2-
/* [DO NOT EDIT!] begin */
3-
const WebpackAssetsManifest = require('webpack-assets-manifest');
4-
/* [DO NOT EDIT!] end */
5-
61
// const CompressionWebpackPlugin = require('compression-webpack-plugin');
72
// const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
83

@@ -13,14 +8,15 @@ const {
138
outputDir,
149
devServer,
1510
publicPath,
16-
manifestOutput,
11+
manifest,
1712
isProd,
1813
} = require('./vue.rails');
1914

2015
module.exports = {
2116
outputDir,
2217
publicPath,
2318
devServer,
19+
css,
2420
chainWebpack: (config) => {
2521
config
2622
// clear entry points if there is any
@@ -29,14 +25,8 @@ module.exports = {
2925
.end()
3026
/* [DO NOT EDIT!] begin */
3127
.plugin('manifest')
32-
.use(WebpackAssetsManifest)
33-
.init(Plugin => new Plugin({
34-
integrity: false,
35-
entrypoints: true,
36-
writeToDisk: true,
37-
publicPath: true,
38-
output: manifestOutput,
39-
}))
28+
.use(manifest.plugin)
29+
.init(Plugin => new Plugin(manifest.options))
4030
.end()
4131
/* [DO NOT EDIT!] end */
4232
.plugins
@@ -73,7 +63,6 @@ module.exports = {
7363
*/
7464
}
7565
},
76-
css,
7766
configureWebpack: {
7867
entry,
7968
resolve: {

lib/source/vue.rails.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module.exports = (() => {
44
let settings = {};
55

66
/* eslint-disable global-require,import/no-extraneous-dependencies */
7+
const WebpackAssetsManifest = require('webpack-assets-manifest');
78
try {
89
const yaml = require('js-yaml');
910
const { readFileSync, readdirSync, lstatSync } = require('fs');
@@ -56,8 +57,22 @@ module.exports = (() => {
5657
}
5758
/* eslint-enable global-require,import/no-extraneous-dependencies */
5859

60+
const assets = {};
61+
const manifest = {
62+
plugin: WebpackAssetsManifest,
63+
assets,
64+
options: {
65+
assets,
66+
entrypoints: true,
67+
writeToDisk: true,
68+
publicPath: true,
69+
output: settings.manifestOutput,
70+
},
71+
};
72+
5973
return {
6074
...settings,
75+
manifest,
6176
isProd: settings.env === 'production',
6277
};
6378
})();

lib/source/vue.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
default: &default
22
package_manager: #PACKAGE_MANAGER
3-
manifestOutput: app/assets/vue/manifest.dev.json
3+
manifestOutput: tmp/manifest.dev.json
44
public_output_path: vue_assets
55
alias:
66
'@': app/assets/vue

lib/tasks/vue.rake

Lines changed: 83 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,94 @@
11
namespace :vue do
22
desc 'Run vue-cli create and regenerate configuration'
3-
task :create, [:package_manager] do |_t, args|
3+
task :create do
44
pm = VueCli::Rails::NodeEnv.new
5-
pm.use!(args.package_manager)
6-
root = ::Rails.root
5+
abort('Cannot find node.js') unless pm.node?
76

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}"
1438

15-
# dirs under `app`
1639
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')
2178
end
79+
FileUtils.cp(src_dir.join('vue.config.js'), "#{root}/") if input == 'y'
2280

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
2692
end
2793

2894
desc 'Add pug template support: formats=pug,sass,less,stylus'

lib/vue_cli/rails/helper.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ module Helper
44
def vue_entry(name)
55
@config ||= VueCli::Rails::Configuration.instance
66

7-
entry = @config.manifest_data['entrypoints']&.fetch(name)
7+
entry = (@config.manifest_data['entrypoints'] || {})[name]
88
return nil if entry.blank?
99

1010
assets = []
11-
entry['css']&.each do |css|
11+
(entry['css'] || []).each do |css|
1212
assets << stylesheet_link_tag(css)
1313
end
14-
entry['js']&.each do |js|
14+
(entry['js'] || []).each do |js|
1515
assets << javascript_include_tag(js)
1616
end
1717

lib/vue_cli/rails/node_env.rb

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,24 @@ class NodeEnv
44
NODE_BIN_LIST = %i[node yarn npm npx vue].freeze
55

66
def initialize
7-
h = {}
8-
NODE_BIN_LIST.each do |bin|
9-
h[bin] = get_version_of(bin)
10-
end
11-
@versions = h
7+
@versions = {}
128
yield(self) if block_given?
139
end
1410

1511
NODE_BIN_LIST.each do |bin|
1612
define_method :"#{bin}_version" do
17-
@versions[bin]
13+
get_version_of(bin)
1814
end
1915

2016
define_method :"#{bin}?" do
21-
@versions[bin].present?
17+
get_version_of(bin).present?
2218
end
2319
end
2420

2521
def use!(pm)
26-
@pm = (pm || (yarn? ? 'yarn' : 'npm')).to_sym
27-
unless (@pm == :npm || @pm == :yarn) && self.try(:"#{@pm}?")
28-
raise(VueCli::Rails::Error, "Unknown package manager: #{@pm}")
29-
end
22+
@pm = pm.to_sym
23+
raise(ArgumentError, "Unsupported manager: #{@pm}") unless %i[npm yarn].include?(@pm)
24+
raise(VueCli::Rails::Error, "Not installed: #{@pm}") unless self.try(:"#{@pm}?")
3025
end
3126

3227
def package_manager
@@ -65,10 +60,11 @@ def method_missing(cmd, *args)
6560
private
6661

6762
def get_version_of(bin)
68-
r = `#{bin} --version`.strip.presence
69-
return nil if r.nil?
63+
return @versions[bin] if @versions.key?(bin)
7064

71-
r.start_with?('v') ? r[1..-1] : r
65+
r = `#{bin} --version`.strip.presence
66+
@versions[bin] = r && r.start_with?('v') ? r[1..-1] : r
67+
@versions[bin]
7268
end
7369

7470
def version_ge?(v1, v2)

lib/vue_cli/rails/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module VueCli
22
module Rails
3-
VERSION = "0.1.2"
3+
VERSION = "0.1.3"
44
end
55
end

0 commit comments

Comments
 (0)