lib/souls/cli/sync/model.rb in souls-0.36.4 vs lib/souls/cli/sync/model.rb in souls-0.37.0

- old
+ new

@@ -1,90 +1,25 @@ module Souls module Sync class << self def model - current_dir_name = FileUtils.pwd.to_s.match(%r{/([^/]+)/?$})[1] - permitted_dirs = %w[worker api] - unless permitted_dirs.include?(current_dir_name) - raise(StandardError, "You are at wrong directory!Go to API or Worker Directory!") + cp_dir = %w[db app/models spec/factories] + cp_dir.each do |dir| + cp_and_dl_files(dir: dir) end - - cp_dir = get_models_path(service_name: current_dir_name) - cp_dir.each do |path| - cp_and_dl_files(api_dir: path[:api], worker_dir: path[:worker]) - end end private - def file_diff(paths = []) - paths.map do |path| - stat(path)[:last_update] + def cp_and_dl_files(dir: "db") + worker_paths = Souls.configuration.workers.map { |n| n[:name] } + worker_paths.each do |path| + cp_path = "./apps/api/#{dir}" + old_path = "./apps/#{path}/#{dir}" + FileUtils.rm_rf(old_path) if Dir.exist?(old_path) + FileUtils.mkdir(old_path) unless Dir.exist?(old_path) + system("cp -r #{cp_path}/* #{old_path}", chdir: Souls.get_mother_path) end - end - - def stat(path) - s = File::Stat.new(path) - last_update = s.mtime.to_s - last_status_change = s.ctime.to_s - last_access = s.atime.to_s - { - last_update: last_update, - last_status_change: last_status_change, - last_access: last_access - } - end - - def cp_and_dl_files(api_dir: "", worker_dir: "") - if Dir["#{worker_dir}/*.rb"].blank? - - api_latest_date = 1 - worker_latest_date = 0 - else - api_file_data = file_diff(Dir["#{api_dir}/*.rb"]) - worker_file_data = file_diff(Dir["#{worker_dir}/*.rb"]) - # rubocop:disable Style/DateTime - api_latest_date = DateTime.parse(api_file_data.max) - worker_latest_date = DateTime.parse(worker_file_data.max) - # rubocop:enable Style/DateTime - end - - if api_latest_date < worker_latest_date - FileUtils.rm_rf(api_dir) if Dir.exist?(api_dir) - FileUtils.mkdir(api_dir) unless Dir.exist?(api_dir) - system("cp -r #{worker_dir}/* #{api_dir}") - else - FileUtils.rm_rf(worker_dir) if Dir.exist?(worker_dir) - FileUtils.mkdir(worker_dir) unless Dir.exist?(worker_dir) - system("cp -r #{api_dir}/* #{worker_dir}") - end - rescue StandardError => e - puts(Paint[e, :red]) - end - - def get_models_path(service_name: "api") - case service_name - when "api" - api_path = "." - worker_path = "../worker" - when "worker" - api_path = "../api" - worker_path = "." - end - [ - { - api: "#{api_path}/db", - worker: "#{worker_path}/db" - }, - { - api: "#{api_path}/app/models", - worker: "#{worker_path}/app/models" - }, - { - api: "#{api_path}/spec/factories", - worker: "#{worker_path}/spec/factories" - } - ] end end end end