# frozen_string_literal: true require "hashdiff" module NeetoCompliance class JsconfigVerifier < Base attr_accessor :diff def initialize @diff = [] end def local_copy "app/javascript/jsconfig.json" end def commons_copy NeetoCompliance::NeetoCommons.path.join "common_files", "app", "javascript", "jsconfig.json" end def local_jsconfig_data @_local_jsconfig_data ||= JSON.parse(File.read(local_copy)) end def required_jsconfig_data @_required_jsconfig_data ||= JSON.parse(File.read(commons_copy)) end def verify_command self.diff = Hashdiff.best_diff(required_jsconfig_data, local_jsconfig_data) removed_symbol = "\-" modified_symbol = "\~" symbol_index = 0 self.diff.any? { |p| /#{removed_symbol}|#{modified_symbol}/.match?(p[symbol_index]) } end def local_file_exists? File.file?(local_copy) end def valid? return false unless local_file_exists? is_modified_or_removed = verify_command !is_modified_or_removed end def autofix_command "cp #{commons_copy} #{local_copy}" end def upstream_copy "https://github.com/bigbinary/neeto-commons-backend/blob/stable/lib/neeto-commons-backend/common_files/app/javascript/jsconfig.json" end def autofix_suggestion base_message = %{ Your jsconfig.json content doesn't match with the expected neeto-commons content. The following is the recommended jsconfig.json content in neeto ecosystem: 1) View #{upstream_copy} 2) Or open #{commons_copy} To fix, start by fully replacing app/javascript/jsconfig.json of your app with above mentioned content, by running the following: #{autofix_command.yellow} } # The following is an example diff: # [["-", "compilerOptions.moduleResolution", "NodeNext"], # ["~", "compilerOptions.module", "ESNext", "ESNex"], # ["+", "compilerOptions.paths.neetou", ["../../node_modules/@bigbinary/neetoui/**"]], # For the above diff, we only need to get the newly added items marked by "+" # and also check whether it's a "path" that has been added. capture_addition_into_paths_key = "(?=.*\+)(?=.*compilerOptions.paths)" new_paths = self.diff.select { |p| p.join =~ /#{capture_addition_into_paths_key}.*/ } if new_paths.empty? return base_message end custom_paths = Hash.new new_paths.each do |path| k = path[1].split(".").last v = path[2] custom_paths[k] = v end message = %{ #{base_message} ================================================== It seems that you've also added some custom paths in your jsconfig. You can cherry pick and append the custom paths to app/javascript/jsconfig.json, if need be, after running above mentioned copy command. Refer the comments in #{upstream_copy} to see how to add custom paths. The following are the custom paths that we have detected in your app/javascript/jsconfig.json: #{JSON.pretty_generate(custom_paths).yellow} } end end end