Repository URL to install this package:
|
Version:
1.8.0 ▾
|
# frozen_string_literal: true
require 'rake/tasklib'
module Faculty
module Rake
# Functions for running common npm tasks using a docker container
class NodeTasks < ::Rake::TaskLib
include Helpers
TASKS = %w[npm npm_install vue vue_production vue_watch webpack webpack_production webpack_watch].freeze
def initialize(node_version: 14, vue_file: nil, vue_files: [])
super()
@node_version = node_version
@vue_files = vue_files
@vue_files = [vue_file] if vue_file
TASKS.each do |task|
send task
end
end
def node_command(command, name: nil)
flags = [
'--rm',
'-i',
"-v #{Dir.pwd}:/app",
'-w /app',
'--env HOME=./.node',
"--user #{Process.uid}:#{Process.gid}"
]
flags.push "--name #{name}" unless name.nil?
"docker run #{flags.join ' '} node:#{@node_version}-alpine #{command}"
end
def node_exec(command)
system node_command(command)
end
# This is a little bit hacky - rake would normally accept parameters individually and assume each is a separate
# task, but we can capture them and pass them all to `npm`. We then have to `exit 0` otherwise rake will
# try to execute the parameters as tasks.
def npm
desc 'Run npm commands'
task :npm do
node_exec "npm #{ARGV[1..-1].join ' '}"
exit 0
end
end
def npm_install
namespace :npm do
desc 'Run npm install'
task :install do
node_exec 'npm install'
end
end
end
def vue_build(file, mode: 'development', watch: false)
"npx vue-cli-service build --no-clean --mode #{mode} --target lib --formats umd-min #{watch ? '--watch' : ''}" \
" #{file}"
end
def vue
desc 'Build your vue component(s)'
task :vue do
@vue_files.each { |file| node_exec vue_build(file) }
end
end
def vue_production
namespace :vue do
desc 'Build your vue component(s) in production mode'
task :prod do
@vue_files.each { |file| node_exec vue_build(file, mode: 'production') }
end
end
end
def vue_watch
namespace :vue do
desc 'Build and watch a vue component'
task :watch, [:file] do |_, args|
pid = spawn node_command(vue_build(args[:file], watch: true), name: 'vue-watch')
Process.wait pid
ensure
puts bold 'Stopping container (this may take a moment...)'
system 'docker stop vue-watch'
end
end
end
def webpack
desc 'npx webpack'
task :webpack do
node_exec 'npx webpack'
end
end
def webpack_production
namespace :webpack do
desc 'npx webpack --config webpack.prod.js'
task :production do
node_exec 'npx webpack --config webpack.prod.js'
end
end
end
def webpack_watch
namespace :webpack do
desc 'npx webpack --watch'
task :watch do
pid = spawn node_command('npx webpack --watch', name: 'webpack-watch')
Process.wait pid
ensure
puts bold 'Stopping container (this may take a moment...)'
system 'docker stop webpack-watch'
end
end
end
end
end
end