Class: ImportJS::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/import_js/configuration.rb

Overview

Class that initializes configuration from a .importjs.json file

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



17
18
19
# File 'lib/import_js/configuration.rb', line 17

def initialize
  @config = DEFAULT_CONFIG.merge(load_config)
end

Instance Method Details

#get(key) ⇒ Object

Returns a configuration value.

Returns:

  • (Object)

    a configuration value



27
28
29
# File 'lib/import_js/configuration.rb', line 27

def get(key)
  @config[key]
end

#package_dependenciesArray<String>

Returns:

  • (Array<String>)


60
61
62
63
64
65
66
67
68
69
70
# File 'lib/import_js/configuration.rb', line 60

def package_dependencies
  return [] unless File.exist?('package.json')

  package = JSON.parse(File.read('package.json'))
  dependencies = package['dependencies'] ?
    package['dependencies'].keys : []
  peer_dependencies = package['peerDependencies'] ?
    package['peerDependencies'].keys : []

  dependencies.concat(peer_dependencies)
end

#refreshObject



21
22
23
24
# File 'lib/import_js/configuration.rb', line 21

def refresh
  return if @config_time == config_file_last_modified
  @config = DEFAULT_CONFIG.merge(load_config)
end

#resolve_alias(variable_name, path_to_current_file) ⇒ ImportJS::JSModule?

Parameters:

  • variable_name (String)
  • path_to_current_file (String?)

Returns:



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/import_js/configuration.rb', line 34

def resolve_alias(variable_name, path_to_current_file)
  path = @config['aliases'][variable_name]
  return resolve_destructured_alias(variable_name) unless path

  path = path['path'] if path.is_a? Hash

  if path_to_current_file && !path_to_current_file.empty?
    path = path.sub(/\{filename\}/,
                    File.basename(path_to_current_file, '.*'))
  end
  ImportJS::JSModule.new(nil, path, [])
end

#resolve_destructured_alias(variable_name) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/import_js/configuration.rb', line 47

def resolve_destructured_alias(variable_name)
  @config['aliases'].each do |_, path|
    next if path.is_a? String
    if (path['destructure'] || []).include?(variable_name)
      js_module = ImportJS::JSModule.new(nil, path['path'], [])
      js_module.is_destructured = true
      return js_module
    end
  end
  nil
end