@grafana/create-plugin 6.2.0-canary.2233.19097561440.0 → 6.2.0-canary.2233.19368311379.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/CHANGELOG.md +61 -0
  2. package/CONTRIBUTING.md +3 -0
  3. package/dist/codemods/additions/additions.js +8 -8
  4. package/dist/codemods/additions/scripts/example-addition.js +47 -0
  5. package/dist/codemods/migrations/manager.js +13 -40
  6. package/dist/codemods/migrations/migrations.js +34 -25
  7. package/dist/codemods/migrations/scripts/004-eslint9-flat-config.js +1 -2
  8. package/dist/codemods/migrations/scripts/005-react-18-3.js +20 -0
  9. package/dist/codemods/migrations/scripts/example-migration.js +7 -3
  10. package/dist/codemods/runner.js +38 -0
  11. package/dist/codemods/schema-parser.js +20 -0
  12. package/dist/codemods/utils.js +15 -6
  13. package/dist/commands/add.command.js +24 -55
  14. package/dist/commands/update.command.js +7 -41
  15. package/dist/utils/utils.checks.js +40 -0
  16. package/dist/utils/utils.config.js +1 -16
  17. package/package.json +3 -2
  18. package/src/codemods/additions/additions.test.ts +12 -0
  19. package/src/codemods/additions/additions.ts +9 -22
  20. package/src/codemods/additions/scripts/example-addition.test.ts +92 -0
  21. package/src/codemods/additions/scripts/example-addition.ts +62 -0
  22. package/src/codemods/migrations/fixtures/migrations.ts +19 -18
  23. package/src/codemods/migrations/manager.test.ts +67 -73
  24. package/src/codemods/migrations/manager.ts +17 -50
  25. package/src/codemods/migrations/migrations.test.ts +8 -5
  26. package/src/codemods/migrations/migrations.ts +38 -34
  27. package/src/codemods/migrations/scripts/004-eslint9-flat-config.ts +2 -2
  28. package/src/codemods/migrations/scripts/005-react-18-3.test.ts +145 -0
  29. package/src/codemods/migrations/scripts/005-react-18-3.ts +19 -0
  30. package/src/codemods/migrations/scripts/example-migration.test.ts +1 -1
  31. package/src/codemods/migrations/scripts/example-migration.ts +20 -3
  32. package/src/codemods/runner.ts +57 -0
  33. package/src/codemods/schema-parser.ts +27 -0
  34. package/src/codemods/types.ts +9 -14
  35. package/src/codemods/{migrations/utils.test.ts → utils.test.ts} +8 -7
  36. package/src/codemods/utils.ts +28 -36
  37. package/src/commands/add.command.ts +26 -62
  38. package/src/commands/update.command.ts +8 -47
  39. package/src/migrations/migrations.ts +44 -0
  40. package/src/utils/utils.checks.ts +47 -0
  41. package/src/utils/utils.config.ts +1 -28
  42. package/templates/common/_package.json +7 -5
  43. package/templates/github/workflows/bundle-stats.yml +1 -1
  44. package/templates/github/workflows/ci.yml +11 -11
  45. package/templates/github/workflows/cp-update.yml +9 -14
  46. package/templates/github/workflows/is-compatible.yml +3 -3
  47. package/templates/github/workflows/release.yml +1 -1
  48. package/vitest.config.ts +12 -0
  49. package/dist/codemods/additions/manager.js +0 -115
  50. package/dist/codemods/additions/scripts/add-i18n.js +0 -445
  51. package/dist/codemods/additions/utils.js +0 -10
  52. package/dist/codemods/migrations/utils.js +0 -10
  53. package/src/codemods/additions/manager.ts +0 -145
  54. package/src/codemods/additions/scripts/add-i18n.test.ts +0 -347
  55. package/src/codemods/additions/scripts/add-i18n.ts +0 -584
  56. package/src/codemods/additions/utils.ts +0 -12
  57. package/src/codemods/migrations/utils.ts +0 -12
