@grafana/create-plugin 6.4.4 → 6.5.0-canary.2320.20231018478.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.
@@ -0,0 +1,361 @@
1
+ import { coerce, gte } from 'semver';
2
+ import { parseDocument, stringify } from 'yaml';
3
+ import * as recast from 'recast';
4
+ import * as typeScriptParser from 'recast/parsers/typescript.js';
5
+
6
+ import type { Context } from '../../../context.js';
7
+ import { additionsDebug } from '../../../utils.js';
8
+
9
+ const { builders } = recast.types;
10
+
11
+ export function updateDockerCompose(context: Context): void {
12
+ if (!context.doesFileExist('docker-compose.yaml')) {
13
+ additionsDebug('docker-compose.yaml not found, skipping');
14
+ return;
15
+ }
16
+
17
+ const composeContent = context.getFile('docker-compose.yaml');
18
+ if (!composeContent) {
19
+ return;
20
+ }
21
+
22
+ try {
23
+ const composeDoc = parseDocument(composeContent);
24
+ const currentEnv = composeDoc.getIn(['services', 'grafana', 'environment']);
25
+
26
+ if (!currentEnv) {
27
+ additionsDebug('No environment section found in docker-compose.yaml, skipping');
28
+ return;
29
+ }
30
+
31
+ // Check if the feature toggle is already set
32
+ if (typeof currentEnv === 'object') {
33
+ const envMap = currentEnv as any;
34
+ const toggleValue = envMap.get('GF_FEATURE_TOGGLES_ENABLE');
35
+
36
+ if (toggleValue) {
37
+ const toggleStr = toggleValue.toString();
38
+ if (toggleStr.includes('localizationForPlugins')) {
39
+ additionsDebug('localizationForPlugins already in GF_FEATURE_TOGGLES_ENABLE');
40
+ return;
41
+ }
42
+ // Append to existing feature toggles
43
+ composeDoc.setIn(
44
+ ['services', 'grafana', 'environment', 'GF_FEATURE_TOGGLES_ENABLE'],
45
+ `${toggleStr},localizationForPlugins`
46
+ );
47
+ } else {
48
+ // Set new feature toggle
49
+ composeDoc.setIn(['services', 'grafana', 'environment', 'GF_FEATURE_TOGGLES_ENABLE'], 'localizationForPlugins');
50
+ }
51
+
52
+ context.updateFile('docker-compose.yaml', stringify(composeDoc));
53
+ additionsDebug('Updated docker-compose.yaml with localizationForPlugins feature toggle');
54
+ }
55
+ } catch (error) {
56
+ additionsDebug('Error updating docker-compose.yaml:', error);
57
+ }
58
+ }
59
+
60
+ export function updatePluginJson(context: Context, locales: string[], needsBackwardCompatibility: boolean): void {
61
+ if (!context.doesFileExist('src/plugin.json')) {
62
+ additionsDebug('src/plugin.json not found, skipping');
63
+ return;
64
+ }
65
+
66
+ const pluginJsonRaw = context.getFile('src/plugin.json');
67
+ if (!pluginJsonRaw) {
68
+ return;
69
+ }
70
+
71
+ try {
72
+ const pluginJson = JSON.parse(pluginJsonRaw);
73
+
74
+ // Merge locales with existing languages (defensive: avoid duplicates)
75
+ const existingLanguages = Array.isArray(pluginJson.languages) ? pluginJson.languages : [];
76
+ const mergedLanguages = [...new Set([...existingLanguages, ...locales])];
77
+ pluginJson.languages = mergedLanguages;
78
+
79
+ // Update grafanaDependency based on backward compatibility needs
80
+ if (!pluginJson.dependencies) {
81
+ pluginJson.dependencies = {};
82
+ }
83
+
84
+ const currentGrafanaDep = pluginJson.dependencies.grafanaDependency || '>=11.0.0';
85
+ const targetVersion = needsBackwardCompatibility ? '11.0.0' : '12.1.0';
86
+ const minVersion = coerce(targetVersion);
87
+ const currentVersion = coerce(currentGrafanaDep.replace(/^[><=]+/, ''));
88
+
89
+ if (!currentVersion || !minVersion || !gte(currentVersion, minVersion)) {
90
+ pluginJson.dependencies.grafanaDependency = `>=${targetVersion}`;
91
+ additionsDebug(`Updated grafanaDependency to >=${targetVersion}`);
92
+ }
93
+
94
+ context.updateFile('src/plugin.json', JSON.stringify(pluginJson, null, 2));
95
+ additionsDebug('Updated src/plugin.json with languages:', locales);
96
+ } catch (error) {
97
+ additionsDebug('Error updating src/plugin.json:', error);
98
+ }
99
+ }
100
+
101
+ export function createI18nextConfig(context: Context): void {
102
+ // Defensive: skip if already exists
103
+ if (context.doesFileExist('i18next.config.ts')) {
104
+ additionsDebug('i18next.config.ts already exists, skipping');
105
+ return;
106
+ }
107
+
108
+ const pluginJsonRaw = context.getFile('src/plugin.json');
109
+ if (!pluginJsonRaw) {
110
+ additionsDebug('Cannot create i18next.config.ts without plugin.json');
111
+ return;
112
+ }
113
+
114
+ try {
115
+ const pluginJson = JSON.parse(pluginJsonRaw);
116
+ const pluginId = pluginJson.id;
117
+
118
+ if (!pluginId) {
119
+ additionsDebug('No plugin ID found in plugin.json');
120
+ return;
121
+ }
122
+
123
+ const i18nextConfig = `import { defineConfig } from 'i18next-cli';
124
+ import pluginJson from './src/plugin.json';
125
+
126
+ export default defineConfig({
127
+ locales: pluginJson.languages,
128
+ extract: {
129
+ input: ['src/**/*.{tsx,ts}'],
130
+ output: 'src/locales/{{language}}/{{namespace}}.json',
131
+ defaultNS: pluginJson.id,
132
+ functions: ['t', '*.t'],
133
+ transComponents: ['Trans'],
134
+ },
135
+ });
136
+ `;
137
+
138
+ context.addFile('i18next.config.ts', i18nextConfig);
139
+ additionsDebug('Created i18next.config.ts');
140
+ } catch (error) {
141
+ additionsDebug('Error creating i18next.config.ts:', error);
142
+ }
143
+ }
144
+
145
+ /**
146
+ * Adds 'i18next' to an externals array if it's not already present
147
+ * @returns true if changes were made, false otherwise
148
+ */
149
+ function addI18nextToExternalsArray(externalsArray: recast.types.namedTypes.ArrayExpression): boolean {
150
+ // Check if 'i18next' is already in the array
151
+ const hasI18next = externalsArray.elements.some((element) => {
152
+ if (
153
+ element &&
154
+ (element.type === 'Literal' || element.type === 'StringLiteral') &&
155
+ typeof element.value === 'string'
156
+ ) {
157
+ return element.value === 'i18next';
158
+ }
159
+ return false;
160
+ });
161
+
162
+ if (hasI18next) {
163
+ additionsDebug("'i18next' already in externals array");
164
+ return false;
165
+ }
166
+
167
+ // Find the position after 'rxjs' to insert 'i18next'
168
+ let insertIndex = -1;
169
+ for (let i = 0; i < externalsArray.elements.length; i++) {
170
+ const element = externalsArray.elements[i];
171
+ if (element && (element.type === 'Literal' || element.type === 'StringLiteral') && element.value === 'rxjs') {
172
+ insertIndex = i + 1;
173
+ break;
174
+ }
175
+ }
176
+
177
+ // If 'rxjs' not found, append to the end (before the function at the end)
178
+ if (insertIndex === -1) {
179
+ // Find the last non-function element
180
+ for (let i = externalsArray.elements.length - 1; i >= 0; i--) {
181
+ const element = externalsArray.elements[i];
182
+ if (element && element.type !== 'FunctionExpression' && element.type !== 'ArrowFunctionExpression') {
183
+ insertIndex = i + 1;
184
+ break;
185
+ }
186
+ }
187
+ // If still not found, append at the end
188
+ if (insertIndex === -1) {
189
+ insertIndex = externalsArray.elements.length;
190
+ }
191
+ }
192
+
193
+ // Insert 'i18next' at the found position
194
+ externalsArray.elements.splice(insertIndex, 0, builders.literal('i18next'));
195
+ additionsDebug(`Added 'i18next' to externals array at position ${insertIndex}`);
196
+ return true;
197
+ }
198
+
199
+ export function ensureI18nextExternal(context: Context): void {
200
+ try {
201
+ additionsDebug('Checking for externals configuration...');
202
+
203
+ // Try new structure first: .config/bundler/externals.ts
204
+ const externalsPath = '.config/bundler/externals.ts';
205
+ if (context.doesFileExist(externalsPath)) {
206
+ additionsDebug(`Found ${externalsPath}, checking for i18next...`);
207
+ const externalsContent = context.getFile(externalsPath);
208
+ if (externalsContent) {
209
+ try {
210
+ const ast = recast.parse(externalsContent, {
211
+ parser: typeScriptParser,
212
+ });
213
+
214
+ let hasChanges = false;
215
+
216
+ // Find the externals array
217
+ recast.visit(ast, {
218
+ visitVariableDeclarator(path) {
219
+ const { node } = path;
220
+
221
+ if (
222
+ node.id.type === 'Identifier' &&
223
+ node.id.name === 'externals' &&
224
+ node.init &&
225
+ node.init.type === 'ArrayExpression'
226
+ ) {
227
+ additionsDebug('Found externals array in externals.ts');
228
+ if (addI18nextToExternalsArray(node.init)) {
229
+ hasChanges = true;
230
+ }
231
+ }
232
+
233
+ return this.traverse(path);
234
+ },
235
+ });
236
+
237
+ // Only update the file if we made changes
238
+ if (hasChanges) {
239
+ const output = recast.print(ast, {
240
+ tabWidth: 2,
241
+ trailingComma: true,
242
+ lineTerminator: '\n',
243
+ });
244
+ context.updateFile(externalsPath, output.code);
245
+ additionsDebug(`Updated ${externalsPath} with i18next external`);
246
+ }
247
+ return;
248
+ } catch (error) {
249
+ additionsDebug(`Error updating ${externalsPath}:`, error);
250
+ }
251
+ }
252
+ }
253
+
254
+ // Fall back to legacy structure: .config/webpack/webpack.config.ts with inline externals
255
+ const webpackConfigPath = '.config/webpack/webpack.config.ts';
256
+ additionsDebug(`Checking for ${webpackConfigPath}...`);
257
+ if (context.doesFileExist(webpackConfigPath)) {
258
+ additionsDebug(`Found ${webpackConfigPath}, checking for inline externals...`);
259
+ const webpackContent = context.getFile(webpackConfigPath);
260
+ if (webpackContent) {
261
+ try {
262
+ const ast = recast.parse(webpackContent, {
263
+ parser: typeScriptParser,
264
+ });
265
+
266
+ let hasChanges = false;
267
+ let foundExternals = false;
268
+
269
+ // Find the externals property in the Configuration object
270
+ // It can be in baseConfig or any variable with an object initializer
271
+ recast.visit(ast, {
272
+ visitObjectExpression(path) {
273
+ const { node } = path;
274
+ const properties = node.properties;
275
+
276
+ if (properties) {
277
+ for (const prop of properties) {
278
+ // Handle both Property and ObjectProperty types
279
+ if (prop && (prop.type === 'Property' || prop.type === 'ObjectProperty')) {
280
+ const key = 'key' in prop ? prop.key : null;
281
+ const value = 'value' in prop ? prop.value : null;
282
+
283
+ if (
284
+ key &&
285
+ key.type === 'Identifier' &&
286
+ key.name === 'externals' &&
287
+ value &&
288
+ value.type === 'ArrayExpression'
289
+ ) {
290
+ foundExternals = true;
291
+ additionsDebug('Found externals property in webpack.config.ts');
292
+ if (addI18nextToExternalsArray(value)) {
293
+ hasChanges = true;
294
+ }
295
+ // Don't break, continue to check all object expressions
296
+ }
297
+ }
298
+ }
299
+ }
300
+
301
+ return this.traverse(path);
302
+ },
303
+ visitProperty(path) {
304
+ const { node } = path;
305
+
306
+ // Also check properties directly (fallback)
307
+ if (
308
+ node.key &&
309
+ node.key.type === 'Identifier' &&
310
+ node.key.name === 'externals' &&
311
+ node.value &&
312
+ node.value.type === 'ArrayExpression'
313
+ ) {
314
+ if (!foundExternals) {
315
+ foundExternals = true;
316
+ additionsDebug('Found externals property in webpack.config.ts (via visitProperty)');
317
+ }
318
+ if (addI18nextToExternalsArray(node.value)) {
319
+ hasChanges = true;
320
+ }
321
+ }
322
+
323
+ return this.traverse(path);
324
+ },
325
+ });
326
+
327
+ if (!foundExternals) {
328
+ additionsDebug('No externals property found in webpack.config.ts');
329
+ }
330
+
331
+ // Only update the file if we made changes
332
+ if (hasChanges) {
333
+ const output = recast.print(ast, {
334
+ tabWidth: 2,
335
+ trailingComma: true,
336
+ lineTerminator: '\n',
337
+ });
338
+ context.updateFile(webpackConfigPath, output.code);
339
+ additionsDebug(`Updated ${webpackConfigPath} with i18next external`);
340
+ } else if (foundExternals) {
341
+ additionsDebug('i18next already present in externals, no changes needed');
342
+ }
343
+ return;
344
+ } catch (error) {
345
+ additionsDebug(`Error updating ${webpackConfigPath}:`, error);
346
+ additionsDebug(`Error details: ${error instanceof Error ? error.message : String(error)}`);
347
+ }
348
+ } else {
349
+ additionsDebug(`File ${webpackConfigPath} exists but content is empty`);
350
+ }
351
+ } else {
352
+ additionsDebug(`File ${webpackConfigPath} does not exist`);
353
+ }
354
+
355
+ additionsDebug('No externals configuration found, skipping i18next external check');
356
+ } catch (error) {
357
+ additionsDebug(
358
+ `Unexpected error in ensureI18nextExternal: ${error instanceof Error ? error.message : String(error)}`
359
+ );
360
+ }
361
+ }