Class: ShopifyCli::Project

Inherits:
Object
  • Object
show all
Includes:
SmartProperties
Defined in:
lib/shopify-cli/project.rb

Overview

ShopifyCli::Project captures the current project that the user is working on. This class can be used to fetch and save project environment as well as the project config .shopify-cli.yml.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.current(force_reload: false) ⇒ Object

will get an instance of the project that the user is currently operating on. This is used for access to project resources.

#### Parameters

  • force_reload - whether to force a reload of the project files

#### Returns

  • project - a Project instance if the user is currently in the project.

#### Raises

  • ShopifyCli::Abort - If the cli is not currently in a project directory then this will be raised with a message implying that the user is not in a project directory.

#### Example

project = ShopifyCli::Project.current


36
37
38
# File 'lib/shopify-cli/project.rb', line 36

def current(force_reload: false)
  at(Dir.pwd, force_reload: force_reload)
end

.current_project_typeObject

will fetch the project type of the current project. This is mostly used for internal project type loading, you should not normally need this.

#### Returns

  • type - a symbol of the name of the project type identifier. i.e. [rails, node] This will be nil if the user is not in a current project.

#### Example

type = ShopifyCli::Project.current_project_type


64
65
66
67
# File 'lib/shopify-cli/project.rb', line 64

def current_project_type
  return unless has_current?
  current.config['project_type'].to_sym
end

.has_current?Boolean

will return true if the command line is currently within a project

#### Returns

  • has_current? - boolean, true if there is a current project

Returns:

  • (Boolean)


47
48
49
# File 'lib/shopify-cli/project.rb', line 47

def has_current?
  !directory(Dir.pwd).nil?
end

.project_nameObject



94
95
96
# File 'lib/shopify-cli/project.rb', line 94

def project_name
  File.basename(current.directory)
end

.write(ctx, project_type:, organization_id:, **identifiers) ⇒ Object

writes out the .shopify-cli.yml file. You should use this when creating a project type so that the rest of your project type commands will load in this project, in the future.

#### Parameters

  • ctx - the current running context of your command

  • project_type - a string or symbol of your project type name

  • organization_id - the id of the partner organization that the app is owned by. Used for metrics

  • identifiers - an optional hash of other app identifiers

#### Example

type = ShopifyCli::Project.current_project_type


85
86
87
88
89
90
91
92
# File 'lib/shopify-cli/project.rb', line 85

def write(ctx, project_type:, organization_id:, **identifiers)
  require 'yaml' # takes 20ms, so deferred as late as possible.
  content = Hash[{ project_type: project_type, organization_id: organization_id.to_i }
    .merge(identifiers)
    .collect { |k, v| [k.to_s, v] }]

  ctx.write('.shopify-cli.yml', YAML.dump(content))
end

Instance Method Details

#configObject

will read, parse and return the .shopify-cli.yml for the project

#### Returns

  • config - A hash of configuration

#### Raises

  • ShopifyCli::Abort - If the yml is invalid or poorly formatted

  • ShopifyCli::Abort - If the yml file does not exist

#### Example

ShopifyCli::Project.current.config


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/shopify-cli/project.rb', line 165

def config
  @config ||= begin
    config = load_yaml_file('.shopify-cli.yml')
    unless config.is_a?(Hash)
      raise ShopifyCli::Abort, Context.message('core.yaml.error.not_hash', '.shopify-cli.yml')
    end

    # The app_type key was deprecated in favour of project_type, so replace it
    if config.key?('app_type')
      config['project_type'] = config['app_type']
      config.delete('app_type')
    end

    config
  end
end

#envObject

will read, parse and return the envfile for the project

#### Returns

  • env - An instance of a ShopifyCli::Resources::EnvFile

#### Example

ShopifyCli::Project.current.env


141
142
143
144
145
146
147
# File 'lib/shopify-cli/project.rb', line 141

def env
  @env ||= begin
             Resources::EnvFile.read(directory)
           rescue Errno::ENOENT
             nil
           end
end