grafana/packages/grafana-toolkit
Arve Knudsen d28d495235
Chore: Enable PR testing in Drone (#26189)
* Add Drone configuration

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Add more steps

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Add more steps

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Build front-end before testing it

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Upgrade grafana/build-container

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Add packaging step

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Trigger on push

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Remove some steps

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Enable steps

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Install Dockerize

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Use node image for test-frontend

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Increase number of test workers

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Make plugin installation depend on frontend tests

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Make integration tests depend on frontend tests

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Use grafana/build-container also for front-end tests

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Upgrade dependencies in order to fix front-end tests

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Depend on es-check

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Dont' depend on tests before building front-end

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Add more steps

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Fix packaging

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Simplify

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Try to build images

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Fix e2e tests

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Remove steps

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Install netcat

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Include golangci-lint with grafana/build-container

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Build storybook and docs website

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Fix e2e tests

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Use build image with root user

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Drop CircleCI dependencies

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Fix e2e tests

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Fix e2e under Drone

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Execute e2e server separately

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Use own plugin for building Docker images

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Use Starlark to configure Drone

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Add enterprise steps to pipeline

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Add more enterprise steps to pipeline

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Maintain Yarn cache

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Build enterprise Docker images

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Build Ubuntu Docker images

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Refactor

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Add Postgres integration test

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Add MySQL integration test

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Fix integration tests

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Parameterize integration test DB connections

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Categorize integration tests

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Use grabpl integration-tests

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Remove unintended change

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Drone: Disable Ubuntu Docker images for PR pipeline

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Regenerate yarn.lock

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Upgrade grabpl

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Restore Yarn cache before installing in grafana-enterprise

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Use separate pipelines for OSS and enterprise

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Let OSS builds depend on tests

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Restore Go cache before building back-end

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Reduce number of variants built for PRs

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Fix building of Docker images

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Drone: Simplify logic

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Drone: Use Starlark

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Drone: Fix syntax error

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Convert .drone.star to YAML

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Upgrade AWS Go SDK

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Drone: Fix Go linting

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Undo irrelevant changes

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Revert "Undo irrelevant changes"

This reverts commit 5152f65972.

* Undo irrelevant changes

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* e2e: Support Circle

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Remove unused script

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* TypeScript fixes

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* TypeScript fixes

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Remove unused script

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* More Drone support

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Remove unused script

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Fix build on Circle

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Remove TODO comment

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
2020-07-10 16:09:21 +02:00
..
bin Toolkit: fix grafana-toolkit binary path so toolkit is not run in linked mode (#25860) 2020-06-26 10:45:39 -07:00
config/circleci Toolkit: fixes for security and publishing (#23749) 2020-04-23 12:58:22 -06:00
docker Chore: Rename plugin CI image as grafana/grafana-plugin-ci:latest-alpine (#26106) 2020-07-07 19:16:22 +02:00
src Chore: Enable PR testing in Drone (#26189) 2020-07-10 16:09:21 +02:00
.eslintrc TSLint → ESLint (#21006) 2020-02-08 02:40:04 +01:00
CHANGELOG.md Grafana v7.0 changelog update (#24737) 2020-05-18 17:52:59 +02:00
package.json Chore: bumped version to next minor. (#25971) 2020-07-01 13:26:36 +02:00
README.md Changelog and Readme: Update packages to beta and add Select breaking change (#24670) 2020-05-14 11:58:26 +02:00
tsconfig.json FieldOverride: Support data links via field overrides (#23590) 2020-04-20 07:37:38 +02:00

WARNING: @grafana/toolkit is currently in BETA.

grafana-toolkit

grafana-toolkit is a CLI that enables efficient development of Grafana plugins. We want to help our community focus on the core value of their plugins rather than all the setup required to develop them.

Getting started

Set up a new plugin with grafana-toolkit plugin:create command:

npx @grafana/toolkit plugin:create my-grafana-plugin
cd my-grafana-plugin
yarn install
yarn dev

Update your plugin to use grafana-toolkit

Follow the steps below to start using grafana-toolkit in your existing plugin.

  1. Add @grafana/toolkit package to your project by running yarn add @grafana/toolkit or npm install @grafana/toolkit.
  2. Create tsconfig.json file in the root dir of your plugin and paste the code below:
{
  "extends": "./node_modules/@grafana/toolkit/src/config/tsconfig.plugin.json",
  "include": ["src", "types"],
  "compilerOptions": {
    "rootDir": "./src",
    "baseUrl": "./src",
    "typeRoots": ["./node_modules/@types"]
  }
}
  1. Create .prettierrc.js file in the root dir of your plugin and paste the code below:
module.exports = {
  ...require('./node_modules/@grafana/toolkit/src/config/prettier.plugin.config.json'),
};
  1. In your package.json file add following scripts:
"scripts": {
  "build": "grafana-toolkit plugin:build",
  "test": "grafana-toolkit plugin:test",
  "dev": "grafana-toolkit plugin:dev",
  "watch": "grafana-toolkit plugin:dev --watch"
},

Usage

With grafana-toolkit, we give you a CLI that addresses common tasks performed when working on Grafana plugin:

  • grafana-toolkit plugin:create
  • grafana-toolkit plugin:dev
  • grafana-toolkit plugin:test
  • grafana-toolkit plugin:build

Create your plugin

grafana-toolkit plugin:create plugin-name

This command creates a new Grafana plugin from template.

If plugin-name is provided, then the template is downloaded to ./plugin-name directory. Otherwise, it will be downloaded to the current directory.

Develop your plugin

grafana-toolkit plugin:dev

This command creates a development build that's easy to play with and debug using common browser tooling.

Available options:

  • -w, --watch - run development task in a watch mode

Test your plugin

grafana-toolkit plugin:test

This command runs Jest against your codebase.

Available options:

Build your plugin

grafana-toolkit plugin:build

This command creates a production-ready build of your plugin.

FAQ

Which version of grafana-toolkit should I use?

See Grafana packages versioning guide.

What tools does grafana-toolkit use?

grafana-toolkit comes with TypeScript, ESLint, Prettier, Jest, CSS and SASS support.

How to start using grafana-toolkit in my plugin?

See Updating your plugin to use grafana-toolkit.

Can I use TypeScript to develop Grafana plugins?

Yes! grafana-toolkit supports TypeScript by default.

How can I test my plugin?

grafana-toolkit comes with Jest as a test runner.

Internally at Grafana we use Enzyme. If you are developing React plugin and you want to configure Enzyme as a testing utility, then you need to configure enzyme-adapter-react. To do so, create <YOUR_PLUGIN_DIR>/config/jest-setup.ts file that will provide necessary setup. Copy the following code into that file to get Enzyme working with React:

import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

configure({ adapter: new Adapter() });

You can also set up Jest with shims of your needs by creating jest-shim.ts file in the same directory: <YOUR_PLUGIN_DIR_>/config/jest-shim.ts

Can I provide custom setup for Jest?

You can provide custom Jest configuration with a package.json file. For more details, see Jest docs.

Currently we support following Jest configuration properties:

How can I customize Webpack rules or plugins?

You can provide your own webpack.config.js file that exports a getWebpackConfig function. We recommend that you extend the standard configuration, but you are free to create your own:

const CustomPlugin = require('custom-plugin');

module.exports.getWebpackConfig = (config, options) => ({
  ...config,
  plugins: [...config.plugins, new CustomPlugin()],
});

How can I style my plugin?

We support pure CSS, SASS, and CSS-in-JS approach (via Emotion).

Single CSS or SASS file

Create your CSS or SASS file and import it in your plugin entry point (typically module.ts):

import 'path/to/your/css_or_sass';

The styles will be injected via style tag during runtime.

Note that imported static assets will be inlined as base64 URIs. This can be subject of change in the future!

Theme-specific stylesheets

If you want to provide different stylesheets for dark/light theme, then create dark.[css|scss] and light.[css|scss] files in the src/styles directory of your plugin. grafana-toolkit generates theme-specific stylesheets that are stored in dist/styles directory.

In order for Grafana to pick up your theme stylesheets, you need to use loadPluginCss from @grafana/runtime package. Typically you would do that in the entry point of your plugin:

import { loadPluginCss } from '@grafana/runtime';

loadPluginCss({
  dark: 'plugins/<YOUR-PLUGIN-ID>/styles/dark.css',
  light: 'plugins/<YOUR-PLUGIN-ID>/styles/light.css',
});

You must add @grafana/runtime to your plugin dependencies by running yarn add @grafana/runtime or npm install @grafana/runtime.

Note that in this case static files (png, svg, json, html) are all copied to dist directory when the plugin is bundled. Relative paths to those files does not change!

Emotion

Starting from Grafana 6.2 our suggested way for styling plugins is by using Emotion. It's a CSS-in-JS library that we use internally at Grafana. The biggest advantage of using Emotion is that you can access Grafana Theme variables.

To start using Emotion, you first must add it to your plugin dependencies:

  yarn add "emotion"@10.0.27

Then, import css function from Emotion:

import { css } from 'emotion';

Now you are ready to implement your styles:

const MyComponent = () => {
  return (
    <div
      className={css`
        background: red;
      `}
    />
  );
};

To learn more about using Grafana theme please refer to Theme usage guide

We do not support Emotion's css prop. Use className instead!

Can I adjust TypeScript configuration to suit my needs?

Yes! However, it's important that your tsconfig.json file contains the following lines:

{
  "extends": "./node_modules/@grafana/toolkit/src/config/tsconfig.plugin.json",
  "include": ["src"],
  "compilerOptions": {
    "rootDir": "./src",
    "typeRoots": ["./node_modules/@types"]
  }
}

Can I adjust ESLint configuration to suit my needs?

grafana-toolkit comes with default config for ESLint. For now, there is now way to customise ESLint config.

How is Prettier integrated into grafana-toolkit workflow?

When building plugin with grafana-toolkit plugin:build task, grafana-toolkit performs Prettier check. If the check detects any Prettier issues, the build will not pass. To avoid such situation we suggest developing plugin with grafana-toolkit plugin:dev --watch task running. This task tries to fix Prettier issues automatically.

My editor does not respect Prettier config, what should I do?

In order for your editor to pick up our Prettier config you need to create .prettierrc.js file in the root directory of your plugin with following content:

module.exports = {
  ...require('./node_modules/@grafana/toolkit/src/config/prettier.plugin.config.json'),
};

How do I add third-party dependencies that are not npm packages?

Put them in the static directory in the root of your project. The static directory is copied when the plugin is built.

I am getting this message when I run yarn install: Request failed \"404 Not Found\"

If you are using version canary, this error occurs because a canary release unpublishes previous versions leaving yarn.lock outdated. Remove yarn.lock and run yarn install again.

I am getting this message when I run my plugin: Unable to dynamically transpile ES module A loader plugin needs to be configured via SystemJS.config({ transpiler: 'transpiler-module' }).

This error occurs when you bundle your plugin using the grafana-toolkit plugin:dev task and your code comments include ES2016 code.

There are two issues at play:

  • The grafana-toolkit plugin:dev task does not remove comments from your bundled package.
  • Grafana does not support ES modules.

If your comments include ES2016 code, then SystemJS v0.20.19, which Grafana uses internally to load plugins, interprets your code as an ESM and fails.

To fix this error, remove the ES2016 code from your comments.

Contribute to grafana-toolkit

You can contribute to grafana-toolkit by helping develop it or by debugging it.

Develop grafana-toolkit

Typically plugins should be developed using the @grafana/toolkit installed from npm. However, when working on the toolkit, you might want to use the local version. Follow the steps below to develop with a local version:

  1. Clone Grafana repository.
  2. Navigate to the directory you have cloned Grafana repo to and then run yarn install --pure-lockfile.
  3. Navigate to <GRAFANA_DIR>/packages/grafana-toolkit and then run yarn link.
  4. Navigate to the directory where your plugin code is and then run npx grafana-toolkit plugin:dev --yarnlink. This adds all dependencies required by grafana-toolkit to your project, as well as link your local grafana-toolkit version to be used by the plugin.

Debug grafana-toolkit

To debug grafana-toolkit you can use standard NodeJS debugging methods (node --inspect, node --inspect-brk).

To run grafana-toolkit in a debugging session use the following command in the toolkit's directory:

node --inspect-brk ./bin/grafana-toolkit.js [task]

To run linked grafana-toolkit in a debugging session use the following command in the plugin's directory:

node --inspect-brk ./node_modules/@grafana/toolkit/bin/grafana-toolkit.js [task]