@grafana/create-plugin 6.2.0-canary.2233.19097561440.0 → 6.2.0-canary.2233.19133609453.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/dist/codemods/additions/scripts/example-addition.js +61 -0
- package/package.json +2 -2
- package/src/codemods/additions/scripts/example-addition.test.ts +111 -0
- package/src/codemods/additions/scripts/example-addition.ts +79 -0
- package/dist/codemods/additions/scripts/add-i18n.js +0 -445
- package/src/codemods/additions/scripts/add-i18n.test.ts +0 -347
- package/src/codemods/additions/scripts/add-i18n.ts +0 -584
|
@@ -1,445 +0,0 @@
|
|
|
1
|
-
import * as recast from 'recast';
|
|
2
|
-
import { coerce, gte } from 'semver';
|
|
3
|
-
import { parseDocument, stringify } from 'yaml';
|
|
4
|
-
import { addDependenciesToPackageJson } from '../../utils.js';
|
|
5
|
-
import { additionsDebug } from '../utils.js';
|
|
6
|
-
|
|
7
|
-
const { builders } = recast.types;
|
|
8
|
-
const flags = [
|
|
9
|
-
{
|
|
10
|
-
name: "locales",
|
|
11
|
-
description: "Comma-separated list of locales (e.g., en-US,es-ES)",
|
|
12
|
-
required: true
|
|
13
|
-
}
|
|
14
|
-
];
|
|
15
|
-
function parseFlags(argv) {
|
|
16
|
-
return {
|
|
17
|
-
locales: argv.locales ? argv.locales.split(",").map((l) => l.trim()) : []
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
function migrate(context, options = { locales: ["en-US"] }) {
|
|
21
|
-
const { locales } = options;
|
|
22
|
-
additionsDebug("Adding i18n support with locales:", locales);
|
|
23
|
-
if (isI18nConfigured(context)) {
|
|
24
|
-
additionsDebug("i18n already configured, skipping");
|
|
25
|
-
return context;
|
|
26
|
-
}
|
|
27
|
-
const needsBackwardCompatibility = checkNeedsBackwardCompatibility(context);
|
|
28
|
-
additionsDebug("Needs backward compatibility:", needsBackwardCompatibility);
|
|
29
|
-
if (!needsBackwardCompatibility) {
|
|
30
|
-
updateDockerCompose(context);
|
|
31
|
-
}
|
|
32
|
-
updatePluginJson(context, locales, needsBackwardCompatibility);
|
|
33
|
-
createLocaleFiles(context, locales);
|
|
34
|
-
addI18nDependency(context);
|
|
35
|
-
if (needsBackwardCompatibility) {
|
|
36
|
-
addSemverDependency(context);
|
|
37
|
-
}
|
|
38
|
-
updateEslintConfig(context);
|
|
39
|
-
addI18nInitialization(context, needsBackwardCompatibility);
|
|
40
|
-
if (needsBackwardCompatibility) {
|
|
41
|
-
createLoadResourcesFile(context);
|
|
42
|
-
}
|
|
43
|
-
addI18nextCli(context);
|
|
44
|
-
createI18nextConfig(context);
|
|
45
|
-
return context;
|
|
46
|
-
}
|
|
47
|
-
function isI18nConfigured(context) {
|
|
48
|
-
if (!context.doesFileExist("src/plugin.json")) {
|
|
49
|
-
return false;
|
|
50
|
-
}
|
|
51
|
-
const pluginJsonRaw = context.getFile("src/plugin.json");
|
|
52
|
-
if (!pluginJsonRaw) {
|
|
53
|
-
return false;
|
|
54
|
-
}
|
|
55
|
-
try {
|
|
56
|
-
const pluginJson = JSON.parse(pluginJsonRaw);
|
|
57
|
-
if (pluginJson.languages && Array.isArray(pluginJson.languages) && pluginJson.languages.length > 0) {
|
|
58
|
-
additionsDebug("Found languages in plugin.json, i18n already configured");
|
|
59
|
-
return true;
|
|
60
|
-
}
|
|
61
|
-
} catch (error) {
|
|
62
|
-
additionsDebug("Error parsing plugin.json:", error);
|
|
63
|
-
}
|
|
64
|
-
return false;
|
|
65
|
-
}
|
|
66
|
-
function checkNeedsBackwardCompatibility(context) {
|
|
67
|
-
const pluginJsonRaw = context.getFile("src/plugin.json");
|
|
68
|
-
if (!pluginJsonRaw) {
|
|
69
|
-
return false;
|
|
70
|
-
}
|
|
71
|
-
try {
|
|
72
|
-
const pluginJson = JSON.parse(pluginJsonRaw);
|
|
73
|
-
const currentGrafanaDep = pluginJson.dependencies?.grafanaDependency || ">=11.0.0";
|
|
74
|
-
const minVersion = coerce("12.1.0");
|
|
75
|
-
const currentVersion = coerce(currentGrafanaDep.replace(/^[><=]+/, ""));
|
|
76
|
-
if (currentVersion && minVersion && gte(currentVersion, minVersion)) {
|
|
77
|
-
return false;
|
|
78
|
-
}
|
|
79
|
-
return true;
|
|
80
|
-
} catch (error) {
|
|
81
|
-
additionsDebug("Error checking backward compatibility:", error);
|
|
82
|
-
return true;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
function updateDockerCompose(context) {
|
|
86
|
-
if (!context.doesFileExist("docker-compose.yaml")) {
|
|
87
|
-
additionsDebug("docker-compose.yaml not found, skipping");
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
const composeContent = context.getFile("docker-compose.yaml");
|
|
91
|
-
if (!composeContent) {
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
try {
|
|
95
|
-
const composeDoc = parseDocument(composeContent);
|
|
96
|
-
const currentEnv = composeDoc.getIn(["services", "grafana", "environment"]);
|
|
97
|
-
if (!currentEnv) {
|
|
98
|
-
additionsDebug("No environment section found in docker-compose.yaml, skipping");
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
if (typeof currentEnv === "object") {
|
|
102
|
-
const envMap = currentEnv;
|
|
103
|
-
const toggleValue = envMap.get("GF_FEATURE_TOGGLES_ENABLE");
|
|
104
|
-
if (toggleValue) {
|
|
105
|
-
const toggleStr = toggleValue.toString();
|
|
106
|
-
if (toggleStr.includes("localizationForPlugins")) {
|
|
107
|
-
additionsDebug("localizationForPlugins already in GF_FEATURE_TOGGLES_ENABLE");
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
composeDoc.setIn(
|
|
111
|
-
["services", "grafana", "environment", "GF_FEATURE_TOGGLES_ENABLE"],
|
|
112
|
-
`${toggleStr},localizationForPlugins`
|
|
113
|
-
);
|
|
114
|
-
} else {
|
|
115
|
-
composeDoc.setIn(["services", "grafana", "environment", "GF_FEATURE_TOGGLES_ENABLE"], "localizationForPlugins");
|
|
116
|
-
}
|
|
117
|
-
context.updateFile("docker-compose.yaml", stringify(composeDoc));
|
|
118
|
-
additionsDebug("Updated docker-compose.yaml with localizationForPlugins feature toggle");
|
|
119
|
-
}
|
|
120
|
-
} catch (error) {
|
|
121
|
-
additionsDebug("Error updating docker-compose.yaml:", error);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
function updatePluginJson(context, locales, needsBackwardCompatibility) {
|
|
125
|
-
if (!context.doesFileExist("src/plugin.json")) {
|
|
126
|
-
additionsDebug("src/plugin.json not found, skipping");
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
const pluginJsonRaw = context.getFile("src/plugin.json");
|
|
130
|
-
if (!pluginJsonRaw) {
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
try {
|
|
134
|
-
const pluginJson = JSON.parse(pluginJsonRaw);
|
|
135
|
-
pluginJson.languages = locales;
|
|
136
|
-
if (!pluginJson.dependencies) {
|
|
137
|
-
pluginJson.dependencies = {};
|
|
138
|
-
}
|
|
139
|
-
const currentGrafanaDep = pluginJson.dependencies.grafanaDependency || ">=11.0.0";
|
|
140
|
-
const targetVersion = needsBackwardCompatibility ? "11.0.0" : "12.1.0";
|
|
141
|
-
const minVersion = coerce(targetVersion);
|
|
142
|
-
const currentVersion = coerce(currentGrafanaDep.replace(/^[><=]+/, ""));
|
|
143
|
-
if (!currentVersion || !minVersion || !gte(currentVersion, minVersion)) {
|
|
144
|
-
pluginJson.dependencies.grafanaDependency = `>=${targetVersion}`;
|
|
145
|
-
additionsDebug(`Updated grafanaDependency to >=${targetVersion}`);
|
|
146
|
-
}
|
|
147
|
-
context.updateFile("src/plugin.json", JSON.stringify(pluginJson, null, 2));
|
|
148
|
-
additionsDebug("Updated src/plugin.json with languages:", locales);
|
|
149
|
-
} catch (error) {
|
|
150
|
-
additionsDebug("Error updating src/plugin.json:", error);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
function createLocaleFiles(context, locales) {
|
|
154
|
-
const pluginJsonRaw = context.getFile("src/plugin.json");
|
|
155
|
-
if (!pluginJsonRaw) {
|
|
156
|
-
additionsDebug("Cannot create locale files without plugin.json");
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
|
-
try {
|
|
160
|
-
const pluginJson = JSON.parse(pluginJsonRaw);
|
|
161
|
-
const pluginId = pluginJson.id;
|
|
162
|
-
const pluginName = pluginJson.name || pluginId;
|
|
163
|
-
if (!pluginId) {
|
|
164
|
-
additionsDebug("No plugin ID found in plugin.json");
|
|
165
|
-
return;
|
|
166
|
-
}
|
|
167
|
-
const exampleTranslations = {
|
|
168
|
-
components: {
|
|
169
|
-
exampleComponent: {
|
|
170
|
-
title: `${pluginName} component title`,
|
|
171
|
-
description: "Example description"
|
|
172
|
-
}
|
|
173
|
-
},
|
|
174
|
-
config: {
|
|
175
|
-
title: `${pluginName} configuration`,
|
|
176
|
-
apiUrl: {
|
|
177
|
-
label: "API URL",
|
|
178
|
-
placeholder: "Enter API URL"
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
};
|
|
182
|
-
for (const locale of locales) {
|
|
183
|
-
const localePath = `src/locales/${locale}/${pluginId}.json`;
|
|
184
|
-
if (!context.doesFileExist(localePath)) {
|
|
185
|
-
context.addFile(localePath, JSON.stringify(exampleTranslations, null, 2));
|
|
186
|
-
additionsDebug(`Created ${localePath} with example translations`);
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
} catch (error) {
|
|
190
|
-
additionsDebug("Error creating locale files:", error);
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
function addI18nDependency(context) {
|
|
194
|
-
addDependenciesToPackageJson(context, { "@grafana/i18n": "12.2.2" }, {});
|
|
195
|
-
additionsDebug("Added @grafana/i18n dependency version 12.2.2");
|
|
196
|
-
}
|
|
197
|
-
function addSemverDependency(context) {
|
|
198
|
-
addDependenciesToPackageJson(context, { semver: "^7.6.0" }, { "@types/semver": "^7.5.0" });
|
|
199
|
-
additionsDebug("Added semver dependency for backward compatibility");
|
|
200
|
-
}
|
|
201
|
-
function addI18nextCli(context) {
|
|
202
|
-
addDependenciesToPackageJson(context, {}, { "i18next-cli": "^1.1.1" });
|
|
203
|
-
const packageJsonRaw = context.getFile("package.json");
|
|
204
|
-
if (!packageJsonRaw) {
|
|
205
|
-
return;
|
|
206
|
-
}
|
|
207
|
-
try {
|
|
208
|
-
const packageJson = JSON.parse(packageJsonRaw);
|
|
209
|
-
if (!packageJson.scripts) {
|
|
210
|
-
packageJson.scripts = {};
|
|
211
|
-
}
|
|
212
|
-
if (!packageJson.scripts["i18n-extract"]) {
|
|
213
|
-
packageJson.scripts["i18n-extract"] = "i18next-cli extract --sync-primary";
|
|
214
|
-
context.updateFile("package.json", JSON.stringify(packageJson, null, 2));
|
|
215
|
-
additionsDebug("Added i18n-extract script to package.json");
|
|
216
|
-
}
|
|
217
|
-
} catch (error) {
|
|
218
|
-
additionsDebug("Error adding i18n-extract script:", error);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
function updateEslintConfig(context) {
|
|
222
|
-
if (!context.doesFileExist("eslint.config.mjs")) {
|
|
223
|
-
additionsDebug("eslint.config.mjs not found, skipping");
|
|
224
|
-
return;
|
|
225
|
-
}
|
|
226
|
-
const eslintConfigRaw = context.getFile("eslint.config.mjs");
|
|
227
|
-
if (!eslintConfigRaw) {
|
|
228
|
-
return;
|
|
229
|
-
}
|
|
230
|
-
if (eslintConfigRaw.includes("@grafana/eslint-plugin-plugins")) {
|
|
231
|
-
additionsDebug("ESLint i18n rule already configured");
|
|
232
|
-
return;
|
|
233
|
-
}
|
|
234
|
-
try {
|
|
235
|
-
const ast = recast.parse(eslintConfigRaw, {
|
|
236
|
-
parser: require("recast/parsers/babel-ts")
|
|
237
|
-
});
|
|
238
|
-
const imports = ast.program.body.filter((node) => node.type === "ImportDeclaration");
|
|
239
|
-
const lastImport = imports[imports.length - 1];
|
|
240
|
-
if (lastImport) {
|
|
241
|
-
const pluginImport = builders.importDeclaration(
|
|
242
|
-
[builders.importDefaultSpecifier(builders.identifier("grafanaPluginsPlugin"))],
|
|
243
|
-
builders.literal("@grafana/eslint-plugin-plugins")
|
|
244
|
-
);
|
|
245
|
-
const lastImportIndex = ast.program.body.indexOf(lastImport);
|
|
246
|
-
ast.program.body.splice(lastImportIndex + 1, 0, pluginImport);
|
|
247
|
-
}
|
|
248
|
-
recast.visit(ast, {
|
|
249
|
-
visitCallExpression(path) {
|
|
250
|
-
if (path.node.callee.name === "defineConfig" && path.node.arguments[0]?.type === "ArrayExpression") {
|
|
251
|
-
const configArray = path.node.arguments[0];
|
|
252
|
-
const pluginsConfig = builders.objectExpression([
|
|
253
|
-
builders.property(
|
|
254
|
-
"init",
|
|
255
|
-
builders.identifier("plugins"),
|
|
256
|
-
builders.objectExpression([
|
|
257
|
-
builders.property(
|
|
258
|
-
"init",
|
|
259
|
-
builders.literal("grafanaPlugins"),
|
|
260
|
-
builders.identifier("grafanaPluginsPlugin")
|
|
261
|
-
)
|
|
262
|
-
])
|
|
263
|
-
),
|
|
264
|
-
builders.property(
|
|
265
|
-
"init",
|
|
266
|
-
builders.identifier("rules"),
|
|
267
|
-
builders.objectExpression([
|
|
268
|
-
builders.property(
|
|
269
|
-
"init",
|
|
270
|
-
builders.literal("grafanaPlugins/no-untranslated-strings"),
|
|
271
|
-
builders.literal("warn")
|
|
272
|
-
)
|
|
273
|
-
])
|
|
274
|
-
)
|
|
275
|
-
]);
|
|
276
|
-
configArray.elements.push(pluginsConfig);
|
|
277
|
-
}
|
|
278
|
-
this.traverse(path);
|
|
279
|
-
}
|
|
280
|
-
});
|
|
281
|
-
const output = recast.print(ast, {
|
|
282
|
-
tabWidth: 2,
|
|
283
|
-
trailingComma: true,
|
|
284
|
-
lineTerminator: "\n"
|
|
285
|
-
}).code;
|
|
286
|
-
context.updateFile("eslint.config.mjs", output);
|
|
287
|
-
additionsDebug("Updated eslint.config.mjs with i18n linting rules");
|
|
288
|
-
} catch (error) {
|
|
289
|
-
additionsDebug("Error updating eslint.config.mjs:", error);
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
function createI18nextConfig(context) {
|
|
293
|
-
if (context.doesFileExist("i18next.config.ts")) {
|
|
294
|
-
additionsDebug("i18next.config.ts already exists, skipping");
|
|
295
|
-
return;
|
|
296
|
-
}
|
|
297
|
-
const pluginJsonRaw = context.getFile("src/plugin.json");
|
|
298
|
-
if (!pluginJsonRaw) {
|
|
299
|
-
additionsDebug("Cannot create i18next.config.ts without plugin.json");
|
|
300
|
-
return;
|
|
301
|
-
}
|
|
302
|
-
try {
|
|
303
|
-
const pluginJson = JSON.parse(pluginJsonRaw);
|
|
304
|
-
const pluginId = pluginJson.id;
|
|
305
|
-
if (!pluginId) {
|
|
306
|
-
additionsDebug("No plugin ID found in plugin.json");
|
|
307
|
-
return;
|
|
308
|
-
}
|
|
309
|
-
const i18nextConfig = `import { defineConfig } from 'i18next-cli';
|
|
310
|
-
import pluginJson from './src/plugin.json';
|
|
311
|
-
|
|
312
|
-
export default defineConfig({
|
|
313
|
-
locales: pluginJson.languages,
|
|
314
|
-
extract: {
|
|
315
|
-
input: ['src/**/*.{tsx,ts}'],
|
|
316
|
-
output: 'src/locales/{{language}}/{{namespace}}.json',
|
|
317
|
-
defaultNS: pluginJson.id,
|
|
318
|
-
functions: ['t', '*.t'],
|
|
319
|
-
transComponents: ['Trans'],
|
|
320
|
-
},
|
|
321
|
-
});
|
|
322
|
-
`;
|
|
323
|
-
context.addFile("i18next.config.ts", i18nextConfig);
|
|
324
|
-
additionsDebug("Created i18next.config.ts");
|
|
325
|
-
} catch (error) {
|
|
326
|
-
additionsDebug("Error creating i18next.config.ts:", error);
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
function addI18nInitialization(context, needsBackwardCompatibility) {
|
|
330
|
-
const moduleTsPath = context.doesFileExist("src/module.ts") ? "src/module.ts" : context.doesFileExist("src/module.tsx") ? "src/module.tsx" : null;
|
|
331
|
-
if (!moduleTsPath) {
|
|
332
|
-
additionsDebug("No module.ts or module.tsx found, skipping i18n initialization");
|
|
333
|
-
return;
|
|
334
|
-
}
|
|
335
|
-
const moduleContent = context.getFile(moduleTsPath);
|
|
336
|
-
if (!moduleContent) {
|
|
337
|
-
return;
|
|
338
|
-
}
|
|
339
|
-
if (moduleContent.includes("initPluginTranslations")) {
|
|
340
|
-
additionsDebug("i18n already initialized in module file");
|
|
341
|
-
return;
|
|
342
|
-
}
|
|
343
|
-
try {
|
|
344
|
-
const ast = recast.parse(moduleContent, {
|
|
345
|
-
parser: require("recast/parsers/babel-ts")
|
|
346
|
-
});
|
|
347
|
-
const imports = [];
|
|
348
|
-
imports.push(
|
|
349
|
-
builders.importDeclaration(
|
|
350
|
-
[builders.importSpecifier(builders.identifier("initPluginTranslations"))],
|
|
351
|
-
builders.literal("@grafana/i18n")
|
|
352
|
-
)
|
|
353
|
-
);
|
|
354
|
-
imports.push(
|
|
355
|
-
builders.importDeclaration(
|
|
356
|
-
[builders.importDefaultSpecifier(builders.identifier("pluginJson"))],
|
|
357
|
-
builders.literal("plugin.json")
|
|
358
|
-
)
|
|
359
|
-
);
|
|
360
|
-
if (needsBackwardCompatibility) {
|
|
361
|
-
imports.push(
|
|
362
|
-
builders.importDeclaration(
|
|
363
|
-
[builders.importSpecifier(builders.identifier("config"))],
|
|
364
|
-
builders.literal("@grafana/runtime")
|
|
365
|
-
)
|
|
366
|
-
);
|
|
367
|
-
imports.push(
|
|
368
|
-
builders.importDeclaration(
|
|
369
|
-
[builders.importDefaultSpecifier(builders.identifier("semver"))],
|
|
370
|
-
builders.literal("semver")
|
|
371
|
-
)
|
|
372
|
-
);
|
|
373
|
-
imports.push(
|
|
374
|
-
builders.importDeclaration(
|
|
375
|
-
[builders.importSpecifier(builders.identifier("loadResources"))],
|
|
376
|
-
builders.literal("./loadResources")
|
|
377
|
-
)
|
|
378
|
-
);
|
|
379
|
-
}
|
|
380
|
-
const firstImportIndex = ast.program.body.findIndex((node) => node.type === "ImportDeclaration");
|
|
381
|
-
if (firstImportIndex !== -1) {
|
|
382
|
-
ast.program.body.splice(firstImportIndex + 1, 0, ...imports);
|
|
383
|
-
} else {
|
|
384
|
-
ast.program.body.unshift(...imports);
|
|
385
|
-
}
|
|
386
|
-
const i18nInitCode = needsBackwardCompatibility ? `// Before Grafana version 12.1.0 the plugin is responsible for loading translation resources
|
|
387
|
-
// In Grafana version 12.1.0 and later Grafana is responsible for loading translation resources
|
|
388
|
-
const loaders = semver.lt(config?.buildInfo?.version, '12.1.0') ? [loadResources] : [];
|
|
389
|
-
|
|
390
|
-
await initPluginTranslations(pluginJson.id, loaders);` : `await initPluginTranslations(pluginJson.id);`;
|
|
391
|
-
const initAst = recast.parse(i18nInitCode, {
|
|
392
|
-
parser: require("recast/parsers/babel-ts")
|
|
393
|
-
});
|
|
394
|
-
const lastImportIndex = ast.program.body.findLastIndex((node) => node.type === "ImportDeclaration");
|
|
395
|
-
if (lastImportIndex !== -1) {
|
|
396
|
-
ast.program.body.splice(lastImportIndex + 1, 0, ...initAst.program.body);
|
|
397
|
-
} else {
|
|
398
|
-
ast.program.body.unshift(...initAst.program.body);
|
|
399
|
-
}
|
|
400
|
-
const output = recast.print(ast, {
|
|
401
|
-
tabWidth: 2,
|
|
402
|
-
trailingComma: true,
|
|
403
|
-
lineTerminator: "\n"
|
|
404
|
-
}).code;
|
|
405
|
-
context.updateFile(moduleTsPath, output);
|
|
406
|
-
additionsDebug(`Updated ${moduleTsPath} with i18n initialization`);
|
|
407
|
-
} catch (error) {
|
|
408
|
-
additionsDebug("Error updating module file:", error);
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
function createLoadResourcesFile(context) {
|
|
412
|
-
const loadResourcesPath = "src/loadResources.ts";
|
|
413
|
-
if (context.doesFileExist(loadResourcesPath)) {
|
|
414
|
-
additionsDebug("loadResources.ts already exists, skipping");
|
|
415
|
-
return;
|
|
416
|
-
}
|
|
417
|
-
const pluginJsonRaw = context.getFile("src/plugin.json");
|
|
418
|
-
if (!pluginJsonRaw) {
|
|
419
|
-
additionsDebug("Cannot create loadResources.ts without plugin.json");
|
|
420
|
-
return;
|
|
421
|
-
}
|
|
422
|
-
const loadResourcesContent = `import { LANGUAGES, ResourceLoader, Resources } from '@grafana/i18n';
|
|
423
|
-
import pluginJson from 'plugin.json';
|
|
424
|
-
|
|
425
|
-
const resources = LANGUAGES.reduce<Record<string, () => Promise<{ default: Resources }>>>((acc, lang) => {
|
|
426
|
-
acc[lang.code] = async () => await import(\`./locales/\${lang.code}/\${pluginJson.id}.json\`);
|
|
427
|
-
return acc;
|
|
428
|
-
}, {});
|
|
429
|
-
|
|
430
|
-
export const loadResources: ResourceLoader = async (resolvedLanguage: string) => {
|
|
431
|
-
try {
|
|
432
|
-
const translation = await resources[resolvedLanguage]();
|
|
433
|
-
return translation.default;
|
|
434
|
-
} catch (error) {
|
|
435
|
-
// This makes sure that the plugin doesn't crash when the resolved language in Grafana isn't supported by the plugin
|
|
436
|
-
console.error(\`The plugin '\${pluginJson.id}' doesn't support the language '\${resolvedLanguage}'\`, error);
|
|
437
|
-
return {};
|
|
438
|
-
}
|
|
439
|
-
};
|
|
440
|
-
`;
|
|
441
|
-
context.addFile(loadResourcesPath, loadResourcesContent);
|
|
442
|
-
additionsDebug("Created src/loadResources.ts for backward compatibility");
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
export { migrate as default, flags, parseFlags };
|