@grafana/create-plugin 6.2.0-canary.2233.19097561440.0 → 6.2.0-canary.2283.19173715977.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 (60) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/CONTRIBUTING.md +3 -0
  3. package/dist/bin/run.js +1 -3
  4. package/dist/commands/update.command.js +1 -1
  5. package/dist/{codemods → migrations}/context.js +2 -3
  6. package/dist/{codemods/migrations → migrations}/manager.js +7 -8
  7. package/dist/{codemods/migrations → migrations}/migrations.js +6 -3
  8. package/dist/{codemods/migrations → migrations}/scripts/003-update-eslint-deprecation-rule.js +1 -1
  9. package/dist/{codemods/migrations → migrations}/scripts/004-eslint9-flat-config.js +1 -2
  10. package/dist/migrations/scripts/005-react-18-3.js +20 -0
  11. package/dist/{codemods → migrations}/utils.js +22 -5
  12. package/dist/utils/utils.config.js +1 -16
  13. package/package.json +2 -2
  14. package/src/bin/run.ts +1 -2
  15. package/src/commands/index.ts +0 -1
  16. package/src/commands/update.command.ts +1 -1
  17. package/src/{codemods → migrations}/context.test.ts +10 -10
  18. package/src/{codemods → migrations}/context.ts +2 -4
  19. package/src/{codemods/migrations → migrations}/manager.test.ts +10 -15
  20. package/src/{codemods/migrations → migrations}/manager.ts +9 -10
  21. package/src/{codemods/migrations → migrations}/migrations.ts +8 -3
  22. package/src/{codemods/migrations → migrations}/scripts/001-update-grafana-compose-extend.test.ts +1 -1
  23. package/src/{codemods/migrations → migrations}/scripts/001-update-grafana-compose-extend.ts +1 -1
  24. package/src/{codemods/migrations → migrations}/scripts/002-update-is-compatible-workflow.test.ts +1 -1
  25. package/src/{codemods/migrations → migrations}/scripts/002-update-is-compatible-workflow.ts +1 -1
  26. package/src/{codemods/migrations → migrations}/scripts/003-update-eslint-deprecation-rule.test.ts +1 -1
  27. package/src/{codemods/migrations → migrations}/scripts/003-update-eslint-deprecation-rule.ts +2 -2
  28. package/src/{codemods/migrations → migrations}/scripts/004-eslint9-flat-config.test.ts +1 -1
  29. package/src/{codemods/migrations → migrations}/scripts/004-eslint9-flat-config.ts +2 -3
  30. package/src/migrations/scripts/005-react-18-3.test.ts +147 -0
  31. package/src/migrations/scripts/005-react-18-3.ts +19 -0
  32. package/src/{codemods/migrations → migrations}/scripts/example-migration.test.ts +1 -1
  33. package/src/{codemods/migrations → migrations}/scripts/example-migration.ts +1 -1
  34. package/src/{codemods/migrations → migrations}/utils.test.ts +4 -4
  35. package/src/{codemods → migrations}/utils.ts +33 -37
  36. package/src/utils/utils.config.ts +1 -28
  37. package/templates/common/_package.json +5 -3
  38. package/templates/github/workflows/cp-update.yml +8 -13
  39. package/dist/codemods/additions/additions.js +0 -11
  40. package/dist/codemods/additions/manager.js +0 -115
  41. package/dist/codemods/additions/scripts/add-i18n.js +0 -445
  42. package/dist/codemods/additions/utils.js +0 -10
  43. package/dist/codemods/migrations/utils.js +0 -10
  44. package/dist/commands/add.command.js +0 -86
  45. package/src/codemods/additions/additions.ts +0 -23
  46. package/src/codemods/additions/manager.ts +0 -145
  47. package/src/codemods/additions/scripts/add-i18n.test.ts +0 -347
  48. package/src/codemods/additions/scripts/add-i18n.ts +0 -584
  49. package/src/codemods/additions/utils.ts +0 -12
  50. package/src/codemods/migrations/utils.ts +0 -12
  51. package/src/codemods/types.ts +0 -21
  52. package/src/commands/add.command.ts +0 -97
  53. /package/dist/{codemods/migrations → migrations}/scripts/001-update-grafana-compose-extend.js +0 -0
  54. /package/dist/{codemods/migrations → migrations}/scripts/002-update-is-compatible-workflow.js +0 -0
  55. /package/dist/{codemods/migrations → migrations}/scripts/example-migration.js +0 -0
  56. /package/src/{codemods/migrations → migrations}/fixtures/foo/bar.ts +0 -0
  57. /package/src/{codemods/migrations → migrations}/fixtures/foo/baz.ts +0 -0
  58. /package/src/{codemods/migrations → migrations}/fixtures/migrations.ts +0 -0
  59. /package/src/{codemods/migrations → migrations}/migrations.test.ts +0 -0
  60. /package/src/{codemods → migrations}/test-utils.ts +0 -0
