# Constructor.io Connect CLI

[![npm](https://img.shields.io/npm/v/@constructor-io/constructorio-connect-cli)](https://www.npmjs.com/package/@constructor-io/constructorio-connect-cli)
[![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/Constructor-io/constructorio-connect-cli/blob/master/LICENSE)

Constructor.io is an e-commerce first product discovery service that optimizes results using artificial intelligence
(including natural language processing, re-ranking to optimize for business metrics, and end user personalization).

This package is a CLI tool that helps you **build, test and deploy your connector templates**.
By using templates, you can customize your catalog integration to shape your data the way you want it.

> If you're looking for a package to consume our API, please use
> [@constructor-io/constructorio-node](https://github.com/Constructor-io/constructorio-node) instead.
> Alternatively, if you want a JavaScript client for client side (i.e. front end) integrations please use [@constructor-io/constructorio-client-javascript](https://github.com/Constructor-io/constructorio-client-javascript).

## 1. Review the Requirements

Before you begin, note that this package is intended for use with partner connections.
If you don't have a connector running yet, please take a look at
[our documentation](https://docs.constructor.com/docs/integrating-with-constructor-platform-connectors)
to see how you can get started.

Finally, make sure you have your **Index Key** and **API token** available.
If you don't have this yet, please get in touch with our team and we'll set this up.

## 2. Install

You can initialize a new working repo with the following command:

```bash
npx @constructor-io/constructorio-connect-cli init my-repo
```

Where `my-repo` is the name of the folder and repository you want to create.

With the new repository initialized, you can start developing your catalog integration.
Please refer to our [documentation](https://docs.constructor.com/docs/integrating-with-constructor-platform-connectors)
for more information on how to build your templates.

## 3. Commands

<!-- Generated code below. Do not update! -->

<!-- commands -->
* [`constructorio-connect-cli deploy ENV`](#constructorio-connect-cli-deploy-env)
* [`constructorio-connect-cli execute`](#constructorio-connect-cli-execute)
* [`constructorio-connect-cli generate-fixture`](#constructorio-connect-cli-generate-fixture)
* [`constructorio-connect-cli help [COMMAND]`](#constructorio-connect-cli-help-command)
* [`constructorio-connect-cli init NAME`](#constructorio-connect-cli-init-name)
* [`constructorio-connect-cli pull`](#constructorio-connect-cli-pull)
* [`constructorio-connect-cli refresh-connections`](#constructorio-connect-cli-refresh-connections)
* [`constructorio-connect-cli trigger-catalog-sync`](#constructorio-connect-cli-trigger-catalog-sync)
* [`constructorio-connect-cli upgrade`](#constructorio-connect-cli-upgrade)

## `constructorio-connect-cli deploy ENV`

Deploys all templates defined on the `connectrc.js` file to the specified environment, after running tests ('--force' to skip). 

```
USAGE
  $ constructorio-connect-cli deploy ENV [--cloud] [-f]

ARGUMENTS
  ENV  (development|qa|production|demo) The target Constructor environment to deploy to.

FLAGS
  -f, --force  Skip tests before deploying
      --cloud  Use the cloud config (connectrc.js) even if a local config (connectrc.local.js) exists

DESCRIPTION
  Deploys all templates defined on the `connectrc.js` file to the specified environment, after running tests ('--force'
  to skip).

  The script will use the `CONNECT_AUTH_TOKEN` environment variable to authenticate with Constructor.

EXAMPLES
  $ constructorio-connect-cli deploy development

  $ constructorio-connect-cli deploy qa

  $ constructorio-connect-cli deploy production

  $ constructorio-connect-cli deploy production --force
```


## `constructorio-connect-cli execute`

Execute a template against a connection and fixture to see the resulting transformed data. Each value not provided as a flag will be prompted for when the command is executed.

```
USAGE
  $ constructorio-connect-cli execute [--cloud] [--template-path <value>] [--fixture-path <value>]
    [--external-data-path <value>] [--connection-id <value>] [--override-date <value>]

FLAGS
  --cloud                       Use the cloud config (connectrc.js) even if a local config (connectrc.local.js) exists
  --connection-id=<value>       The ID of the connection to execute the template against.
  --external-data-path=<value>  The path to the external data to execute the template against. Must be in the
                                'src/fixtures/external_data' directory.
  --fixture-path=<value>        The path to the fixture to execute the template against. Must be in the 'src/fixtures'
                                directory.
  --override-date=<value>       Date with which to override $now and $millis in the template.
  --template-path=<value>       The path to the template to execute. Must be in the 'src/templates' directory.

DESCRIPTION
  Execute a template against a connection and fixture to see the resulting transformed data. Each value not provided as
  a flag will be prompted for when the command is executed.

EXAMPLES
  $ constructorio-connect-cli execute

  $ constructorio-connect-cli execute --template-path=item/item.jsonata

  $ constructorio-connect-cli execute --template-path=item/item.js

  $ constructorio-connect-cli execute --template-path=item/item.ts

  $ constructorio-connect-cli execute --template-path=variation/variation.jsonata

  $ constructorio-connect-cli execute --template-path=item_group/item_group.jsonata --fixture-path=item_group.json

  $ constructorio-connect-cli execute --template-path=grouping/grouping.jsonata --connection-id=example-connection-id

  $ constructorio-connect-cli execute --template-path=mapping/mapping.jsonata
```


## `constructorio-connect-cli generate-fixture`

This command will fetch one fixture from the server and save it into a specified file. This fixture file will be an example of how the data would be passed to the connector when executing the templates.

```
USAGE
  $ constructorio-connect-cli generate-fixture [--cloud] [--connection <value>] [--type
    item|variation|item_group|external_data|mapping] [--filename <value>] [--item-id <value> | --auto-id |
    --item-group-id <value>] [--variation-id <value>] [--replace-file]

FLAGS
  --auto-id                Use an automatically generated sample ID instead of providing a specific one (only for live
                           fixtures)
  --cloud                  Use the cloud config (connectrc.js) even if a local config (connectrc.local.js) exists
  --connection=<value>     The connection to generate the fixture for
  --filename=<value>       The name of the fixture file to generate
  --item-group-id=<value>  The ID of the item group to generate the fixture for (only applicable when
                           --type=item_group). For Shopify, this is the Collection ID (e.g. 123456789 or
                           gid://shopify/Collection/123456789)
  --item-id=<value>        The ID of the item to generate the fixture for
  --replace-file           Whether to replace the fixture file if it already exists without asking
  --type=<option>          The type of fixture to generate
                           <options: item|variation|item_group|external_data|mapping>
  --variation-id=<value>   The ID of the variation to generate the fixture for (only applicable when --type=variation)

DESCRIPTION
  This command will fetch one fixture from the server and save it into a specified file. This fixture file will be an
  example of how the data would be passed to the connector when executing the templates.
  You are expected to customize this file to match the data you expect to cover in a template execution (e.g. an item
  with stock, a variation with missing data), etc. Note that you can have multiple fixtures of the same type to cover
  different scenarios.
  Finally, if the file already exists you'll be prompted to overwrite it.

EXAMPLES
  $ constructorio-connect-cli generate-fixture

  $ constructorio-connect-cli generate-fixture --connection=my-connection-id --type=item --filename=my-fixture.json

  $ constructorio-connect-cli generate-fixture --connection=my-connection-id --type=item --filename=my-fixture.json --auto-id

  $ constructorio-connect-cli generate-fixture --connection=my-connection-id --type=variation --filename=my-fixture.json

  $ constructorio-connect-cli generate-fixture --connection=my-connection-id --type=variation --item-id=123456789 --variation-id=987654321 --filename=my-fixture.json

  $ constructorio-connect-cli generate-fixture --connection=my-connection-id --type=variation --item-id=123456789 --filename=my-fixture.json

  $ constructorio-connect-cli generate-fixture --connection=my-connection-id --type=variation --filename=my-fixture.json --replace-file

  $ constructorio-connect-cli generate-fixture --connection=my-shopify-connection-id --type=item_group --item-group-id=123456789 --filename=my-fixture.json

  $ constructorio-connect-cli generate-fixture --connection=my-shopify-connection-id --type=item_group --item-group-id=gid://shopify/Collection/123456789 --filename=my-fixture.json

  $ constructorio-connect-cli generate-fixture --connection=my-shopify-connection-id --type=item_group --filename=my-fixture.json --auto-id
```


## `constructorio-connect-cli help [COMMAND]`

Display help for constructorio-connect-cli.

```
USAGE
  $ constructorio-connect-cli help [COMMAND...] [-n]

ARGUMENTS
  [COMMAND...]  Command to show help for.

FLAGS
  -n, --nested-commands  Include all nested commands in the output.

DESCRIPTION
  Display help for constructorio-connect-cli.
```


## `constructorio-connect-cli init NAME`

Bootstrap a new Constructor.io Connect CLI repository.

```
USAGE
  $ constructorio-connect-cli init NAME [--engine jsonata|js|ts]

ARGUMENTS
  NAME  name of the new folder

FLAGS
  --engine=<option>  [default: jsonata] The template engine to use for the new repository.
                     <options: jsonata|js|ts>

DESCRIPTION
  Bootstrap a new Constructor.io Connect CLI repository.

EXAMPLES
  $ constructorio-connect-cli init

  $ constructorio-connect-cli init new-repo-name

  $ constructorio-connect-cli init new-repo-name --engine=ts

  $ constructorio-connect-cli init new-repo-name --engine=js
```


## `constructorio-connect-cli pull`

Pulls the deployed templates from Constructor and overwrites the local files defined in your `connectrc.js`. 

```
USAGE
  $ constructorio-connect-cli pull [--cloud] [--history]

FLAGS
  --cloud    Use the cloud config (connectrc.js) even if a local config (connectrc.local.js) exists
  --history  Choose a connection and one of its past deployments to pull, instead of the templates it currently runs.

DESCRIPTION
  Pulls the deployed templates from Constructor and overwrites the local files defined in your `connectrc.js`.

  By default it pulls the templates your connections currently run. When they run different templates, you will be
  prompted to choose which one to pull.

  With `--history` you choose one of your connections and then one of its past deployments to pull.

EXAMPLES
  $ constructorio-connect-cli pull

  $ constructorio-connect-cli pull --history
```


## `constructorio-connect-cli refresh-connections`

Refresh the connections list

```
USAGE
  $ constructorio-connect-cli refresh-connections [--cloud]

FLAGS
  --cloud  Use the cloud config (connectrc.js) even if a local config (connectrc.local.js) exists

DESCRIPTION
  Refresh the connections list
```


## `constructorio-connect-cli trigger-catalog-sync`

Triggers a catalog sync for selected connection. 

```
USAGE
  $ constructorio-connect-cli trigger-catalog-sync [--cloud]

FLAGS
  --cloud  Use the cloud config (connectrc.js) even if a local config (connectrc.local.js) exists

DESCRIPTION
  Triggers a catalog sync for selected connection.

  The script will use the `CONNECT_AUTH_TOKEN` environment variable to authenticate with Constructor.

EXAMPLES
  $ constructorio-connect-cli
```


## `constructorio-connect-cli upgrade`

Upgrade infrastructure files in an existing Connect CLI repository to match the latest boilerplate.

```
USAGE
  $ constructorio-connect-cli upgrade [--engine jsonata|js|ts]

FLAGS
  --engine=<option>  [default: jsonata] The template engine used in this repository.
                     <options: jsonata|js|ts>

DESCRIPTION
  Upgrade infrastructure files in an existing Connect CLI repository to match the latest boilerplate.

EXAMPLES
  $ constructorio-connect-cli upgrade

  $ constructorio-connect-cli upgrade --engine=ts
```

<!-- commandsstop -->

## Development

Here's some useful npm scripts:

```bash
npm run lint            # run lint on source code and tests
npm run test            # run tests
npm run test:cov        # run tests with coverage report
```

When you use an all-companies Auth Service token, also set `CONNECT_COMPANY_ID` to the company's
Auth Service UUID so the CLI sends the `constructor-company-id` header.
