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

Core APIs

Tim Anema edited this page Apr 16, 2020 · 21 revisions

We provide a base set of core APIs to help enable you to not have to write as much code and so that every developer can access resources in a consistent way.

Platform Support

Platform APIs

Platform Support

ShopifyCli::Context

Context is a very special object in the codebase. It is passed throughout most code and is automatically set as @ctx on each command that is being run. The context can be used to mostly interact with the operating system and keep the context of the current working directory.

There are 3 domains that the context holds

  • Console output and formatting
  • File operations, writing, removing, making directories
  • System calls to other commands in the users system.

Output

  • ctx.puts(message): output a formatted message to the console. See CLI::UI::Formatter::SGR_MAP+ for available formatting options
  • ctx.abort(message): outputs a formatted message and then throws an abort exception making the command exit immediately.
  • ctx.debug(message): outputs a formatted message but only when a DEBUG env var exists.

FS Operations

  • ctx.write(filename, content): Create or overwrite a file at path filename, with the contents of contents
  • ctx.rename(filename): Same as OS mv
  • ctx.rm(filename): Same as OS rm
  • ctx.rm_r(dirname): Same as OS rm -r
  • ctx.mkdir_p(dirname): Same as OS mkdir -p meaning it will make all directories recursively.

System

  • ctx.os: returns :mac or :linux
  • ctx.mac?: returns true if running on mac.
  • ctx.linux?: returns true if running on linux
  • ctx.system?: returns true if you have not used load-dev
  • ctx.development?: returns true if you used load-dev
  • ctx.testing?: returns true if the tests are being run
  • ctx.ci?: returns true if tests are being run on the CI
  • ctx.uname: returns the architecture of the current platform
  • ctx.system(*command): run a command and only capture the status
  • ctx.capture2(*command): run a command and return [STDOUT, Status]
  • ctx.capture2e(*command): run a command and return [STDOUT+STDERR, Status]
  • ctx.capture3(*command): run a command and return [STDOUT, STDERR, Status]

ShopifyCli::Project

The ShopifyCli::Project helps interacting with the current project, if the user is currently in a directory where a project is.

There are 3 main things you may want from a project. Project.current.env will return the current projects .env file values. Project.current.config will return the values from the .shopify-app-cli.yml file. The .env file is used for values that are necessary for the project to run and the .shopify-app-cli.yml file is for values that the cli needs to understand the current project.

Platform APIs

ShopifyCli::AdminApi

Auto-authenticated graphql access to a stores admin API. This requires a shop for authentication so either the shop param is required or an env-file with the shop variable defined.

ShopifyCli::AdminApi.query(ctx, query_name, api_version: nil, shop: nil, **variables)
  • ctx: The running command context
  • query_name: the name of a graphql file in lib/graphql to be loaded for the query
  • api_version: To be supplied if you want to specify the version, otherwise unstable will be used.
  • shop: can be provided if you are not using a shop from an env file
  • variables: are the variables that will be supplied to the query or mutation.

ShopifyCli::DB

ShopifyCli::Git

ShopifyCli::Heroku

ShopifyCli::PartnersApi

Auto-authenticated graphql access to the partner dashboard CLI schema. This will authenticate the user with identity and then make authenticated calls to the dashboard.

Note To use a local instance of the Partners Dashboard you can preface your commands with SHOPIFY_APP_CLI_LOCAL_PARTNERS. For example SHOPIFY_APP_CLI_LOCAL_PARTNERS=1 shopify create

ShopifyCli::PartnersApi.query(ctx, query_name, **variables)
  • ctx: The running command context
  • query_name: the name of a graphql file in lib/graphql to be loaded for the query
  • variables: are the variables that will be supplied to the query or mutation.

ShopifyCli::ProcessSupervision

Process supervision is for running long running processes concurrently in a way that we can monitor and clean up.

ShopifyCli::ProcessSupervision.for_ident(identifier)
  • identifier: a symbol or string to refer to this process by.
  • return: a process that can be checked for status and stopped if it is currently running. If it is not running then this method returns nil
ShopifyCli::ProcessSupervision.start(identifier, command)
  • identifier: a symbol or string to refer to this process by.
  • command: string representation of the command being executed
  • return: a process that can be checked for status and stopped if it is currently running. If it is not running then this method returns nil

example

process = ShopifyCli::ProcessSupervision.start(:ngrok, "ngrok http 8080")
ShopifyCli::ProcessSupervision.stop(identifier)
  • identifier: a symbol or string that refers to a running process.
  • return: true if the process was stopped or not running. false if it could not be stopped.
ShopifyCli::ProcessSupervision.running?(identifier)
  • identifier: a symbol or string that refers to a running process.
  • return: true if the process is running and false otherwise.

ShopifyCli::Tunnel

Clone this wiki locally