@@ -1,584 +0,0 @@
1
- import * as recast from 'recast';
2
-
3
- import { coerce, gte } from 'semver';
4
- import { parseDocument, stringify } from 'yaml';
5
-
6
- import type { Context } from '../../context.js';
7
- import type { FlagDefinition } from '../../types.js';
8
- import { addDependenciesToPackageJson } from '../../utils.js';
9
- import { additionsDebug } from '../utils.js';
10
-
11
- const { builders } = recast.types;
12
-
13
- export type I18nOptions = {
14
- locales: string[];
15
- };
16
-
17
- export const flags: FlagDefinition[] = [
18
- {
19
- name: 'locales',
20
- description: 'Comma-separated list of locales (e.g., en-US,es-ES)',
21
- required: true,
22
- },
23
- ];
24
-
25
- export function parseFlags(argv: any): I18nOptions {
26
- return {
27
- locales: argv.locales ? argv.locales.split(',').map((l: string) => l.trim()) : [],
28
- };
29
- }
30
-
31
- export default function migrate(context: Context, options: I18nOptions = { locales: ['en-US'] }): Context {
32
- const { locales } = options;
33
-
34
- additionsDebug('Adding i18n support with locales:', locales);
35
-
36
- // Check if i18n is already configured
37
- if (isI18nConfigured(context)) {
38
- additionsDebug('i18n already configured, skipping');
39
- return context;
40
- }
41
-
42
- // Determine if we need backward compatibility (Grafana < 12.1.0)
43
- const needsBackwardCompatibility = checkNeedsBackwardCompatibility(context);
44
- additionsDebug('Needs backward compatibility:', needsBackwardCompatibility);
45
-
46
- // 1. Update docker-compose.yaml with feature toggle (only if >= 12.1.0)
47
- if (!needsBackwardCompatibility) {
48
- updateDockerCompose(context);
49
- }
50
-
51
- // 2. Update plugin.json with languages and grafanaDependency
52
- updatePluginJson(context, locales, needsBackwardCompatibility);
53
-
54
- // 3. Create locale folders and files with example translations
55
- createLocaleFiles(context, locales);
56
-
57
- // 4. Add @grafana/i18n dependency
58
- addI18nDependency(context);
59
-
60
- // 5. Add semver dependency for backward compatibility
61
- if (needsBackwardCompatibility) {
62
- addSemverDependency(context);
63
- }
64
-
65
- // 6. Update eslint.config.mjs if needed
66
- updateEslintConfig(context);
67
-
68
- // 7. Add i18n initialization to module file
69
- addI18nInitialization(context, needsBackwardCompatibility);
70
-
71
- // 8. Create loadResources.ts for backward compatibility
72
- if (needsBackwardCompatibility) {
73
- createLoadResourcesFile(context);
74
- }
75
-
76
- // 9. Add i18next-cli as dev dependency and add script
77
- addI18nextCli(context);
78
-
79
- // 10. Create i18next.config.ts
80
- createI18nextConfig(context);
81
-
82
- return context;
83
- }
84
-
85
- function isI18nConfigured(context: Context): boolean {
86
- // Check if plugin.json has languages field
87
- if (!context.doesFileExist('src/plugin.json')) {
88
- return false;
89
- }
90
-
91
- const pluginJsonRaw = context.getFile('src/plugin.json');
92
- if (!pluginJsonRaw) {
93
- return false;
94
- }
95
-
96
- try {
97
- const pluginJson = JSON.parse(pluginJsonRaw);
98
- if (pluginJson.languages && Array.isArray(pluginJson.languages) && pluginJson.languages.length > 0) {
99
- additionsDebug('Found languages in plugin.json, i18n already configured');
100
- return true;
101
- }
102
- } catch (error) {
103
- additionsDebug('Error parsing plugin.json:', error);
104
- }
105
-
106
- return false;
107
- }
108
-
109
- function checkNeedsBackwardCompatibility(context: Context): boolean {
110
- const pluginJsonRaw = context.getFile('src/plugin.json');
111
- if (!pluginJsonRaw) {
112
- return false;
113
- }
114
-
115
- try {
116
- const pluginJson = JSON.parse(pluginJsonRaw);
117
- const currentGrafanaDep = pluginJson.dependencies?.grafanaDependency || '>=11.0.0';
118
- const minVersion = coerce('12.1.0');
119
- const currentVersion = coerce(currentGrafanaDep.replace(/^[><=]+/, ''));
120
-
121
- // If current version is less than 12.1.0, we need backward compatibility
122
- if (currentVersion && minVersion && gte(currentVersion, minVersion)) {
123
- return false; // Already >= 12.1.0, no backward compat needed
124
- }
125
- return true; // < 12.1.0, needs backward compat
126
- } catch (error) {
127
- additionsDebug('Error checking backward compatibility:', error);
128
- return true; // Default to backward compat on error
129
- }
130
- }
131
-
132
- function updateDockerCompose(context: Context): void {
133
- if (!context.doesFileExist('docker-compose.yaml')) {
134
- additionsDebug('docker-compose.yaml not found, skipping');
135
- return;
136
- }
137
-
138
- const composeContent = context.getFile('docker-compose.yaml');
139
- if (!composeContent) {
140
- return;
141
- }
142
-
143
- try {
144
- const composeDoc = parseDocument(composeContent);
145
- const currentEnv = composeDoc.getIn(['services', 'grafana', 'environment']);
146
-
147
- if (!currentEnv) {
148
- additionsDebug('No environment section found in docker-compose.yaml, skipping');
149
- return;
150
- }
151
-
152
- // Check if the feature toggle is already set
153
- if (typeof currentEnv === 'object') {
154
- const envMap = currentEnv as any;
155
- const toggleValue = envMap.get('GF_FEATURE_TOGGLES_ENABLE');
156
-
157
- if (toggleValue) {
158
- const toggleStr = toggleValue.toString();
159
- if (toggleStr.includes('localizationForPlugins')) {
160
- additionsDebug('localizationForPlugins already in GF_FEATURE_TOGGLES_ENABLE');
161
- return;
162
- }
163
- // Append to existing feature toggles
164
- composeDoc.setIn(
165
- ['services', 'grafana', 'environment', 'GF_FEATURE_TOGGLES_ENABLE'],
166
- `${toggleStr},localizationForPlugins`
167
- );
168
- } else {
169
- // Set new feature toggle
170
- composeDoc.setIn(['services', 'grafana', 'environment', 'GF_FEATURE_TOGGLES_ENABLE'], 'localizationForPlugins');
171
- }
172
-
173
- context.updateFile('docker-compose.yaml', stringify(composeDoc));
174
- additionsDebug('Updated docker-compose.yaml with localizationForPlugins feature toggle');
175
- }
176
- } catch (error) {
177
- additionsDebug('Error updating docker-compose.yaml:', error);
178
- }
179
- }
180
-
181
- function updatePluginJson(context: Context, locales: string[], needsBackwardCompatibility: boolean): void {
182
- if (!context.doesFileExist('src/plugin.json')) {
183
- additionsDebug('src/plugin.json not found, skipping');
184
- return;
185
- }
186
-
187
- const pluginJsonRaw = context.getFile('src/plugin.json');
188
- if (!pluginJsonRaw) {
189
- return;
190
- }
191
-
192
- try {
193
- const pluginJson = JSON.parse(pluginJsonRaw);
194
-
195
- // Add languages array
196
- pluginJson.languages = locales;
197
-
198
- // Update grafanaDependency based on backward compatibility needs
199
- if (!pluginJson.dependencies) {
200
- pluginJson.dependencies = {};
201
- }
202
-
203
- const currentGrafanaDep = pluginJson.dependencies.grafanaDependency || '>=11.0.0';
204
- const targetVersion = needsBackwardCompatibility ? '11.0.0' : '12.1.0';
205
- const minVersion = coerce(targetVersion);
206
- const currentVersion = coerce(currentGrafanaDep.replace(/^[><=]+/, ''));
207
-
208
- if (!currentVersion || !minVersion || !gte(currentVersion, minVersion)) {
209
- pluginJson.dependencies.grafanaDependency = `>=${targetVersion}`;
210
- additionsDebug(`Updated grafanaDependency to >=${targetVersion}`);
211
- }
212
-
213
- context.updateFile('src/plugin.json', JSON.stringify(pluginJson, null, 2));
214
- additionsDebug('Updated src/plugin.json with languages:', locales);
215
- } catch (error) {
216
- additionsDebug('Error updating src/plugin.json:', error);
217
- }
218
- }
219
-
220
- function createLocaleFiles(context: Context, locales: string[]): void {
221
- // Get plugin ID from plugin.json
222
- const pluginJsonRaw = context.getFile('src/plugin.json');
223
- if (!pluginJsonRaw) {
224
- additionsDebug('Cannot create locale files without plugin.json');
225
- return;
226
- }
227
-
228
- try {
229
- const pluginJson = JSON.parse(pluginJsonRaw);
230
- const pluginId = pluginJson.id;
231
- const pluginName = pluginJson.name || pluginId;
232
-
233
- if (!pluginId) {
234
- additionsDebug('No plugin ID found in plugin.json');
235
- return;
236
- }
237
-
238
- // Create example translation structure
239
- const exampleTranslations = {
240
- components: {
241
- exampleComponent: {
242
- title: `${pluginName} component title`,
243
- description: 'Example description',
244
- },
245
- },
246
- config: {
247
- title: `${pluginName} configuration`,
248
- apiUrl: {
249
- label: 'API URL',
250
- placeholder: 'Enter API URL',
251
- },
252
- },
253
- };
254
-
255
- // Create locale files for each locale
256
- for (const locale of locales) {
257
- const localePath = `src/locales/${locale}/${pluginId}.json`;
258
-
259
- if (!context.doesFileExist(localePath)) {
260
- context.addFile(localePath, JSON.stringify(exampleTranslations, null, 2));
261
- additionsDebug(`Created ${localePath} with example translations`);
262
- }
263
- }
264
- } catch (error) {
265
- additionsDebug('Error creating locale files:', error);
266
- }
267
- }
268
-
269
- function addI18nDependency(context: Context): void {
270
- addDependenciesToPackageJson(context, { '@grafana/i18n': '12.2.2' }, {});
271
- additionsDebug('Added @grafana/i18n dependency version 12.2.2');
272
- }
273
-
274
- function addSemverDependency(context: Context): void {
275
- // Add semver as regular dependency and @types/semver as dev dependency for backward compatibility
276
- addDependenciesToPackageJson(context, { semver: '^7.6.0' }, { '@types/semver': '^7.5.0' });
277
- additionsDebug('Added semver dependency for backward compatibility');
278
- }
279
-
280
- function addI18nextCli(context: Context): void {
281
- // Add i18next-cli as dev dependency
282
- addDependenciesToPackageJson(context, {}, { 'i18next-cli': '^1.1.1' });
283
-
284
- // Add i18n-extract script to package.json
285
- const packageJsonRaw = context.getFile('package.json');
286
- if (!packageJsonRaw) {
287
- return;
288
- }
289
-
290
- try {
291
- const packageJson = JSON.parse(packageJsonRaw);
292
-
293
- if (!packageJson.scripts) {
294
- packageJson.scripts = {};
295
- }
296
-
297
- // Only add if not already present
298
- if (!packageJson.scripts['i18n-extract']) {
299
- packageJson.scripts['i18n-extract'] = 'i18next-cli extract --sync-primary';
300
- context.updateFile('package.json', JSON.stringify(packageJson, null, 2));
301
- additionsDebug('Added i18n-extract script to package.json');
302
- }
303
- } catch (error) {
304
- additionsDebug('Error adding i18n-extract script:', error);
305
- }
306
- }
307
-
308
- function updateEslintConfig(context: Context): void {
309
- if (!context.doesFileExist('eslint.config.mjs')) {
310
- additionsDebug('eslint.config.mjs not found, skipping');
311
- return;
312
- }
313
-
314
- const eslintConfigRaw = context.getFile('eslint.config.mjs');
315
- if (!eslintConfigRaw) {
316
- return;
317
- }
318
-
319
- // Check if @grafana/eslint-plugin-plugins is already configured
320
- if (eslintConfigRaw.includes('@grafana/eslint-plugin-plugins')) {
321
- additionsDebug('ESLint i18n rule already configured');
322
- return;
323
- }
324
-
325
- try {
326
- const ast = recast.parse(eslintConfigRaw, {
327
- parser: require('recast/parsers/babel-ts'),
328
- });
329
-
330
- // Find the import section and add the plugin import
331
- const imports = ast.program.body.filter((node: any) => node.type === 'ImportDeclaration');
332
- const lastImport = imports[imports.length - 1];
333
-
334
- if (lastImport) {
335
- const pluginImport = builders.importDeclaration(
336
- [builders.importDefaultSpecifier(builders.identifier('grafanaPluginsPlugin'))],
337
- builders.literal('@grafana/eslint-plugin-plugins')
338
- );
339
-
340
- const lastImportIndex = ast.program.body.indexOf(lastImport);
341
- ast.program.body.splice(lastImportIndex + 1, 0, pluginImport);
342
- }
343
-
344
- // Find the defineConfig array and add the plugin config
345
- recast.visit(ast, {
346
- visitCallExpression(path: any) {
347
- if (path.node.callee.name === 'defineConfig' && path.node.arguments[0]?.type === 'ArrayExpression') {
348
- const configArray = path.node.arguments[0];
349
-
350
- // Add the grafana plugins config object
351
- const pluginsConfig = builders.objectExpression([
352
- builders.property(
353
- 'init',
354
- builders.identifier('plugins'),
355
- builders.objectExpression([
356
- builders.property(
357
- 'init',
358
- builders.literal('grafanaPlugins'),
359
- builders.identifier('grafanaPluginsPlugin')
360
- ),
361
- ])
362
- ),
363
- builders.property(
364
- 'init',
365
- builders.identifier('rules'),
366
- builders.objectExpression([
367
- builders.property(
368
- 'init',
369
- builders.literal('grafanaPlugins/no-untranslated-strings'),
370
- builders.literal('warn')
371
- ),
372
- ])
373
- ),
374
- ]);
375
-
376
- configArray.elements.push(pluginsConfig);
377
- }
378
- this.traverse(path);
379
- },
380
- });
381
-
382
- const output = recast.print(ast, {
383
- tabWidth: 2,
384
- trailingComma: true,
385
- lineTerminator: '\n',
386
- }).code;
387
-
388
- context.updateFile('eslint.config.mjs', output);
389
- additionsDebug('Updated eslint.config.mjs with i18n linting rules');
390
- } catch (error) {
391
- additionsDebug('Error updating eslint.config.mjs:', error);
392
- }
393
- }
394
-
395
- function createI18nextConfig(context: Context): void {
396
- if (context.doesFileExist('i18next.config.ts')) {
397
- additionsDebug('i18next.config.ts already exists, skipping');
398
- return;
399
- }
400
-
401
- const pluginJsonRaw = context.getFile('src/plugin.json');
402
- if (!pluginJsonRaw) {
403
- additionsDebug('Cannot create i18next.config.ts without plugin.json');
404
- return;
405
- }
406
-
407
- try {
408
- const pluginJson = JSON.parse(pluginJsonRaw);
409
- const pluginId = pluginJson.id;
410
-
411
- if (!pluginId) {
412
- additionsDebug('No plugin ID found in plugin.json');
413
- return;
414
- }
415
-
416
- const i18nextConfig = `import { defineConfig } from 'i18next-cli';
417
- import pluginJson from './src/plugin.json';
418
-
419
- export default defineConfig({
420
- locales: pluginJson.languages,
421
- extract: {
422
- input: ['src/**/*.{tsx,ts}'],
423
- output: 'src/locales/{{language}}/{{namespace}}.json',
424
- defaultNS: pluginJson.id,
425
- functions: ['t', '*.t'],
426
- transComponents: ['Trans'],
427
- },
428
- });
429
- `;
430
-
431
- context.addFile('i18next.config.ts', i18nextConfig);
432
- additionsDebug('Created i18next.config.ts');
433
- } catch (error) {
434
- additionsDebug('Error creating i18next.config.ts:', error);
435
- }
436
- }
437
-
438
- function addI18nInitialization(context: Context, needsBackwardCompatibility: boolean): void {
439
- // Find module.ts or module.tsx
440
- const moduleTsPath = context.doesFileExist('src/module.ts')
441
- ? 'src/module.ts'
442
- : context.doesFileExist('src/module.tsx')
443
- ? 'src/module.tsx'
444
- : null;
445
-
446
- if (!moduleTsPath) {
447
- additionsDebug('No module.ts or module.tsx found, skipping i18n initialization');
448
- return;
449
- }
450
-
451
- const moduleContent = context.getFile(moduleTsPath);
452
- if (!moduleContent) {
453
- return;
454
- }
455
-
456
- // Check if i18n is already initialized
457
- if (moduleContent.includes('initPluginTranslations')) {
458
- additionsDebug('i18n already initialized in module file');
459
- return;
460
- }
461
-
462
- try {
463
- const ast = recast.parse(moduleContent, {
464
- parser: require('recast/parsers/babel-ts'),
465
- });
466
-
467
- const imports = [];
468
-
469
- // Add necessary imports based on backward compatibility
470
- imports.push(
471
- builders.importDeclaration(
472
- [builders.importSpecifier(builders.identifier('initPluginTranslations'))],
473
- builders.literal('@grafana/i18n')
474
- )
475
- );
476
-
477
- imports.push(
478
- builders.importDeclaration(
479
- [builders.importDefaultSpecifier(builders.identifier('pluginJson'))],
480
- builders.literal('plugin.json')
481
- )
482
- );
483
-
484
- if (needsBackwardCompatibility) {
485
- imports.push(
486
- builders.importDeclaration(
487
- [builders.importSpecifier(builders.identifier('config'))],
488
- builders.literal('@grafana/runtime')
489
- )
490
- );
491
- imports.push(
492
- builders.importDeclaration(
493
- [builders.importDefaultSpecifier(builders.identifier('semver'))],
494
- builders.literal('semver')
495
- )
496
- );
497
- imports.push(
498
- builders.importDeclaration(
499
- [builders.importSpecifier(builders.identifier('loadResources'))],
500
- builders.literal('./loadResources')
501
- )
502
- );
503
- }
504
-
505
- // Add imports after the first import statement
506
- const firstImportIndex = ast.program.body.findIndex((node: any) => node.type === 'ImportDeclaration');
507
- if (firstImportIndex !== -1) {
508
- ast.program.body.splice(firstImportIndex + 1, 0, ...imports);
509
- } else {
510
- ast.program.body.unshift(...imports);
511
- }
512
-
513
- // Add i18n initialization code
514
- const i18nInitCode = needsBackwardCompatibility
515
- ? `// Before Grafana version 12.1.0 the plugin is responsible for loading translation resources
516
- // In Grafana version 12.1.0 and later Grafana is responsible for loading translation resources
517
- const loaders = semver.lt(config?.buildInfo?.version, '12.1.0') ? [loadResources] : [];
518
-
519
- await initPluginTranslations(pluginJson.id, loaders);`
520
- : `await initPluginTranslations(pluginJson.id);`;
521
-
522
- // Parse the initialization code and insert it at the top level (after imports)
523
- const initAst = recast.parse(i18nInitCode, {
524
- parser: require('recast/parsers/babel-ts'),
525
- });
526
-
527
- // Find the last import index
528
- const lastImportIndex = ast.program.body.findLastIndex((node: any) => node.type === 'ImportDeclaration');
529
- if (lastImportIndex !== -1) {
530
- ast.program.body.splice(lastImportIndex + 1, 0, ...initAst.program.body);
531
- } else {
532
- ast.program.body.unshift(...initAst.program.body);
533
- }
534
-
535
- const output = recast.print(ast, {
536
- tabWidth: 2,
537
- trailingComma: true,
538
- lineTerminator: '\n',
539
- }).code;
540
-
541
- context.updateFile(moduleTsPath, output);
542
- additionsDebug(`Updated ${moduleTsPath} with i18n initialization`);
543
- } catch (error) {
544
- additionsDebug('Error updating module file:', error);
545
- }
546
- }
547
-
548
- function createLoadResourcesFile(context: Context): void {
549
- const loadResourcesPath = 'src/loadResources.ts';
550
-
551
- if (context.doesFileExist(loadResourcesPath)) {
552
- additionsDebug('loadResources.ts already exists, skipping');
553
- return;
554
- }
555
-
556
- const pluginJsonRaw = context.getFile('src/plugin.json');
557
- if (!pluginJsonRaw) {
558
- additionsDebug('Cannot create loadResources.ts without plugin.json');
559
- return;
560
- }
561
-
562
- const loadResourcesContent = `import { LANGUAGES, ResourceLoader, Resources } from '@grafana/i18n';
563
- import pluginJson from 'plugin.json';
564
-
565
- const resources = LANGUAGES.reduce<Record<string, () => Promise<{ default: Resources }>>>((acc, lang) => {
566
- acc[lang.code] = async () => await import(\`./locales/\${lang.code}/\${pluginJson.id}.json\`);
567
- return acc;
568
- }, {});
569
-
570
- export const loadResources: ResourceLoader = async (resolvedLanguage: string) => {
571
- try {
572
- const translation = await resources[resolvedLanguage]();
573
- return translation.default;
574
- } catch (error) {
575
- // This makes sure that the plugin doesn't crash when the resolved language in Grafana isn't supported by the plugin
576
- console.error(\`The plugin '\${pluginJson.id}' doesn't support the language '\${resolvedLanguage}'\`, error);
577
- return {};
578
- }
579
- };
580
- `;
581
-
582
- context.addFile(loadResourcesPath, loadResourcesContent);
583
- additionsDebug('Created src/loadResources.ts for backward compatibility');
584
- }
@@ -1,12 +0,0 @@
1
- import type { AdditionMeta } from './additions.js';
2
- import type { Context } from '../context.js';
3
- import { debug } from '../../utils/utils.cli.js';
4
- import { printChanges as sharedPrintChanges } from '../utils.js';
5
-
6
- export const additionsDebug = debug.extend('additions');
7
-
8
- // addition-specific wrapper for printChanges.
9
- export function printChanges(context: Context, key: string, addition: AdditionMeta) {
10
- additionsDebug('printChanges for addition: %s', key);
11
- sharedPrintChanges(context, key, addition);
12
- }
@@ -1,12 +0,0 @@
1
- import type { Context } from '../context.js';
2
- import type { MigrationMeta } from './migrations.js';
3
- import { debug } from '../../utils/utils.cli.js';
4
- import { printChanges as sharedPrintChanges } from '../utils.js';
5
-
6
- export const migrationsDebug = debug.extend('migrations');
7
-
8
- // migration-specific wrapper for printChanges
9
- export function printChanges(context: Context, key: string, migration: MigrationMeta) {
10
- migrationsDebug('printChanges for migration: %s', key);
11
- sharedPrintChanges(context, key, migration);
12
- }
@@ -1,21 +0,0 @@
1
- import type { Context } from './context.js';
2
-
3
- export interface CodemodModule {
4
- default: (context: Context) => Context | Promise<Context>;
5
- }
6
-
7
- export interface FlagDefinition {
8
- name: string;
9
- description: string;
10
- required: boolean;
11
- }
12
-
13
- export interface AdditionModule<TOptions = any> extends CodemodModule {
14
- default: (context: Context, options?: TOptions) => Context | Promise<Context>;
15
- flags?: FlagDefinition[];
16
- parseFlags?: (argv: any) => TOptions;
17
- }
18
-
19
- export interface MigrationModule extends CodemodModule {
20
- default: (context: Context) => Context | Promise<Context>;
21
- }