Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial README #500

Merged
merged 10 commits into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions packages/cli-lib/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
## What is CLI-lib for?

HubSpot's cli-lib aims to provide useful ways of interacting with HubSpot's public APIs.

## Framework-agnostic Integrations

HubSpot's cli-lib can integrate with various task runners and bundlers such as Gulp, Grunt, and Webpack.

Internally, the HubSpot cli-lib is consumed by the HubSpot CLI, the official HubSpot Webpack plugin, and the official HubSpot serverless development runtime.

## Functionality

The full scope of the cli-lib is quite large. It provides the ability to manage authentication, custom objects, the file manager, files within the Design Manager, serverless functions, HubDB, and more.
drewjenkins marked this conversation as resolved.
Show resolved Hide resolved

## Installation

- Install Node.js. The [LTS version](https://nodejs.org/en/) is recommended, however the HubSpot CLI supports any version from 10 and up. Installation details can be found at [nodejs.org](https://nodejs.org/en/)
- Install the following modules from NPM:
- `npm i --global @hubspot/cli`
- `npm i --save @hubspot/cli-lib`

## Usage

The easiest way to authenticate is through the use of a `hubspot.config.yml` file. This can be generated with the HubSpot CLI we installed above. Run `hs init` to generate this file. If you already have a `hubspot.config.yml` file, you can ensure you are authenticated by running the `hs auth` command. Upon doing this, the `hubspot.config.yml` file will be updated with the access token needed to make API calls.

It is possible to do this programmatically through APIs as well, however all of the functionality within cli-lib assumes the authentication credentials are within a `hubspot.config.yml` file.

## Hello World

Let's get started with a simple example. In this example, we are going to get the contents of the root directory in the Design Manager file system. One important note. For any of the cli-lib functionality to work, you must have your `hubspot.config.yml` configured and authenticated for whatever accountId you specify in the code below. See the usage section above for details.

```js
const {
getDirectoryContentsByPath,
} = require('@hubspot/cli-lib/api/fileMapper');

const accountId = YOUR_ACCOUNT_ID;

getDirectoryContentsByPath(accountId, '/').then(response => {
console.log(response);
});
drewjenkins marked this conversation as resolved.
Show resolved Hide resolved
```

## Examples
For more examples, view the `examples` folder

## Contributing

### Requirements

- Installed Git
- Installed node.js >= 10

### Clone

`https://github.com/HubSpot/hubspot-cli`

### Install Dependencies

`npm i`

### Directory

`cd hubspot-cli/packages/cli-lib`

### Submitting Changes

1. Create a fork of `https://github.com/HubSpot/hubspot-cli`
2. Apply your changes to the fork
3. Open a Pull Request on the `https://github.com/HubSpot/hubspot-cli`

---

Licensed under [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0)
1 change: 1 addition & 0 deletions packages/cli-lib/examples/file-upload/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
1 change: 1 addition & 0 deletions packages/cli-lib/examples/file-upload/MyProject/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file is purely for example on how to get a file uploaded to your Account
27 changes: 27 additions & 0 deletions packages/cli-lib/examples/file-upload/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## File Upload Example

** This example is for uploading a file to design-manager. To see an example on how to upload assets to File Manager, check out the filemanager-upload example **

### Overview

This example should you a simple file upload scenario. This would be used to upload a single file. To see how to upload a whole folder, check out the `slack` example.

### Running the Example

1. `npm install`
2. Open index.js, replace each ACCOUNT_ID with the account id of your HubSpot account. Note that in a production environment, data like this should be stored securely as an environment variable.
3. `node index.js`

You should see a success message in your terminal

### Real Life Usage

An example flow would be:

1. You make local changes to MyProject
2. You push the changes to GitHub
3. You use GitHub Actions to run tests on your code
4. If tests fail, abort. No upload happens
5. If all tests pass, have the Github action invoke this script. The LOCAL_PROJECT_PATH would be set to the path it is accessed from on the Github action. The mocked out environment variables we have in index.js would instead be securely stored on Github Actions and referenced through process.env.MY_VARIABLE
6. The folder at LOCAL_PROJECT_PATH is uploaded to your HubSpot DesignManager. Because we specified publish rather than draft, our changes are immediately published and available in your live HubSpot instance.
7. If the upload succeeds, log out a success message to the terminal
20 changes: 20 additions & 0 deletions packages/cli-lib/examples/file-upload/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { Mode } = require('@hubspot/cli-lib');
const { upload } = require('@hubspot/cli-lib/api/fileMapper');

// Mock out some environment variables
const ACCOUNT_ID = undefined; // Should be your HubSpot portal/account ID, as a number
const LOCAL_FILE_PATH = './MyProject/README.md';
const REMOTE_FILE_PATH = '/MyProject/README.md';

(async function() {
try {
// Upload the contents of LOCAL_FILE_PATH to REMOTE_FILE_PATH in Design Manager
await upload(ACCOUNT_ID, LOCAL_FILE_PATH, REMOTE_FILE_PATH, Mode.publish);
` has been deployed to ${ACCOUNT_ID}`;
} catch (e) {
console.error(
`Encountered an error uploading ${LOCAL_FILE_PATH} to your ${ACCOUNT_ID} account\n${e.message}`
);
process.exit(1);
}
})();
Loading