diff --git a/docusaurus/docs/creating-a-plugin.md b/docusaurus/docs/creating-a-plugin.mdx similarity index 92% rename from docusaurus/docs/creating-a-plugin.md rename to docusaurus/docs/creating-a-plugin.mdx index 03c5c6339..42ad9f1e4 100644 --- a/docusaurus/docs/creating-a-plugin.md +++ b/docusaurus/docs/creating-a-plugin.mdx @@ -5,9 +5,15 @@ title: Creating a Plugin To create a new Grafana plugin run the following command in your shell: -```shell -npx @grafana/create-plugin@latest -``` + :::info The answers to the name, organization and type of plugin prompts are combined to create the plugin directory name and the plugin id. @@ -25,7 +31,6 @@ In the above example, this will cause the directory and plugin ID to be named `g When running the create command, the following prompts appear asking for confirmation before making changes: - ### What is the name of your plugin? The name of your plugin. This helps to identify its purpose. diff --git a/docusaurus/docs/distributing-your-plugin.md b/docusaurus/docs/distributing-your-plugin.md index 4f1aad01f..1c2ca956e 100644 --- a/docusaurus/docs/distributing-your-plugin.md +++ b/docusaurus/docs/distributing-your-plugin.md @@ -7,7 +7,7 @@ To distribute a Grafana plugin either to the community or privately, the plugin :::info -It's not necessary to sign a plugin during development. The [docker development environment](./docker.md) that is scaffolded with `@grafana/create-plugin` will load the plugin without a signature. +It's not necessary to sign a plugin during development. The [docker development environment](./docker.mdx) that is scaffolded with `@grafana/create-plugin` will load the plugin without a signature. ::: diff --git a/docusaurus/docs/docker.md b/docusaurus/docs/docker.mdx similarity index 76% rename from docusaurus/docs/docker.md rename to docusaurus/docs/docker.mdx index 14d6750ae..3cb165639 100644 --- a/docusaurus/docs/docker.md +++ b/docusaurus/docs/docker.mdx @@ -13,19 +13,26 @@ The Docker container is configured with the necessary variables to allow easy ac To get started run the following commands in the order listed: -- `yarn install`: Install frontend dependencies. -- `yarn dev`: Build and watch the plugin frontend code. +- : Install frontend dependencies. +- : Build and watch the plugin frontend code. - `mage -v build:linux`: Build the plugin backend code. This command must be rerun every time you edit your backend files. -- `yarn server`: Start a grafana development server running on [http://localhost:3000](http://localhost:3000). Restart this command each time you run `mage` to run your new backend code. - +- : Start a grafana development server running on + [http://localhost:3000](http://localhost:3000). Restart this command each time you run `mage` to run your new backend + code. ### Configure the Grafana version You can use the environment variable `GRAFANA_VERSION` to set the Grafana version whilst developing the plugin. This is useful for testing a plugin across different versions of Grafana. -```shell -GRAFANA_VERSION=8.5.5 yarn server -``` + ### Configure the Grafana image diff --git a/docusaurus/docs/frontend.md b/docusaurus/docs/frontend.mdx similarity index 75% rename from docusaurus/docs/frontend.md rename to docusaurus/docs/frontend.mdx index e62f8e4c3..9207740df 100644 --- a/docusaurus/docs/frontend.md +++ b/docusaurus/docs/frontend.mdx @@ -5,26 +5,28 @@ title: Frontend The frontend part of a Grafana plugin is written in Typescript with React as the view library. Within the plugin directory, you can run: -### `yarn dev` + +### Builds the plugin in development mode and watches for changes to source code to rebuild the plugin. -### `yarn build` +### + Builds the plugin for production. The output can be found in the `./dist` directory. +### -### `yarn test` Launches the test runner in watch mode. +### -### `yarn e2e` Launches the e2e test runner. (Make sure to have an instance of Grafana running before running e2e tests.) +### -### `yarn lint` Run the linter against source code to verify formatting and styling rules are adherred to. +### -### `yarn lint:fix` Run the linter in fix mode to automatically format the frontend code. diff --git a/docusaurus/docs/getting-started.md b/docusaurus/docs/getting-started.mdx similarity index 74% rename from docusaurus/docs/getting-started.md rename to docusaurus/docs/getting-started.mdx index 351658fd7..5952fbb6e 100644 --- a/docusaurus/docs/getting-started.md +++ b/docusaurus/docs/getting-started.mdx @@ -12,11 +12,17 @@ Plugin tools consists of two packages: ## Quick Start -```shell -npx @grafana/create-plugin@latest -``` - -Follow the prompts to scaffold a Grafana plugin. [See here](./creating-a-plugin.md) for detailed information about creating a plugin. + + +Follow the prompts to scaffold a Grafana plugin. [See here](./creating-a-plugin.mdx) for detailed information about creating a plugin. :::note @@ -30,9 +36,12 @@ Plugins previously scaffolded with `@grafana/toolkit` can be migrated. Please se - [Mage](https://magefile.org/) - [Node.js](https://nodejs.org/en/download/) version 16 or above: - When installing Node.js, you are recommended to check all checkboxes related to dependencies. -- [Yarn 1](https://classic.yarnpkg.com/lang/en/docs/install) +- Optionally [Yarn 1](https://classic.yarnpkg.com/lang/en/docs/install) or [PNPM](https://pnpm.io/installation) - [Docker](https://docs.docker.com/get-docker/) +### Choosing a package manager +Starting from v1.3 of Create Plugin when you create a new plugin, the CLI will use `npm`, `pnpm` or `yarn` depending on which tool you use to run `@grafana/create-plugin`. + ## Output Running the above command will create a directory called `--` inside the current directory. Within this new directory is the initial project structure to kickstart development. @@ -83,27 +92,32 @@ Depending on the answers given to the prompts there should be a structure like: Once the installation is done you can open the plugin folder: -```shell -cd -- -yarn install -``` + ## Scripts Inside the newly create plugin, you can run some built-in commands: -### `yarn dev` +### Builds plugin in development mode and runs in watch mode. The plugin will be rebuilt whenever you make changes to the code. You will see build errors and lint warnings in the console. -### `yarn test` +### Runs the tests and watches for changes -### `yarn build` +### Creates a production build of the plugin optimizing for the best performance. The build is minified and the filenames include hashes. -### `mage -v` +

mage -v

Build backend plugin binaries for Linux, Windows and Darwin. diff --git a/docusaurus/docs/migrating-from-toolkit.md b/docusaurus/docs/migrating-from-toolkit.mdx similarity index 89% rename from docusaurus/docs/migrating-from-toolkit.md rename to docusaurus/docs/migrating-from-toolkit.mdx index 63e358219..44878bcb9 100644 --- a/docusaurus/docs/migrating-from-toolkit.md +++ b/docusaurus/docs/migrating-from-toolkit.mdx @@ -13,7 +13,7 @@ We [do not support](https://grafana.com/docs/grafana/latest/developers/angular_d - **More flexible:** With @grafana/create-plugin, developers have more control over their plugins, its dependencies, and can more easily customize the tooling to fit their specific needs. - **Faster development time:** With its out-of-the-box development environment @grafana/create-plugin can significantly reduce development time compared to using @grafana/toolkit. -- **Improved testing capabilities:** Testing plugins with @grafana/create-plugin is much easier with github workflows that automate unit and e2e test runs whenever changes are pushed to github. +- **Improved testing capabilities:** Testing plugins with @grafana/create-plugin is much easier with github workflows that automate unit and e2e test runs whenever changes are pushed to github. - **Better documentation:** The documentation for @grafana/create-plugin is more comprehensive and easier to discover than @grafana/toolkit. :::warning @@ -24,9 +24,15 @@ Before running the following command we strongly suggest backing up the code. Id To get started, in the root directory of the existing plugin (where the `package.json` file is) run the following command and answer the prompts. -``` -npx @grafana/create-plugin@latest migrate -``` + ## Prompts @@ -89,7 +95,7 @@ This step will update any npm scripts in the `package.json` file to match the la ## Next steps -When the migration command finishes running, look at the changes introduced and then running `yarn install` followed by each of the npm scripts inside `package.json` to confirm the plugin can be built, tested, signed etc. +When the migration command finishes running, look at the changes introduced and then install frontend dependencies followed by running each of the npm scripts inside `package.json` to confirm the plugin can be built, tested, signed etc. If the plugin had customizations to the toolkit configs (e.g. webpack) head over to the [advanced configuration guide](./advanced-configuration.md) to learn how to update them. diff --git a/docusaurus/docs/nested-plugins.md b/docusaurus/docs/nested-plugins.mdx similarity index 86% rename from docusaurus/docs/nested-plugins.md rename to docusaurus/docs/nested-plugins.mdx index 09c59f902..048b30988 100644 --- a/docusaurus/docs/nested-plugins.md +++ b/docusaurus/docs/nested-plugins.mdx @@ -5,9 +5,15 @@ title: Nested Plugins Grafana app plugins can nest frontend datasource and panel plugins making it easy to ship a complete user experience. To take advantage of this feature first scaffold an app plugin using `@grafana/create-plugin`: -```bash -npx @grafana/create-plugin@latest -``` + When prompted `What type of plugin would you like?` select `app`. diff --git a/docusaurus/docs/snippets/createplugin-install.npm.shell.md b/docusaurus/docs/snippets/createplugin-install.npm.shell.md new file mode 100644 index 000000000..d94fa12e5 --- /dev/null +++ b/docusaurus/docs/snippets/createplugin-install.npm.shell.md @@ -0,0 +1,2 @@ +cd -- +npm install \ No newline at end of file diff --git a/docusaurus/docs/snippets/createplugin-install.pnpm.shell.md b/docusaurus/docs/snippets/createplugin-install.pnpm.shell.md new file mode 100644 index 000000000..3c87df311 --- /dev/null +++ b/docusaurus/docs/snippets/createplugin-install.pnpm.shell.md @@ -0,0 +1,2 @@ +cd -- +pnpm install \ No newline at end of file diff --git a/docusaurus/docs/snippets/createplugin-install.yarn.shell.md b/docusaurus/docs/snippets/createplugin-install.yarn.shell.md new file mode 100644 index 000000000..b0e154bd5 --- /dev/null +++ b/docusaurus/docs/snippets/createplugin-install.yarn.shell.md @@ -0,0 +1,2 @@ +cd -- +yarn install \ No newline at end of file diff --git a/docusaurus/docs/snippets/createplugin-migrate.npm.shell.md b/docusaurus/docs/snippets/createplugin-migrate.npm.shell.md new file mode 100644 index 000000000..ea06ed6f3 --- /dev/null +++ b/docusaurus/docs/snippets/createplugin-migrate.npm.shell.md @@ -0,0 +1 @@ +npx @grafana/create-plugin@latest migrate \ No newline at end of file diff --git a/docusaurus/docs/snippets/createplugin-migrate.pnpm.shell.md b/docusaurus/docs/snippets/createplugin-migrate.pnpm.shell.md new file mode 100644 index 000000000..05cb440ec --- /dev/null +++ b/docusaurus/docs/snippets/createplugin-migrate.pnpm.shell.md @@ -0,0 +1 @@ +pnpm dlx @grafana/create-plugin@latest migrate \ No newline at end of file diff --git a/docusaurus/docs/snippets/createplugin-migrate.yarn.shell.md b/docusaurus/docs/snippets/createplugin-migrate.yarn.shell.md new file mode 100644 index 000000000..96acb4d2a --- /dev/null +++ b/docusaurus/docs/snippets/createplugin-migrate.yarn.shell.md @@ -0,0 +1 @@ +yarn create @grafana/plugin migrate \ No newline at end of file diff --git a/docusaurus/docs/snippets/createplugin-scaffold.npm.shell.md b/docusaurus/docs/snippets/createplugin-scaffold.npm.shell.md new file mode 100644 index 000000000..31bf7730e --- /dev/null +++ b/docusaurus/docs/snippets/createplugin-scaffold.npm.shell.md @@ -0,0 +1 @@ +npx @grafana/create-plugin@latest \ No newline at end of file diff --git a/docusaurus/docs/snippets/createplugin-scaffold.pnpm.shell.md b/docusaurus/docs/snippets/createplugin-scaffold.pnpm.shell.md new file mode 100644 index 000000000..918c00cfe --- /dev/null +++ b/docusaurus/docs/snippets/createplugin-scaffold.pnpm.shell.md @@ -0,0 +1 @@ +pnpm dlx @grafana/create-plugin@latest \ No newline at end of file diff --git a/docusaurus/docs/snippets/createplugin-scaffold.yarn.shell.md b/docusaurus/docs/snippets/createplugin-scaffold.yarn.shell.md new file mode 100644 index 000000000..290bf8980 --- /dev/null +++ b/docusaurus/docs/snippets/createplugin-scaffold.yarn.shell.md @@ -0,0 +1 @@ +yarn create @grafana/plugin \ No newline at end of file diff --git a/docusaurus/docs/snippets/createplugin-update.npm.shell.md b/docusaurus/docs/snippets/createplugin-update.npm.shell.md new file mode 100644 index 000000000..527c8fd2a --- /dev/null +++ b/docusaurus/docs/snippets/createplugin-update.npm.shell.md @@ -0,0 +1 @@ +npx @grafana/create-plugin@latest update \ No newline at end of file diff --git a/docusaurus/docs/snippets/createplugin-update.pnpm.shell.md b/docusaurus/docs/snippets/createplugin-update.pnpm.shell.md new file mode 100644 index 000000000..be899411e --- /dev/null +++ b/docusaurus/docs/snippets/createplugin-update.pnpm.shell.md @@ -0,0 +1 @@ +pnpm dlx @grafana/create-plugin@latest update \ No newline at end of file diff --git a/docusaurus/docs/snippets/createplugin-update.yarn.shell.md b/docusaurus/docs/snippets/createplugin-update.yarn.shell.md new file mode 100644 index 000000000..18872fa91 --- /dev/null +++ b/docusaurus/docs/snippets/createplugin-update.yarn.shell.md @@ -0,0 +1 @@ +yarn create @grafana/plugin update \ No newline at end of file diff --git a/docusaurus/docs/snippets/docker-grafana-version.npm.shell.md b/docusaurus/docs/snippets/docker-grafana-version.npm.shell.md new file mode 100644 index 000000000..e4a18f3f0 --- /dev/null +++ b/docusaurus/docs/snippets/docker-grafana-version.npm.shell.md @@ -0,0 +1 @@ +GRAFANA_VERSION=8.5.5 npm run server \ No newline at end of file diff --git a/docusaurus/docs/snippets/docker-grafana-version.pnpm.shell.md b/docusaurus/docs/snippets/docker-grafana-version.pnpm.shell.md new file mode 100644 index 000000000..5ef4a98d4 --- /dev/null +++ b/docusaurus/docs/snippets/docker-grafana-version.pnpm.shell.md @@ -0,0 +1 @@ +GRAFANA_VERSION=8.5.5 pnpm run server \ No newline at end of file diff --git a/docusaurus/docs/snippets/docker-grafana-version.yarn.shell.md b/docusaurus/docs/snippets/docker-grafana-version.yarn.shell.md new file mode 100644 index 000000000..511eb9b97 --- /dev/null +++ b/docusaurus/docs/snippets/docker-grafana-version.yarn.shell.md @@ -0,0 +1 @@ +GRAFANA_VERSION=8.5.5 yarn run server \ No newline at end of file diff --git a/docusaurus/docs/updating-to-new-releases.md b/docusaurus/docs/updating-to-new-releases.mdx similarity index 90% rename from docusaurus/docs/updating-to-new-releases.md rename to docusaurus/docs/updating-to-new-releases.mdx index 8eee8d4c6..d7d6929a1 100644 --- a/docusaurus/docs/updating-to-new-releases.md +++ b/docusaurus/docs/updating-to-new-releases.mdx @@ -5,9 +5,11 @@ title: Updating to New Releases To update an existing plugin to use a newer version of `create-plugin` run the following update command: -```shell -npx @grafana/create-plugin@latest update -``` + This command will rerun the original scaffolding commands against the configuration files, dependencies, and scripts, using the latest version of `create-plugin`. It will prompt to confirm any destructive operations are agreed to prior to being run. diff --git a/docusaurus/website/docusaurus.config.js b/docusaurus/website/docusaurus.config.js index 8439c3549..044ccc797 100644 --- a/docusaurus/website/docusaurus.config.js +++ b/docusaurus/website/docusaurus.config.js @@ -51,7 +51,7 @@ const config = { }); // clean up metadata, relref and remove heading const cleanedContent = content - .replace(/\\\[(.+)\]\\(\({{.+}}\))/g, '$1') + .replace(/\\\[(.+?)\]\\(\({{.+?}}\))/g, '$1') .replace(/## title: Sign a plugin\n\n# Sign a plugin/m, '') .replace(/\*\*\*/, ''); return { @@ -81,6 +81,7 @@ ${cleanedContent} ({ docs: { path: '../docs', + exclude: ['**/snippets/**'], sidebarPath: require.resolve('./sidebars.js'), // Please change this to your repo. // Remove this to remove the "edit this page" links. diff --git a/docusaurus/website/package.json b/docusaurus/website/package.json index 1e0f939da..fcd7ecbdb 100644 --- a/docusaurus/website/package.json +++ b/docusaurus/website/package.json @@ -29,6 +29,7 @@ "devDependencies": { "@docusaurus/module-type-aliases": "2.4.0", "@tsconfig/docusaurus": "^1.0.5", + "raw-loader": "^4.0.2", "typescript": "^4.7.4" }, "browserslist": { diff --git a/docusaurus/website/src/components/CodeSnippets/CodeSnippets.tsx b/docusaurus/website/src/components/CodeSnippets/CodeSnippets.tsx new file mode 100644 index 000000000..e6e2dfe4b --- /dev/null +++ b/docusaurus/website/src/components/CodeSnippets/CodeSnippets.tsx @@ -0,0 +1,68 @@ +import React, { useEffect, useState } from 'react'; +import CodeBlock from '@theme/CodeBlock'; +import Tabs from '@theme/Tabs'; +import TabItem from '@theme/TabItem'; + +/* + CodeSnippets is a component that allows you to display code snippets from the docs/snippets folder. + It takes in an array of paths to the snippets you want to display, and will display them in a tabbed + interface. The paths should be relative to the docs/snippets folder, and should include the file extension. + + Snippets should be named in the following format: ... + `value` is used for the tabs unique value and label. + language is used for syntax highlighting. + */ + +function CodeSnippets({ paths, groupId, queryString }: { paths: string[]; groupId?: string; queryString?: string }) { + const snippetPaths = paths.map((path) => path.trim()); + const [snippets, setSnippets] = useState([]); + + useEffect(() => { + async function fetchSnippets() { + const fetchedSnippets = await Promise.all( + snippetPaths.map(async (path) => { + const [_, value, language] = path.split('.'); + try { + const code = (await import(`!!raw-loader!@site/../docs/snippets/${path}`)).default; + + return { + language, + value, + label: value.toUpperCase(), + code, + }; + } catch (error) { + console.log(`CodeSnippets failed to load: docs/snippets/${path}`, error); + return null; + } + }) + ); + setSnippets(fetchedSnippets.filter((snippet) => snippet != null)); + } + + fetchSnippets(); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); + + if (snippets.length === 0) { + return null; + } + + if (snippets.length === 1) { + return {snippets[0].code}; + } + + return ( + // @ts-ignore - Tabs types appear to be broken. + + {snippets.map((snippet) => ( + // @ts-ignore - TabItem types appear to be broken. + + {snippet.code} + + ))} + + ); +} + +export default CodeSnippets; diff --git a/docusaurus/website/src/components/HomepageGettingStarted/index.tsx b/docusaurus/website/src/components/HomepageGettingStarted/index.tsx index 328cbf79b..271428d71 100644 --- a/docusaurus/website/src/components/HomepageGettingStarted/index.tsx +++ b/docusaurus/website/src/components/HomepageGettingStarted/index.tsx @@ -1,6 +1,6 @@ import React from 'react'; import useBaseUrl from '@docusaurus/useBaseUrl'; -import CodeBlock from '@theme/CodeBlock'; +import CodeSnippets from '../CodeSnippets/CodeSnippets'; import styles from './styles.module.css'; @@ -19,7 +19,7 @@ function HomepageGettingStarted() {
To create a plugin run this command:

- npx @grafana/create-plugin +
@@ -44,7 +44,7 @@ function HomepageGettingStarted() { Updating your build tooling can be a daunting and time-consuming task. When new versions of Create Plugin are released, you can upgrade using a single command:

- npx @grafana/create-plugin update +
diff --git a/docusaurus/website/src/components/SyncCommand/SyncCommand.tsx b/docusaurus/website/src/components/SyncCommand/SyncCommand.tsx new file mode 100644 index 000000000..34807d588 --- /dev/null +++ b/docusaurus/website/src/components/SyncCommand/SyncCommand.tsx @@ -0,0 +1,30 @@ +import React from 'react'; +import { useLocation } from '@docusaurus/router'; +import BrowserOnly from '@docusaurus/BrowserOnly'; + +interface SyncCommandProps { + cmd: string; +} + +function SyncCommand({ cmd }: SyncCommandProps) { + let currentPackageManager = window.localStorage['docusaurus.tab.package-manager']; + const location = useLocation(); + + if (!currentPackageManager) { + if (location && location.search && location.search.includes('current-package-manager')) { + const searchParams = new URLSearchParams(location.search); + currentPackageManager = searchParams.get('current-package-manager'); + } else { + currentPackageManager = 'npm'; + } + } + const cmdString = `${currentPackageManager} ${cmd}`; + + return {cmdString}; +} + +function BrowserOnlySyncCommand({ cmd }: SyncCommandProps) { + return {() => }; +} + +export default BrowserOnlySyncCommand; diff --git a/docusaurus/website/src/pages/markdown-page.md b/docusaurus/website/src/pages/markdown-page.md deleted file mode 100644 index 9756c5b66..000000000 --- a/docusaurus/website/src/pages/markdown-page.md +++ /dev/null @@ -1,7 +0,0 @@ ---- -title: Markdown page example ---- - -# Markdown page example - -You don't need React to write simple standalone pages. diff --git a/docusaurus/website/src/theme/MDXComponents.tsx b/docusaurus/website/src/theme/MDXComponents.tsx new file mode 100644 index 000000000..db15cd0a6 --- /dev/null +++ b/docusaurus/website/src/theme/MDXComponents.tsx @@ -0,0 +1,14 @@ +import React from 'react'; +// Import the original mapper +import MDXComponents from '@theme-original/MDXComponents'; +import CodeSnippets from '@site/src/components/CodeSnippets/CodeSnippets'; +import SyncCommand from '@site/src/components/SyncCommand/SyncCommand'; + +export default { + // Re-use the default mapping + ...MDXComponents, + // Map the "" tag to our CodeSnippets component + // `CodeSnippets` will receive all props that were passed to `` in MDX + CodeSnippets, + SyncCommand, +}; diff --git a/yarn.lock b/yarn.lock index dc0fc704d..745faa59a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -12850,6 +12850,14 @@ raw-body@2.5.1: iconv-lite "0.4.24" unpipe "1.0.0" +raw-loader@^4.0.2: + version "4.0.2" + resolved "https://registry.yarnpkg.com/raw-loader/-/raw-loader-4.0.2.tgz#1aac6b7d1ad1501e66efdac1522c73e59a584eb6" + integrity sha512-ZnScIV3ag9A4wPX/ZayxL/jZH+euYb6FcUinPcgiQW0+UBtEv0O6Q3lGd3cqJ+GHH+rksEv3Pj99oxJ3u3VIKA== + dependencies: + loader-utils "^2.0.0" + schema-utils "^3.0.0" + rc@1.2.8, rc@^1.2.8, rc@~1.2.7: version "1.2.8" resolved "https://registry.yarnpkg.com/rc/-/rc-1.2.8.tgz#cd924bf5200a075b83c188cd6b9e211b7fc0d3ed"