Last active
July 6, 2021 22:15
-
-
Save arkhamRejek/5cfafd6a8e39b8a3aa96d4651389ee01 to your computer and use it in GitHub Desktop.
Rails task for running yarn commands: add, remove, run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FRONT_END_PATH = "client" | |
namespace :yarn do | |
desc "install packages using rails instead of having to go into the client folder" | |
args = ARGV.drop(1) | |
task :add do | |
dev = false | |
next no_args_passed if args.length == 0 | |
if args.length == 1 || args.length >= 2 && args.include?("dev") | |
if args.include?("dev") | |
dev = true | |
args.delete_at(args.index("dev")) | |
end | |
next install_packages(args.join(" "), dev) | |
end | |
end | |
task :remove do | |
next no_args_passed(true) if args.length == 0 | |
next remove_packages(args.join(" ")) | |
end | |
task :run do | |
next puts "Please specify the script you want to run" if args.length == 0 | |
next puts "Too many arguments, please specify only 1 script" if args.length > 1 | |
next run_script(args.join(" ")) | |
end | |
end | |
def no_args_passed(remove = false) | |
puts "\nError: No arguments passed" | |
if !remove | |
puts "\n--sample:\trails yarn:add yarn_package dev\n\n" | |
puts "dev:\t if dev is not passed, it will install as a regular dependency\n\n" | |
else | |
puts "\n--sample:\trails yarn:remove yarn_package\n\n" | |
end | |
end | |
def install_packages(packages, save_dev) | |
puts "running yarn add #{packages} #{"--dev" if save_dev}" | |
exec "cd #{FRONT_END_PATH} && yarn add #{packages} #{"--dev" if save_dev}" | |
end | |
def remove_packages(packages) | |
puts "running yarn remove #{packages}" | |
exec "cd #{FRONT_END_PATH} && yarn remove #{packages}" | |
end | |
def run_script(script) | |
puts "running yarn #{script}" | |
exec "cd #{FRONT_END_PATH} && yarn #{script}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment