Skip to content

Commit

Permalink
Merge pull request #54 from kontent-ai/fix/50-update-management-client
Browse files Browse the repository at this point in the history
Rename the project to environment
  • Loading branch information
winklertomas authored Jul 7, 2023
2 parents 0c2a90f + 3dea8bd commit f2eb293
Show file tree
Hide file tree
Showing 19 changed files with 108 additions and 106 deletions.
29 changes: 15 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,27 @@
[![npm](https://img.shields.io/npm/v/@kontent-ai/cli.svg)](https://www.npmjs.com/package/@kontent-ai/cli)
[![Build](https://github.com/kontent-ai/cli/actions/workflows/test.yml/badge.svg)](https://github.com/kontent-ai/cli/actions/workflows/test.yml)

The Kontent.ai CLI helps you when you need to change content models within your [Kontent.ai](https://kontent.ai/) projects and migrate existing content to match the changes. The CLI provides you with guidance on how to write and run migration scripts.
The Kontent.ai CLI helps you when you need to change content models within your [Kontent.ai](https://kontent.ai/) environments and migrate existing content to match the changes. The CLI provides you with guidance on how to write and run migration scripts.

**_NOTE:_** The Kontent.ai CLI tool supports only Javascript files, so if you write your migrations in Typescript or any other language you have to transpile your code before running.

- [Kontent.ai CLI](#kontent-ai-cli)
- [Kontent.ai CLI](#kontentai-cli)
- [Installation](#installation)
- [🌟 Migration example](#-migration-example)
- [1. Prepare a testing environment](#1-prepare-a-testing-environment)
- [2. PrepareKontent.ai CLI boilerplate](#2-prepare-kontent-cli-boilerplate)
- [2. Prepare Kontent.ai CLI boilerplate](#2-prepare-kontentai-cli-boilerplate)
- [3. Run a migration](#3-run-a-migration)
- [4. Explore existing migrations](#4-explore-existing-migrations)
- [Usage](#usage)
- [Commands](#commands)
- [Custom implementation of reading/saving status of migrations](#custom-implementation-of-readingsaving-status-of-migrations)
- [Debugging](#debugging)
- [The vision](#the-vision)
- [Feedback & Contribution](#feedback--contribution)
- [Feedback \& Contribution](#feedback--contribution)

## Installation

The Kontent.ai CLI requires Node 10+ and npm 6+, and uses the [Kontent.ai Management SDK](https://github.com/kontent-ai/management-sdk-js) to manipulate content in your projects.
The Kontent.ai CLI requires Node 10+ and npm 6+, and uses the [Kontent.ai Management SDK](https://github.com/kontent-ai/management-sdk-js) to manipulate content in your environments.

```sh
npm install -g @kontent-ai/cli
Expand Down Expand Up @@ -50,14 +51,14 @@ cd ./migrations-boilerplate

npm install

# Registers an environment (a pair of two keys, a project ID and API key used to manage the project) for migrations.
kontent environment add --name DEV --api-key <Api key> --project-id <Project ID> (Use the copy of your production project from the first step)
# Registers an environment (a pair of two keys, a environment ID and API key used to manage the environment) for migrations.
kontent environment add --name DEV --api-key <Api key> --environment-id <Environment ID> (Use the copy of your production project from the first step)

# Runs a specific migration.
npm run migrate 01_sample_init_createBlogType
```

Kontent.ai CLI supports only running JavaScript migration files so in case you want to write in TypesSript, CoffeScript or in any other language you have to transpile your code before running.
Kontent.ai CLI supports only running JavaScript migration files so in case you want to write in TypesScript, CoffeScript or in any other language you have to transpile your code before running.
In the case of TypeScript, you may use this example from [Kontent.ai CLI boilerplate](https://github.com/kontent-ai/migrations-boilerplate/blob/master/package.json#L7)

That's it! You've run your first Kontent.ai migration. This migration created a content type called *Blog* that contains three text elements named *Title*, *Author* and *Text*. The sample migration is written in TypeScript.
Expand All @@ -68,7 +69,7 @@ The boilerplate is configured to transpile TypeScript migrations into plain Java

You should now be able to go through the other boilerplate sample migrations. The migration scripts in the *Migrations* directory all focus on one scenario – replacing a piece of text from the *Author* text element with *Author* content items, which contain more information about the author. This way you can replace the texts within your items by more complex objects containing, for example, images and rich text.

You can use similar approach for your own specific scenarios. For example, imagine you need to add display information to the images inserted in your articles. You may want to specify relative size or caption for each image. To do this, you would need to open each article and replace every image with a component that would contain the image and a few elements for image metadata. You'd create small migration scripts for separate parts of the scenario (such as creating a new type, updating the articles, and so on) and the migrations will take care of the process for all articles within your project.
You can use similar approach for your own specific scenarios. For example, imagine you need to add display information to the images inserted in your articles. You may want to specify relative size or caption for each image. To do this, you would need to open each article and replace every image with a component that would contain the image and a few elements for image metadata. You'd create small migration scripts for separate parts of the scenario (such as creating a new type, updating the articles, and so on) and the migrations will take care of the process for all articles within your environment.

## Usage

Expand All @@ -89,10 +90,10 @@ kontent migration add --help
The supported commands are divided into groups according to their target, at this first version there are just to spaces "migration" and "environment" containing following commands:

* `environment add` – Store information about the environment locally.
* The environment is defined as a named pair of values. For example, "DEV" environment can be defined as a pair of a specific project ID and Management API key. This named pair of values is stored within your local repository in a configuration file named `.environments.json`.
* You can specify a named pair of project ID and Management API key using these options: `--project-id <your project ID> --api-key <management api key> --name <name of the environment>`.
* The environment is defined as a named pair of values. For example, "DEV" environment can be defined as a pair of a specific environment ID and Management API key. This named pair of values is stored within your local repository in a configuration file named `.environments.json`.
* You can specify a named pair of environment ID and Management API key using these options: `--environment-id <your environment ID> --api-key <management api key> --name <name of the environment>`.

* `migration add --name <migration name>` – Generates a script file (in JavaScript or TypeScript) for running a migration on a [Kontent.ai](https://kontent.ai/) project.
* `migration add --name <migration name>` – Generates a script file (in JavaScript or TypeScript) for running a migration on a [Kontent.ai](https://kontent.ai/) environment.
* The file is stored in the `Migrations` directory within the root of your repository.
* Add your migration script in the body of the `run` function using the [Kontent.ai Management SDK](https://github.com/kontent-ai/management-sdk-js) that was injected via the `apiClient` parameter.
* Add your rollback script in the body of the `rollback` function using the [Kontent.ai Management SDK](https://github.com/kontent-ai/management-sdk-js) that was injected via the `apiClient` parameter.
Expand Down Expand Up @@ -120,10 +121,10 @@ The supported commands are divided into groups according to their target, at thi
* `migration run` - Runs a migration script specified by file name (option `--name <file name>`), or runs multiple migration scripts in the order specified in the migration files (options `--all` or `--range`).
* By adding `--range` you need to add value in form of `number:number` in case of number ordering or in the format of `Tyyyy-mm-dd-hh-mm-ss:yyyy-mm-dd-hh-mm-ss` in case of date order.
> When using the range with dates, only the year value is mandatory and all other values are optional. It is fine to have a range specified like `T2023-01:2023-02`. It will take all migrations created in January of 2023. Notice the T at the beginning of the Date range. It helps to separate date ordering from number order.
* You can execute a migration against a specific project (options `--project <YOUR_PROJECT_ID> --api-key <YOUR_MANAGEMENT_API_KEY>`) or environment stored in the local configuration file (option `--environment <YOUR_ENVIRONMENT_NAME>`).
* You can execute a migration against a specific environment (options `--environment <YOUR_ENVIRONMENT_ID> --api-key <YOUR_MANAGEMENT_API_KEY>`) or environment stored in the local configuration file (option `--environment <YOUR_ENVIRONMENT_NAME>`).
* To execute your `rollback` scripts instead of `run` scripts, use the switch option `--rollback` or shortly `-b`.
* After each run of a migration script, the CLI logs the execution into a status file. This file holds data for the next run to prevent running the same migration script more than once. You can choose to override this behavior, for example for debugging purposes, by using the `--force` parameter.
> Note: For every migration there is only one record in the status file. Calling run/rollback continously overrides the that record with new data.
> Note: For every migration there is only one record in the status file. Calling run/rollback continuously overrides the that record with new data.
* You can choose whether you want to keep executing the migration scripts even if one migration script fails (option `--continue-on-error`) or whether you want to get additional information logged by HttpService into the console (option `--log-http-service-errors-to-console`).

* `backup --action [backup|restore|clean]` - This command enables you to use [Kontent.ai backup manager](https://github.com/kontent-ai/backup-manager-js)
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kontent-ai/cli",
"version": "0.7.5",
"version": "0.8.0",
"description": "Command line interface tool that can be used for generating and runningKontent.ai migration scripts",
"main": "./lib/index.js",
"types": "./lib/types/index.d.ts",
Expand Down Expand Up @@ -43,13 +43,13 @@
},
"homepage": "https://github.com/kontent-ai/cli#readme",
"dependencies": {
"@kontent-ai/backup-manager": "4.0.1",
"@kontent-ai/backup-manager": "4.1.0",
"chalk": "^4.1.2",
"dotenv": "^16.0.1",
"yargs": "^17.7.0"
"dotenv": "^16.3.1",
"yargs": "^17.7.2"
},
"peerDependencies": {
"@kontent-ai/management-sdk": "^3.1.0"
"@kontent-ai/management-sdk": "^5.0.0"
},
"devDependencies": {
"@babel/core": "~7.18.10",
Expand Down
24 changes: 12 additions & 12 deletions src/cmds/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { FileService } from '@kontent-ai/backup-manager/dist/cjs/lib/node';

const kontentBackupCommand: yargs.CommandModule = {
command: 'backup',
describe: 'Kontent.ai backup tool to backup & restore projects through Management API.',
describe: 'Kontent.ai backup tool to backup & restore environments through Management API.',
builder: (yargs: any) =>
yargs
.options({
Expand All @@ -27,9 +27,9 @@ const kontentBackupCommand: yargs.CommandModule = {
type: 'boolean',
default: true,
},
'project-id': {
alias: 'p',
describe: 'Project ID to run the migration script on',
'environment-id': {
alias: 'e',
describe: 'Environment ID to run the migration script on',
type: 'string',
},
'api-key': {
Expand All @@ -50,10 +50,10 @@ const kontentBackupCommand: yargs.CommandModule = {
},
})
.conflicts('environment', 'api-key')
.conflicts('environment', 'project-id')
.conflicts('environment', 'environment-id')
.check((args: any) => {
if (!args.environment && !(args.projectId && args.apiKey)) {
throw new Error(chalk.red('Specify an environment or a project ID with its Management API key.'));
if (!args.environment && !(args.environmentId && args.apiKey)) {
throw new Error(chalk.red('Specify an environment or a environment ID with its Management API key.'));
}

if (args.environment) {
Expand All @@ -71,12 +71,12 @@ const kontentBackupCommand: yargs.CommandModule = {
return true;
}),
handler: async (argv: any) => {
let projectId = argv.projectId;
let environmentId = argv.environmentId;
let apiKey = argv.apiKey;
if (argv.environment) {
const environments = getEnvironmentsConfig();

projectId = environments[argv.environment].projectId || argv.projectId;
environmentId = environments[argv.environment].environmentId || argv.environmentId;
apiKey = environments[argv.environment].apiKey || argv.apiKey;
}

Expand All @@ -96,7 +96,7 @@ const kontentBackupCommand: yargs.CommandModule = {
case 'backup':
const exportService = new ExportService({
apiKey: apiKey,
projectId: projectId,
projectId: environmentId,
onExport: (item: IProcessedItem) => {
if (argv.log) {
console.log(`Exported: ${item.title} | ${item.type}`);
Expand All @@ -119,7 +119,7 @@ const kontentBackupCommand: yargs.CommandModule = {
}
},
preserveWorkflow: argv.preserveWorkflow,
projectId: projectId,
projectId: environmentId,
apiKey: apiKey,
enableLog: argv.log,
fixLanguages: true,
Expand All @@ -134,7 +134,7 @@ const kontentBackupCommand: yargs.CommandModule = {
console.log(`Deleted: ${item.title} | ${item.type}`);
}
},
projectId: projectId,
projectId: environmentId,
apiKey: apiKey,
});

Expand Down
12 changes: 6 additions & 6 deletions src/cmds/environment/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { saveEnvironmentConfig } from '../../utils/environmentUtils';

const addEnvironmentCommand: yargs.CommandModule = {
command: 'add',
describe: 'Store information about the environment locally. The environment is defined as a named pair of values. For example, a "DEV" environment can be defined as a pair of specific project ID and Management API key.',
describe: 'Store information about the environment locally. The environment is defined as a named pair of values. For example, a "DEV" environment can be defined as a pair of specific environment ID and Management API key.',
builder: (yargs: any) =>
yargs
.options({
Expand All @@ -12,9 +12,9 @@ const addEnvironmentCommand: yargs.CommandModule = {
describe: 'Environment name',
type: 'string',
},
'project-id': {
alias: 'p',
describe: 'Project ID to run the migration script on',
'environment-id': {
alias: 'e',
describe: 'Environment ID to run the migration script on',
type: 'string',
},
'api-key': {
Expand All @@ -23,9 +23,9 @@ const addEnvironmentCommand: yargs.CommandModule = {
type: 'string',
},
})
.demandOption(['project-id', 'api-key', 'name']),
.demandOption(['environment-id', 'api-key', 'name']),
handler: (argv: any) => {
saveEnvironmentConfig(argv.name, argv.projectId, argv.apiKey);
saveEnvironmentConfig(argv.name, argv.environmentId, argv.apiKey);
},
};

Expand Down
2 changes: 1 addition & 1 deletion src/cmds/migration/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { TemplateType } from '../../models/templateType';

const addMigrationCommand: yargs.CommandModule = {
command: 'add',
describe: 'Generates a template script (in JavaScript or TypeScript) for running a migration on a Kontent.ai project.',
describe: 'Generates a template script (in JavaScript or TypeScript) for running a migration on a Kontent.ai environment.',
builder: (yargs: any) =>
yargs
.options({
Expand Down
Loading

0 comments on commit f2eb293

Please sign in to comment.