directus-labs / directus-template-cli

Apply "templates" to a new, empty Directus project.
MIT License
88 stars 15 forks source link

Directus Template CLI

A streamlined CLI tool for managing Directus templates - making it easy to apply and extract template configurations across instances.

⚠️ Beta Release Notice: This tool is currently in beta and best suited for:

We strongly recommend against using this tool in existing production environments or as a critical part of your CI/CD pipeline without thorough testing. Always create backups before applying templates.

Important Notes:

Using the @latest tag ensures you're receiving the latest version of the packaged templates with the CLI. You can review the specific versions on NPM and use @{version} syntax to apply the templates included with that version.

Applying a Template

🚧 Make backups of your project/database before applying templates.

  1. Create a Directus instance on Directus Cloud or using self-hosted version.
  2. Login and create a Static Access Token for the admin user.
  3. Copy the static token and your Directus URL.
  4. Run the following command on the terminal and follow the prompts.
npx directus-template-cli@latest apply

You can choose from our community maintained templates or you can also choose a template from a local directory or a public GitHub repository.

Programmatic Mode

By default, the CLI will run in interactive mode. For CI/CD pipelines or automated scripts, you can use the programmatic mode:

Using a token:

npx directus-template-cli@latest apply -p --directusUrl="http://localhost:8055" --directusToken="admin-token-here" --templateLocation="./my-template" --templateType="local"

Using email/password:

npx directus-template-cli@latest apply -p --directusUrl="http://localhost:8055" --userEmail="admin@example.com" --userPassword="admin" --templateLocation="./my-template" --templateType="local"

Partial apply (apply only some of the parts of a template to the instance):

npx directus-template-cli@latest apply -p --directusUrl="http://localhost:8055" --userEmail="admin@example.com" --userPassword="your-password" --templateLocation="./my-template" --templateType="local" --partial --schema --permissions --no-content

Available flags for programmatic mode:

When using --partial, you can also use --no flags to exclude specific components from being applied. For example:

npx directus-template-cli@latest apply -p --directusUrl="http://localhost:8055" --userEmail="admin@example.com" --userPassword="your-password" --templateLocation="./my-template" --templateType="local" --partial --no-content --no-users

This command will apply the template but exclude content and users. Available --no flags include:

Template Component Dependencies

When applying templates, certain components have dependencies on others. Here are the key relationships to be aware of:

When using the --partial flag, keep these dependencies in mind. For example:

npx directus-template-cli@latest apply -p --directusUrl="http://localhost:8055" --directusToken="admin-token-here" --templateLocation="./my-template" --templateType="local" --partial --users

This command will automatically include --permissions and --schema along with --users, even if not explicitly specified.

If you use --no- flags, be cautious about excluding dependencies. For instance, using --no-schema while including --content may lead to errors or incomplete application of the template.

Using Environment Variables

You can also pass flags as environment variables. This can be useful for CI/CD pipelines or when you want to avoid exposing sensitive information in command-line arguments. Here are the available environment variables:

Existing Data

You can apply a template to an existing Directus instance. This is nice because you can have smaller templates that you can "compose" for various use cases. The CLI tries to be smart about existing items in the target Directus instance. But mileage may vary depending on the size and complexity of the template and the existing instance.

System Collections

In most of the system collections (collections,roles, permissions, etc.), if an item with the same identifier already exists, it will be typically be SKIPPED vs overwritten.

Exceptions:

Your Collections:

For data in your own user-created collections, if an item has the same primary key, the data will be overwritten with the incoming data from the template.


Extracting a Template

The CLI can also extract a template from a Directus instance so that it can be applied to other instances.

Note: We do not currently support partial extraction. The entire template will be extracted. We thought it better to have the data and not need it, than need it and not have it.

  1. Make sure you remove any sensitive data from the Directus instance you don't want to include in the template.
  2. Login and create a Static Access Token for the admin user.
  3. Copy the static token and your Directus URL.
  4. Run the following command on the terminal and follow the prompts.
npx directus-template-cli@latest extract

Programmatic Mode

By default, the CLI will run in interactive mode. For CI/CD pipelines or automated scripts, you can use the programmatic mode:

Using a token:

npx directus-template-cli@latest extract -p --templateName="My Template" --templateLocation="./my-template" --directusToken="admin-token-here" --directusUrl="http://localhost:8055"

Using email/password:

npx directus-template-cli@latest extract -p --templateName="My Template" --templateLocation="./my-template" --userEmail="admin@example.com" --userPassword="admin" --directusUrl="http://localhost:8055"

Available flags for programmatic mode:

Using Environment Variables

Similar to the Apply command, you can use environment variables for the Extract command as well:

Logs

The Directus Template CLI logs information to a file in the .directus-template-cli/logs directory.

Logs are automatically generated for each run of the CLI. Here's how the logging system works:

The logger automatically sanitizes sensitive information such as passwords, tokens, and keys before writing to the log file. But it may not catch everything. Just be aware of this and make sure to remove the log files when they are no longer needed.

Note: If you encounter any issues with the CLI, providing these log files can greatly assist in diagnosing and resolving the problem.

License

This tool is licensed under the MIT License.