@@ -1,145 +0,0 @@
1
- import type { AdditionModule, FlagDefinition } from '../types.js';
2
- import { additionsDebug, printChanges } from './utils.js';
3
- import defaultAdditions, { AdditionMeta } from './additions.js';
4
- import { flushChanges, formatFiles, installNPMDependencies } from '../utils.js';
5
- import { getConfig, isFeatureEnabled, setFeatureFlag } from '../../utils/utils.config.js';
6
-
7
- import { Context } from '../context.js';
8
- import { output } from '../../utils/utils.console.js';
9
-
10
- export type AdditionOptions = Record<string, any>;
11
-
12
- export function getAvailableAdditions(
13
- additions: Record<string, AdditionMeta> = defaultAdditions.additions
14
- ): Record<string, AdditionMeta> {
15
- return additions;
16
- }
17
-
18
- export function getAdditionByName(
19
- name: string,
20
- additions: Record<string, AdditionMeta> = defaultAdditions.additions
21
- ): AdditionMeta | undefined {
22
- return additions[name];
23
- }
24
-
25
- async function loadAdditionModule(addition: AdditionMeta): Promise<AdditionModule | null> {
26
- try {
27
- const module = (await import(addition.scriptPath)) as AdditionModule;
28
- return module;
29
- } catch (error) {
30
- additionsDebug('Failed to load addition module for "%s" from %s: %O', addition.name, addition.scriptPath, error);
31
- return null;
32
- }
33
- }
34
-
35
- export async function getAdditionFlags(addition: AdditionMeta): Promise<FlagDefinition[]> {
36
- const module = await loadAdditionModule(addition);
37
- return module?.flags || [];
38
- }
39
-
40
- export async function parseAdditionFlags(addition: AdditionMeta, argv: any): Promise<AdditionOptions> {
41
- const module = await loadAdditionModule(addition);
42
- if (module?.parseFlags) {
43
- return module.parseFlags(argv);
44
- }
45
- return {};
46
- }
47
-
48
- async function validateAdditionOptions(addition: AdditionMeta, options: AdditionOptions): Promise<void> {
49
- const flags = await getAdditionFlags(addition);
50
-
51
- if (!flags || flags.length === 0) {
52
- return;
53
- }
54
-
55
- const missingFlags: string[] = [];
56
-
57
- for (const flag of flags) {
58
- if (flag.required) {
59
- const value = options[flag.name];
60
- if (value === undefined || value === null || (Array.isArray(value) && value.length === 0)) {
61
- missingFlags.push(flag.name);
62
- }
63
- }
64
- }
65
-
66
- if (missingFlags.length > 0) {
67
- const flagDocs = flags.filter((f) => missingFlags.includes(f.name)).map((f) => ` --${f.name}: ${f.description}`);
68
-
69
- throw new Error(
70
- `Missing required flag${missingFlags.length > 1 ? 's' : ''}:\n\n` +
71
- flagDocs.join('\n') +
72
- `\n\nExample: npx @grafana/create-plugin add ${addition.name} --${missingFlags[0]}=value`
73
- );
74
- }
75
- }
76
-
77
- export async function runAdditionByName(additionName: string, argv: any): Promise<void> {
78
- const addition = getAdditionByName(additionName);
79
- if (!addition) {
80
- const availableAdditions = getAvailableAdditions();
81
- const additionsList = Object.keys(availableAdditions);
82
- throw new Error(`Unknown addition: ${additionName}\n\nAvailable additions: ${additionsList.join(', ')}`);
83
- }
84
-
85
- const options = await parseAdditionFlags(addition, argv);
86
- await validateAdditionOptions(addition, options);
87
- await runAddition(addition, options);
88
- }
89
-
90
- export async function runAddition(addition: AdditionMeta, additionOptions: AdditionOptions = {}): Promise<void> {
91
- const basePath = process.cwd();
92
-
93
- // Check if the feature is already enabled
94
- const config = getConfig();
95
- if (isFeatureEnabled(config.features, addition.featureName)) {
96
- output.log({
97
- title: `Addition '${addition.name}' is already enabled`,
98
- body: [`The feature flag '${addition.featureName}' is already set to true in .cprc.json.`, 'No changes needed.'],
99
- });
100
- return;
101
- }
102
-
103
- output.log({
104
- title: `Running addition: ${addition.name}`,
105
- body: [addition.description],
106
- });
107
-
108
- try {
109
- const context = new Context(basePath);
110
- const updatedContext = await executeAddition(addition, context, additionOptions);
111
-
112
- additionsDebug(`context for "${addition.name} (${addition.scriptPath})":`);
113
- additionsDebug('%O', updatedContext.listChanges());
114
-
115
- await formatFiles(updatedContext);
116
- flushChanges(updatedContext);
117
- printChanges(updatedContext, addition.name, addition);
118
-
119
- installNPMDependencies(updatedContext);
120
-
121
- await setFeatureFlag(addition.featureName, true);
122
- additionsDebug(`Set feature flag '${addition.featureName}' to true in .cprc.json`);
123
-
124
- output.success({
125
- title: `Successfully added ${addition.name} to your plugin.`,
126
- });
127
- } catch (error) {
128
- if (error instanceof Error) {
129
- throw new Error(`Error running addition "${addition.name} (${addition.scriptPath})": ${error.message}`);
130
- }
131
- throw error;
132
- }
133
- }
134
-
135
- export async function executeAddition(
136
- addition: AdditionMeta,
137
- context: Context,
138
- options: AdditionOptions = {}
139
- ): Promise<Context> {
140
- const module = await loadAdditionModule(addition);
141
- if (!module) {
142
- throw new Error(`Failed to load addition module for ${addition.name}`);
143
- }
144
- return module.default(context, options);
145
- }
@@ -1,347 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
-
3
- import { Context } from '../../context.js';
4
- import migrate from './add-i18n.js';
5
-
6
- describe('add-i18n', () => {
7
- it('should be idempotent', async () => {
8
- const context = new Context('/virtual');
9
-
10
- // Set up a minimal plugin structure
11
- context.addFile(
12
- 'src/plugin.json',
13
- JSON.stringify({
14
- id: 'test-plugin',
15
- type: 'panel',
16
- name: 'Test Plugin',
17
- dependencies: {
18
- grafanaDependency: '>=11.0.0',
19
- },
20
- })
21
- );
22
- context.addFile('docker-compose.yaml', 'services:\n grafana:\n environment:\n FOO: bar');
23
- context.addFile('package.json', JSON.stringify({ dependencies: {}, devDependencies: {} }));
24
- context.addFile(
25
- 'eslint.config.mjs',
26
- 'import { defineConfig } from "eslint/config";\nexport default defineConfig([]);'
27
- );
28
- context.addFile(
29
- 'src/module.ts',
30
- 'import { PanelPlugin } from "@grafana/data";\nexport const plugin = new PanelPlugin();'
31
- );
32
-
33
- const migrateWithOptions = (ctx: Context) => migrate(ctx, { locales: ['en-US'] });
34
- await expect(migrateWithOptions).toBeIdempotent(context);
35
- });
36
-
37
- it('should add i18n support with a single locale (backward compatibility for Grafana < 12.1.0)', () => {
38
- const context = new Context('/virtual');
39
-
40
- // Set up a minimal plugin structure with Grafana 11.0.0 (needs backward compatibility)
41
- context.addFile(
42
- 'src/plugin.json',
43
- JSON.stringify({
44
- id: 'test-plugin',
45
- type: 'panel',
46
- name: 'Test Plugin',
47
- dependencies: {
48
- grafanaDependency: '>=11.0.0',
49
- },
50
- })
51
- );
52
- context.addFile(
53
- 'docker-compose.yaml',
54
- `services:
55
- grafana:
56
- environment:
57
- FOO: bar`
58
- );
59
- context.addFile('package.json', JSON.stringify({ dependencies: {}, devDependencies: {}, scripts: {} }));
60
- context.addFile(
61
- 'eslint.config.mjs',
62
- 'import { defineConfig } from "eslint/config";\nexport default defineConfig([]);'
63
- );
64
- context.addFile(
65
- 'src/module.ts',
66
- 'import { PanelPlugin } from "@grafana/data";\nexport const plugin = new PanelPlugin();'
67
- );
68
-
69
- const result = migrate(context, { locales: ['en-US'] });
70
-
71
- // Check plugin.json was updated
72
- const pluginJson = JSON.parse(result.getFile('src/plugin.json') || '{}');
73
- expect(pluginJson.languages).toEqual(['en-US']);
74
- // Should stay at 11.0.0 for backward compatibility
75
- expect(pluginJson.dependencies.grafanaDependency).toBe('>=11.0.0');
76
-
77
- // Check locale file was created with example translations
78
- expect(result.doesFileExist('src/locales/en-US/test-plugin.json')).toBe(true);
79
- const localeContent = result.getFile('src/locales/en-US/test-plugin.json');
80
- const localeData = JSON.parse(localeContent || '{}');
81
- expect(localeData).toHaveProperty('components');
82
- expect(localeData).toHaveProperty('config');
83
-
84
- // Check package.json was updated with dependencies
85
- const packageJson = JSON.parse(result.getFile('package.json') || '{}');
86
- expect(packageJson.dependencies['@grafana/i18n']).toBe('12.2.2');
87
- expect(packageJson.dependencies['semver']).toBe('^7.6.0');
88
- expect(packageJson.devDependencies['@types/semver']).toBe('^7.5.0');
89
- expect(packageJson.devDependencies['i18next-cli']).toBeDefined();
90
- expect(packageJson.scripts['i18n-extract']).toBe('i18next-cli extract --sync-primary');
91
-
92
- // Check docker-compose.yaml was NOT updated (backward compat doesn't add feature toggle)
93
- const dockerCompose = result.getFile('docker-compose.yaml');
94
- expect(dockerCompose).not.toContain('localizationForPlugins');
95
-
96
- // Check module.ts was updated with backward compatibility code
97
- const moduleTs = result.getFile('src/module.ts');
98
- expect(moduleTs).toContain('initPluginTranslations');
99
- expect(moduleTs).toContain('semver');
100
- expect(moduleTs).toContain('loadResources');
101
-
102
- // Check loadResources.ts was created for backward compatibility
103
- expect(result.doesFileExist('src/loadResources.ts')).toBe(true);
104
- const loadResources = result.getFile('src/loadResources.ts');
105
- expect(loadResources).toContain('ResourceLoader');
106
-
107
- // Check i18next.config.ts was created
108
- expect(result.doesFileExist('i18next.config.ts')).toBe(true);
109
- const i18nextConfig = result.getFile('i18next.config.ts');
110
- expect(i18nextConfig).toContain('defineConfig');
111
- expect(i18nextConfig).toContain('pluginJson.id');
112
- });
113
-
114
- it('should add i18n support with multiple locales', () => {
115
- const context = new Context('/virtual');
116
-
117
- context.addFile(
118
- 'src/plugin.json',
119
- JSON.stringify({
120
- id: 'test-plugin',
121
- type: 'panel',
122
- name: 'Test Plugin',
123
- dependencies: {
124
- grafanaDependency: '>=11.0.0',
125
- },
126
- })
127
- );
128
- context.addFile(
129
- 'docker-compose.yaml',
130
- `services:
131
- grafana:
132
- environment:
133
- FOO: bar`
134
- );
135
- context.addFile('package.json', JSON.stringify({ dependencies: {}, devDependencies: {}, scripts: {} }));
136
- context.addFile(
137
- 'eslint.config.mjs',
138
- 'import { defineConfig } from "eslint/config";\nexport default defineConfig([]);'
139
- );
140
- context.addFile(
141
- 'src/module.ts',
142
- 'import { PanelPlugin } from "@grafana/data";\nexport const plugin = new PanelPlugin();'
143
- );
144
-
145
- const result = migrate(context, { locales: ['en-US', 'es-ES', 'sv-SE'] });
146
-
147
- // Check plugin.json has all locales
148
- const pluginJson = JSON.parse(result.getFile('src/plugin.json') || '{}');
149
- expect(pluginJson.languages).toEqual(['en-US', 'es-ES', 'sv-SE']);
150
-
151
- // Check all locale files were created
152
- expect(result.doesFileExist('src/locales/en-US/test-plugin.json')).toBe(true);
153
- expect(result.doesFileExist('src/locales/es-ES/test-plugin.json')).toBe(true);
154
- expect(result.doesFileExist('src/locales/sv-SE/test-plugin.json')).toBe(true);
155
- });
156
-
157
- it('should skip if i18n is already configured', () => {
158
- const context = new Context('/virtual');
159
-
160
- // Set up a plugin with i18n already configured
161
- context.addFile(
162
- 'src/plugin.json',
163
- JSON.stringify({
164
- id: 'test-plugin',
165
- type: 'panel',
166
- name: 'Test Plugin',
167
- languages: ['en-US'], // Already configured
168
- dependencies: {
169
- grafanaDependency: '>=12.1.0',
170
- },
171
- })
172
- );
173
- context.addFile(
174
- 'docker-compose.yaml',
175
- `services:
176
- grafana:
177
- environment:
178
- FOO: bar`
179
- );
180
- context.addFile('package.json', JSON.stringify({ dependencies: {}, devDependencies: {}, scripts: {} }));
181
- context.addFile(
182
- 'eslint.config.mjs',
183
- 'import { defineConfig } from "eslint/config";\nexport default defineConfig([]);'
184
- );
185
- context.addFile(
186
- 'src/module.ts',
187
- 'import { PanelPlugin } from "@grafana/data";\nimport { i18n } from "@grafana/i18n";\nexport const plugin = new PanelPlugin();'
188
- );
189
-
190
- // Flush the context to simulate these files existing on "disk"
191
- const initialChanges = Object.keys(context.listChanges()).length;
192
-
193
- const result = migrate(context, { locales: ['es-ES'] });
194
-
195
- // Should not add any NEW changes beyond the initial setup
196
- const finalChanges = Object.keys(result.listChanges()).length;
197
- expect(finalChanges).toBe(initialChanges);
198
- });
199
-
200
- it('should handle existing feature toggles in docker-compose.yaml (Grafana >= 12.1.0)', () => {
201
- const context = new Context('/virtual');
202
-
203
- context.addFile(
204
- 'src/plugin.json',
205
- JSON.stringify({
206
- id: 'test-plugin',
207
- type: 'panel',
208
- name: 'Test Plugin',
209
- dependencies: {
210
- grafanaDependency: '>=12.1.0',
211
- },
212
- })
213
- );
214
- context.addFile(
215
- 'docker-compose.yaml',
216
- `services:
217
- grafana:
218
- environment:
219
- GF_FEATURE_TOGGLES_ENABLE: someOtherFeature`
220
- );
221
- context.addFile('package.json', JSON.stringify({ dependencies: {}, devDependencies: {}, scripts: {} }));
222
- context.addFile(
223
- 'eslint.config.mjs',
224
- 'import { defineConfig } from "eslint/config";\nexport default defineConfig([]);'
225
- );
226
- context.addFile(
227
- 'src/module.ts',
228
- 'import { PanelPlugin } from "@grafana/data";\nexport const plugin = new PanelPlugin();'
229
- );
230
-
231
- const result = migrate(context, { locales: ['en-US'] });
232
-
233
- const dockerCompose = result.getFile('docker-compose.yaml');
234
- expect(dockerCompose).toContain('someOtherFeature,localizationForPlugins');
235
- });
236
-
237
- it('should work with module.tsx instead of module.ts', () => {
238
- const context = new Context('/virtual');
239
-
240
- context.addFile(
241
- 'src/plugin.json',
242
- JSON.stringify({
243
- id: 'test-plugin',
244
- type: 'panel',
245
- name: 'Test Plugin',
246
- dependencies: {
247
- grafanaDependency: '>=11.0.0',
248
- },
249
- })
250
- );
251
- context.addFile(
252
- 'docker-compose.yaml',
253
- `services:
254
- grafana:
255
- environment:
256
- FOO: bar`
257
- );
258
- context.addFile('package.json', JSON.stringify({ dependencies: {}, devDependencies: {}, scripts: {} }));
259
- context.addFile(
260
- 'eslint.config.mjs',
261
- 'import { defineConfig } from "eslint/config";\nexport default defineConfig([]);'
262
- );
263
- context.addFile(
264
- 'src/module.tsx',
265
- 'import { PanelPlugin } from "@grafana/data";\nexport const plugin = new PanelPlugin();'
266
- );
267
-
268
- const result = migrate(context, { locales: ['en-US'] });
269
-
270
- const moduleTsx = result.getFile('src/module.tsx');
271
- expect(moduleTsx).toContain('@grafana/i18n');
272
- });
273
-
274
- it('should not update grafanaDependency if it is already >= 12.1.0', () => {
275
- const context = new Context('/virtual');
276
-
277
- context.addFile(
278
- 'src/plugin.json',
279
- JSON.stringify({
280
- id: 'test-plugin',
281
- type: 'panel',
282
- name: 'Test Plugin',
283
- dependencies: {
284
- grafanaDependency: '>=13.0.0',
285
- },
286
- })
287
- );
288
- context.addFile(
289
- 'docker-compose.yaml',
290
- `services:
291
- grafana:
292
- environment:
293
- FOO: bar`
294
- );
295
- context.addFile('package.json', JSON.stringify({ dependencies: {}, devDependencies: {}, scripts: {} }));
296
- context.addFile(
297
- 'eslint.config.mjs',
298
- 'import { defineConfig } from "eslint/config";\nexport default defineConfig([]);'
299
- );
300
- context.addFile(
301
- 'src/module.ts',
302
- 'import { PanelPlugin } from "@grafana/data";\nexport const plugin = new PanelPlugin();'
303
- );
304
-
305
- const result = migrate(context, { locales: ['en-US'] });
306
-
307
- const pluginJson = JSON.parse(result.getFile('src/plugin.json') || '{}');
308
- expect(pluginJson.dependencies.grafanaDependency).toBe('>=13.0.0');
309
- });
310
-
311
- it('should handle plugins without existing scripts in package.json', () => {
312
- const context = new Context('/virtual');
313
-
314
- context.addFile(
315
- 'src/plugin.json',
316
- JSON.stringify({
317
- id: 'test-plugin',
318
- type: 'panel',
319
- name: 'Test Plugin',
320
- dependencies: {
321
- grafanaDependency: '>=11.0.0',
322
- },
323
- })
324
- );
325
- context.addFile(
326
- 'docker-compose.yaml',
327
- `services:
328
- grafana:
329
- environment:
330
- FOO: bar`
331
- );
332
- context.addFile('package.json', JSON.stringify({ dependencies: {}, devDependencies: {} })); // No scripts field
333
- context.addFile(
334
- 'eslint.config.mjs',
335
- 'import { defineConfig } from "eslint/config";\nexport default defineConfig([]);'
336
- );
337
- context.addFile(
338
- 'src/module.ts',
339
- 'import { PanelPlugin } from "@grafana/data";\nexport const plugin = new PanelPlugin();'
340
- );
341
-
342
- const result = migrate(context, { locales: ['en-US'] });
343
-
344
- const packageJson = JSON.parse(result.getFile('package.json') || '{}');
345
- expect(packageJson.scripts['i18n-extract']).toBe('i18next-cli extract --sync-primary');
346
- });
347
- });