Skip to content

Commit f100720

Browse files
committed
refactor(create-plugin): move panel-docs templates to templates/docs
1 parent ca746b2 commit f100720

14 files changed

Lines changed: 64 additions & 56 deletions

File tree

packages/create-plugin/src/codemods/additions/additions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ export default [
1414
{
1515
name: 'panel-docs',
1616
description: 'Scaffolds multi-page documentation for a Grafana panel plugin',
17-
scriptPath: import.meta.resolve('./scripts/panel-docs/index.js'),
17+
scriptPath: import.meta.resolve('./scripts/panel-docs.js'),
1818
},
1919
] satisfies Codemod[];

packages/create-plugin/src/codemods/additions/scripts/panel-docs/setup.test.ts renamed to packages/create-plugin/src/codemods/additions/docs-scaffolding.test.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { tmpdir } from 'node:os';
33
import { join } from 'node:path';
44
import { pathToFileURL } from 'node:url';
55
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
6-
import { Context } from '../../../context.js';
7-
import { assertPluginType, setupDocsScaffolding } from './setup.js';
6+
import { Context } from '../context.js';
7+
import { assertPluginType, setupDocsScaffolding } from './docs-scaffolding.js';
88

99
// capture the real existsSync before mocking so we can delegate to it in beforeEach
1010
const { existsSync: realExistsSync } = await vi.importActual<typeof import('node:fs')>('node:fs');
@@ -25,16 +25,14 @@ function makeContext(pluginJson: Record<string, unknown> = { type: 'panel', name
2525
return context;
2626
}
2727

28-
describe('panel-docs/setup', () => {
28+
describe('docs-scaffolding', () => {
2929
const tempDirs: string[] = [];
3030

31-
// build a synthetic templateBaseUrl folder with the given docs template file contents
31+
// build a synthetic plugin-type template folder with the given docs template file contents
3232
function makeTemplateBaseUrl(files: Record<string, string>): URL {
3333
const dir = mkdtempSync(join(tmpdir(), 'panel-docs-templates-'));
3434
tempDirs.push(dir);
3535
mkdirSync(join(dir, 'docs'), { recursive: true });
36-
mkdirSync(join(dir, 'workflows'), { recursive: true });
37-
writeFileSync(join(dir, 'workflows', 'validate-docs.yml'), 'name: Validate documentation\n');
3836
for (const [relPath, content] of Object.entries(files)) {
3937
const target = join(dir, 'docs', relPath);
4038
mkdirSync(join(target, '..'), { recursive: true });
@@ -43,6 +41,15 @@ describe('panel-docs/setup', () => {
4341
return pathToFileURL(`${dir}/`);
4442
}
4543

44+
// the templates every plugin type shares, mirroring templates/docs/common
45+
function makeCommonTemplateBaseUrl(): URL {
46+
const dir = mkdtempSync(join(tmpdir(), 'panel-docs-common-'));
47+
tempDirs.push(dir);
48+
mkdirSync(join(dir, 'workflows'), { recursive: true });
49+
writeFileSync(join(dir, 'workflows', 'validate-docs.yml'), 'name: Validate documentation\n');
50+
return pathToFileURL(`${dir}/`);
51+
}
52+
4653
beforeEach(() => {
4754
vi.mocked(existsSync).mockImplementation(realExistsSync);
4855
});
@@ -62,6 +69,7 @@ describe('panel-docs/setup', () => {
6269
context,
6370
docsPath: overrides.docsPath ?? 'docs',
6471
templateBaseUrl: makeTemplateBaseUrl(templates),
72+
commonTemplateBaseUrl: makeCommonTemplateBaseUrl(),
6573
codemodName: 'panel-docs',
6674
});
6775
}
@@ -192,6 +200,7 @@ describe('panel-docs/setup', () => {
192200
context,
193201
docsPath: 'docs',
194202
templateBaseUrl: pathToFileURL(`${dir}/`),
203+
commonTemplateBaseUrl: makeCommonTemplateBaseUrl(),
195204
codemodName: 'panel-docs',
196205
})
197206
).toThrow(/Cannot find docs templates/);

packages/create-plugin/src/codemods/additions/scripts/panel-docs/setup.ts renamed to packages/create-plugin/src/codemods/additions/docs-scaffolding.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { existsSync, readFileSync, readdirSync } from 'node:fs';
22
import { join } from 'node:path';
33
import { fileURLToPath } from 'node:url';
4-
import type { Context } from '../../../context.js';
5-
import { output } from '../../../../utils/utils.console.js';
6-
import { additionsDebug, addDependenciesToPackageJson, isVersionGreater } from '../../../utils.js';
4+
import type { Context } from '../context.js';
5+
import { output } from '../../utils/utils.console.js';
6+
import { additionsDebug, addDependenciesToPackageJson, isVersionGreater } from '../utils.js';
77

88
const REQUIRED_BUILD_PLUGIN_REF = 'build-plugin/v1.2.0';
99

@@ -17,12 +17,15 @@ export interface PluginJson {
1717
export interface DocsSetupOptions {
1818
context: Context;
1919
docsPath: string;
20+
/** Templates specific to this plugin type, under `templates/docs/<type>/`. */
2021
templateBaseUrl: URL;
22+
/** Templates shared by every plugin type, under `templates/docs/common/`. */
23+
commonTemplateBaseUrl: URL;
2124
codemodName: string;
2225
}
2326

2427
export function setupDocsScaffolding(opts: DocsSetupOptions): Context {
25-
const { context, docsPath, templateBaseUrl, codemodName } = opts;
28+
const { context, docsPath, templateBaseUrl, commonTemplateBaseUrl, codemodName } = opts;
2629

2730
// step 1: early exit if the docs directory already exists on disk
2831
if (existsSync(join(context.basePath, docsPath))) {
@@ -56,7 +59,7 @@ export function setupDocsScaffolding(opts: DocsSetupOptions): Context {
5659
// step 6: copy validate-docs workflow, unless the user already customized one
5760
const workflowPath = '.github/workflows/validate-docs.yml';
5861
if (!context.doesFileExist(workflowPath)) {
59-
const workflowContent = readTemplate(templateBaseUrl, 'workflows/validate-docs.yml').replaceAll(
62+
const workflowContent = readTemplate(commonTemplateBaseUrl, 'workflows/validate-docs.yml').replaceAll(
6063
'{{docsPath}}',
6164
docsPath
6265
);

packages/create-plugin/src/codemods/additions/scripts/panel-docs/index.test.ts renamed to packages/create-plugin/src/codemods/additions/scripts/panel-docs.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as v from 'valibot';
22
import { describe, expect, it } from 'vitest';
3-
import { Context } from '../../../context.js';
4-
import panelDocs, { schema } from './index.js';
3+
import { Context } from '../../context.js';
4+
import panelDocs, { schema } from './panel-docs.js';
55

66
function makeContext(): Context {
77
const context = new Context('/virtual');
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import * as v from 'valibot';
2+
import type { Context } from '../../context.js';
3+
import { assertPluginType, setupDocsScaffolding } from '../docs-scaffolding.js';
4+
5+
export const schema = v.object({
6+
docsPath: v.optional(
7+
v.pipe(
8+
v.string(),
9+
v.minLength(1, 'docsPath must not be empty.'),
10+
v.check(
11+
(value) => !value.startsWith('/') && !value.split('/').includes('..'),
12+
'docsPath must be a relative path without ".." segments.'
13+
)
14+
),
15+
'docs'
16+
),
17+
});
18+
19+
type Options = v.InferOutput<typeof schema>;
20+
21+
export default function panelDocs(context: Context, options: Options): Context {
22+
assertPluginType(context, { expectedType: 'panel', codemodName: 'panel-docs' });
23+
return setupDocsScaffolding({
24+
context,
25+
docsPath: options.docsPath,
26+
// templates live in the package-root `templates/` folder alongside every other template set,
27+
// split by plugin type so a future datasource-docs codemod reuses `docs/common/`. this file sits
28+
// four levels below the package root in both `src/` and `dist/` - rollup mirrors the tree - so
29+
// the same relative URL resolves from source and from the built package with no copy step.
30+
// `templates/docs` is deliberately absent from TEMPLATE_PATHS, so `generate` ignores it until we
31+
// scaffold docs for every new plugin.
32+
templateBaseUrl: new URL('../../../../templates/docs/panel/', import.meta.url),
33+
commonTemplateBaseUrl: new URL('../../../../templates/docs/common/', import.meta.url),
34+
codemodName: 'panel-docs',
35+
});
36+
}

packages/create-plugin/src/codemods/additions/scripts/panel-docs/index.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

packages/create-plugin/src/codemods/additions/scripts/panel-docs/templates/workflows/validate-docs.yml renamed to packages/create-plugin/templates/docs/common/workflows/validate-docs.yml

File renamed without changes.

packages/create-plugin/src/codemods/additions/scripts/panel-docs/templates/docs/README.md renamed to packages/create-plugin/templates/docs/panel/docs/README.md

File renamed without changes.

packages/create-plugin/src/codemods/additions/scripts/panel-docs/templates/docs/data-formats.md renamed to packages/create-plugin/templates/docs/panel/docs/data-formats.md

File renamed without changes.

packages/create-plugin/src/codemods/additions/scripts/panel-docs/templates/docs/examples.md renamed to packages/create-plugin/templates/docs/panel/docs/examples.md

File renamed without changes.

0 commit comments

Comments
 (0)