Skip to content

Instantly share code, notes, and snippets.

@TikiTDO
Created August 15, 2017 04:22
Show Gist options
  • Save TikiTDO/dfe73ff57089c38c1928194c426f9640 to your computer and use it in GitHub Desktop.
Save TikiTDO/dfe73ff57089c38c1928194c426f9640 to your computer and use it in GitHub Desktop.
# Fix imports to point to correct locations
(base = "/home/tiki/karma-development/submodules/") && nil
(core = Dir["#{base}/core-react-module/src/**/*.js"].map {|f| f.gsub("#{base}/core-react-module/src/", "")}) && nil
(worker = Dir["#{base}/worker-react-app/src/**/*.js"].map {|f| f.gsub("#{base}/worker-react-app/src/", "")}) && nil
(path_to_module = (core + worker).map {|f| module_name = Pathname.new(f).basename.to_s.gsub(/(.web)?.js$/, ""); local_path = f.gsub(/(.web)?.js$/, ""); [local_path, module_name]}.to_h) && nil
(module_to_path = (core + worker).map {|f| module_name = Pathname.new(f).basename.to_s.gsub(/(.web)?.js$/, ""); local_path = f.gsub(/(.web)?.js$/, ""); [module_name, local_path]}.to_h) && nil
groups = {}
(group_to_module = (core + worker).map {|f| module_name = Pathname.new(f).basename.to_s.gsub(/(.web)?.js$/, ""); group = Pathname.new(f).dirname.to_s; groups[group] ||= []; groups[group].push(module_name)}) && nil
Dir["**/*.js"].each do |file|
content = IO.read(file).gsub(/(?<full>import\s*(?<all_imports>(?<default>\w+)?,?\s*({(?<optional>(\s*\w+,?)+\s*)})?)\s*from\s*(?<path>[-"\w\/]+))/m) do |match|
full = $~[:full]
default_import = $~[:default]
optional_import = $~[:optional]
all_imports = $~[:all_imports]
path = $~[:path].gsub('"', "")
# Only process import paths that are within the app
next full if !path[%r{^(components|domains|navigators|sagas|services|state|ui)}]
# Do not process relative imports
next full if path[%r{^\./}]
# Do not process default imports from top level index files
next full if !optional_import && !path[/\//]
# Special cases for recapitalized core modules
if path == "services"
if optional_import[/log/]
next %Q{import log from "services/Logger"}
end
elsif path[%r{services/testing}]
next full.gsub(%r{services/testing}, "services/Testing")
elsif path[%r{services/validation}]
next full.gsub(%r{services/validation}, "services/Validation")
elsif path[%r{services/iso639}]
next full.gsub(%r{services/iso639}, "services/Languages")
elsif path[%r{services/location}]
next full.gsub(%r{services/location}, "services/Location")
elsif path[%r{services/action-service}]
next full.gsub(%r{services/action-service}, "services/ActionService")
elsif path[%r{services/units}]
next full.gsub(%r{services/action-service}, "services/Units")
elsif path[%r{navigators/Routes}]
next full
end
pathname = Pathname.new(path)
basename = pathname.basename.to_s
if path_to_module[path]
# This import already points to a valid path, so just keep it as is
next full
elsif module_to_path[basename]
# This module has changed locations, but is otherwise still valid, so just update the path, but don't break up the module
output = %Q{import #{all_imports} from "#{module_to_path[basename]}"}
output
else
# All the modules in these imports need to be expanded, and imported individually
imports = []
imports.push(default_import) if default_import
imports += optional_import.split(",").map(&:strip).reject(&:empty?) if optional_import
new_imports = imports.map do |component|
if module_to_path[component]
%Q{import #{component} from "#{module_to_path[component]}"}
else
puts "In: #{file}: COULD NOT IMPORT #{component} FROM #{path}"
"COULD NOT IMPORT #{component} FROM #{path}"
end
end
new_imports.join("\n")
end
end
IO.write(file, content)
end && nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment