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