Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

class ShopifyCli::Project

Kevin O'Sullivan edited this page Jun 28, 2021 · 8 revisions

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 Methods

current

current(force_reload: false) 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
see source

# File lib/shopify-cli/project.rb, line 36
def current(force_reload: false)
  clear if force_reload
  at(Dir.pwd)
end

has_current?

has_current?() will return true if the command line is currently within a project

Returns

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

# File lib/shopify-cli/project.rb, line 48
def has_current?
  !directory(Dir.pwd).nil?
end

current_project_type

current_project_type() 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
see source

# File lib/shopify-cli/project.rb, line 65
def current_project_type
  return unless has_current?
  current.config["project_type"].to_sym
end

write

write(ctx, project_type:, organization_id:, **identifiers) 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
see source

# File lib/shopify-cli/project.rb, line 86
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] }]
  content["shopify_organization"] = true if Shopifolk.acting_as_shopify_organization?

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

project_name

project_name()

see source

# File lib/shopify-cli/project.rb, line 97
def project_name
  File.basename(current.directory)
end

clear

clear()

see source

# File lib/shopify-cli/project.rb, line 101
def clear
  @at = nil
  @dir = nil
end

Instance Methods

env

env() will read, parse and return the envfile for the project

Returns

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

Example

ShopifyCli::Project.current.env
see source

# File lib/shopify-cli/project.rb, line 145
def env
  @env ||= begin
             Resources::EnvFile.read(directory)
           rescue Errno::ENOENT
             nil
           end
end

config

config() 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
see source

# File lib/shopify-cli/project.rb, line 169
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

Includes

  • SmartProperties
Clone this wiki locally