@grafana/create-plugin 6.1.1 → 6.2.0-canary.2233.18586197736.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/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 +335 -0
- package/src/additions/scripts/add-i18n.ts +412 -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,412 @@
|
|
|
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
|
+
// 1. Update docker-compose.yaml with feature toggle
|
|
27
|
+
updateDockerCompose(context);
|
|
28
|
+
|
|
29
|
+
// 2. Update plugin.json with languages and grafanaDependency
|
|
30
|
+
updatePluginJson(context, locales);
|
|
31
|
+
|
|
32
|
+
// 3. Create locale folders and files
|
|
33
|
+
createLocaleFiles(context, locales);
|
|
34
|
+
|
|
35
|
+
// 4. Add @grafana/i18n dependency
|
|
36
|
+
addI18nDependency(context);
|
|
37
|
+
|
|
38
|
+
// 5. Update eslint.config.mjs if needed
|
|
39
|
+
updateEslintConfig(context);
|
|
40
|
+
|
|
41
|
+
// 6. Add i18n initialization to module file
|
|
42
|
+
addI18nInitialization(context);
|
|
43
|
+
|
|
44
|
+
// 7. Add i18next-cli as dev dependency and add script
|
|
45
|
+
addI18nextCli(context);
|
|
46
|
+
|
|
47
|
+
// 8. Create i18next.config.ts
|
|
48
|
+
createI18nextConfig(context);
|
|
49
|
+
|
|
50
|
+
return context;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function isI18nConfigured(context: Context): boolean {
|
|
54
|
+
// Check if plugin.json has languages field
|
|
55
|
+
if (!context.doesFileExist('src/plugin.json')) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const pluginJsonRaw = context.getFile('src/plugin.json');
|
|
60
|
+
if (!pluginJsonRaw) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
const pluginJson = JSON.parse(pluginJsonRaw);
|
|
66
|
+
if (pluginJson.languages && Array.isArray(pluginJson.languages) && pluginJson.languages.length > 0) {
|
|
67
|
+
additionsDebug('Found languages in plugin.json, i18n already configured');
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
} catch (error) {
|
|
71
|
+
additionsDebug('Error parsing plugin.json:', error);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function updateDockerCompose(context: Context): void {
|
|
78
|
+
if (!context.doesFileExist('docker-compose.yaml')) {
|
|
79
|
+
additionsDebug('docker-compose.yaml not found, skipping');
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const composeContent = context.getFile('docker-compose.yaml');
|
|
84
|
+
if (!composeContent) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
try {
|
|
89
|
+
const composeDoc = parseDocument(composeContent);
|
|
90
|
+
const currentEnv = composeDoc.getIn(['services', 'grafana', 'environment']);
|
|
91
|
+
|
|
92
|
+
if (!currentEnv) {
|
|
93
|
+
additionsDebug('No environment section found in docker-compose.yaml, skipping');
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Check if the feature toggle is already set
|
|
98
|
+
if (typeof currentEnv === 'object') {
|
|
99
|
+
const envMap = currentEnv as any;
|
|
100
|
+
const toggleValue = envMap.get('GF_FEATURE_TOGGLES_ENABLE');
|
|
101
|
+
|
|
102
|
+
if (toggleValue) {
|
|
103
|
+
const toggleStr = toggleValue.toString();
|
|
104
|
+
if (toggleStr.includes('localizationForPlugins')) {
|
|
105
|
+
additionsDebug('localizationForPlugins already in GF_FEATURE_TOGGLES_ENABLE');
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
// Append to existing feature toggles
|
|
109
|
+
composeDoc.setIn(
|
|
110
|
+
['services', 'grafana', 'environment', 'GF_FEATURE_TOGGLES_ENABLE'],
|
|
111
|
+
`${toggleStr},localizationForPlugins`
|
|
112
|
+
);
|
|
113
|
+
} else {
|
|
114
|
+
// Set new feature toggle
|
|
115
|
+
composeDoc.setIn(['services', 'grafana', 'environment', 'GF_FEATURE_TOGGLES_ENABLE'], 'localizationForPlugins');
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
context.updateFile('docker-compose.yaml', stringify(composeDoc));
|
|
119
|
+
additionsDebug('Updated docker-compose.yaml with localizationForPlugins feature toggle');
|
|
120
|
+
}
|
|
121
|
+
} catch (error) {
|
|
122
|
+
additionsDebug('Error updating docker-compose.yaml:', error);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function updatePluginJson(context: Context, locales: string[]): void {
|
|
127
|
+
if (!context.doesFileExist('src/plugin.json')) {
|
|
128
|
+
additionsDebug('src/plugin.json not found, skipping');
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const pluginJsonRaw = context.getFile('src/plugin.json');
|
|
133
|
+
if (!pluginJsonRaw) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
try {
|
|
138
|
+
const pluginJson = JSON.parse(pluginJsonRaw);
|
|
139
|
+
|
|
140
|
+
// Add languages array
|
|
141
|
+
pluginJson.languages = locales;
|
|
142
|
+
|
|
143
|
+
// Ensure grafanaDependency is >= 12.1.0
|
|
144
|
+
if (!pluginJson.dependencies) {
|
|
145
|
+
pluginJson.dependencies = {};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const currentGrafanaDep = pluginJson.dependencies.grafanaDependency || '>=11.0.0';
|
|
149
|
+
const minVersion = coerce('12.1.0');
|
|
150
|
+
const currentVersion = coerce(currentGrafanaDep.replace(/^[><=]+/, ''));
|
|
151
|
+
|
|
152
|
+
if (!currentVersion || !minVersion || !gte(currentVersion, minVersion)) {
|
|
153
|
+
pluginJson.dependencies.grafanaDependency = '>=12.1.0';
|
|
154
|
+
additionsDebug('Updated grafanaDependency to >=12.1.0');
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
context.updateFile('src/plugin.json', JSON.stringify(pluginJson, null, 2));
|
|
158
|
+
additionsDebug('Updated src/plugin.json with languages:', locales);
|
|
159
|
+
} catch (error) {
|
|
160
|
+
additionsDebug('Error updating src/plugin.json:', error);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function createLocaleFiles(context: Context, locales: string[]): void {
|
|
165
|
+
// Get plugin ID from plugin.json
|
|
166
|
+
const pluginJsonRaw = context.getFile('src/plugin.json');
|
|
167
|
+
if (!pluginJsonRaw) {
|
|
168
|
+
additionsDebug('Cannot create locale files without plugin.json');
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
try {
|
|
173
|
+
const pluginJson = JSON.parse(pluginJsonRaw);
|
|
174
|
+
const pluginId = pluginJson.id;
|
|
175
|
+
|
|
176
|
+
if (!pluginId) {
|
|
177
|
+
additionsDebug('No plugin ID found in plugin.json');
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Create locale files for each locale
|
|
182
|
+
for (const locale of locales) {
|
|
183
|
+
const localePath = `src/locales/${locale}/${pluginId}.json`;
|
|
184
|
+
|
|
185
|
+
if (!context.doesFileExist(localePath)) {
|
|
186
|
+
context.addFile(localePath, JSON.stringify({}, null, 2));
|
|
187
|
+
additionsDebug(`Created ${localePath}`);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
} catch (error) {
|
|
191
|
+
additionsDebug('Error creating locale files:', error);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function addI18nDependency(context: Context): void {
|
|
196
|
+
addDependenciesToPackageJson(context, { '@grafana/i18n': '^1.0.0' }, {});
|
|
197
|
+
additionsDebug('Added @grafana/i18n dependency');
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function addI18nextCli(context: Context): void {
|
|
201
|
+
// Add i18next-cli as dev dependency
|
|
202
|
+
addDependenciesToPackageJson(context, {}, { 'i18next-cli': '^1.1.1' });
|
|
203
|
+
|
|
204
|
+
// Add i18n-extract script to package.json
|
|
205
|
+
const packageJsonRaw = context.getFile('package.json');
|
|
206
|
+
if (!packageJsonRaw) {
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
try {
|
|
211
|
+
const packageJson = JSON.parse(packageJsonRaw);
|
|
212
|
+
|
|
213
|
+
if (!packageJson.scripts) {
|
|
214
|
+
packageJson.scripts = {};
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// Only add if not already present
|
|
218
|
+
if (!packageJson.scripts['i18n-extract']) {
|
|
219
|
+
packageJson.scripts['i18n-extract'] = 'i18next-cli extract --sync-primary';
|
|
220
|
+
context.updateFile('package.json', JSON.stringify(packageJson, null, 2));
|
|
221
|
+
additionsDebug('Added i18n-extract script to package.json');
|
|
222
|
+
}
|
|
223
|
+
} catch (error) {
|
|
224
|
+
additionsDebug('Error adding i18n-extract script:', error);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function updateEslintConfig(context: Context): void {
|
|
229
|
+
if (!context.doesFileExist('eslint.config.mjs')) {
|
|
230
|
+
additionsDebug('eslint.config.mjs not found, skipping');
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const eslintConfigRaw = context.getFile('eslint.config.mjs');
|
|
235
|
+
if (!eslintConfigRaw) {
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// Check if @grafana/eslint-plugin-plugins is already configured
|
|
240
|
+
if (eslintConfigRaw.includes('@grafana/eslint-plugin-plugins')) {
|
|
241
|
+
additionsDebug('ESLint i18n rule already configured');
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
try {
|
|
246
|
+
const ast = recast.parse(eslintConfigRaw, {
|
|
247
|
+
parser: require('recast/parsers/babel-ts'),
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
// Find the import section and add the plugin import
|
|
251
|
+
const imports = ast.program.body.filter((node: any) => node.type === 'ImportDeclaration');
|
|
252
|
+
const lastImport = imports[imports.length - 1];
|
|
253
|
+
|
|
254
|
+
if (lastImport) {
|
|
255
|
+
const pluginImport = builders.importDeclaration(
|
|
256
|
+
[builders.importDefaultSpecifier(builders.identifier('grafanaPluginsPlugin'))],
|
|
257
|
+
builders.literal('@grafana/eslint-plugin-plugins')
|
|
258
|
+
);
|
|
259
|
+
|
|
260
|
+
const lastImportIndex = ast.program.body.indexOf(lastImport);
|
|
261
|
+
ast.program.body.splice(lastImportIndex + 1, 0, pluginImport);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// Find the defineConfig array and add the plugin config
|
|
265
|
+
recast.visit(ast, {
|
|
266
|
+
visitCallExpression(path: any) {
|
|
267
|
+
if (path.node.callee.name === 'defineConfig' && path.node.arguments[0]?.type === 'ArrayExpression') {
|
|
268
|
+
const configArray = path.node.arguments[0];
|
|
269
|
+
|
|
270
|
+
// Add the grafana plugins config object
|
|
271
|
+
const pluginsConfig = builders.objectExpression([
|
|
272
|
+
builders.property(
|
|
273
|
+
'init',
|
|
274
|
+
builders.identifier('plugins'),
|
|
275
|
+
builders.objectExpression([
|
|
276
|
+
builders.property(
|
|
277
|
+
'init',
|
|
278
|
+
builders.literal('grafanaPlugins'),
|
|
279
|
+
builders.identifier('grafanaPluginsPlugin')
|
|
280
|
+
),
|
|
281
|
+
])
|
|
282
|
+
),
|
|
283
|
+
builders.property(
|
|
284
|
+
'init',
|
|
285
|
+
builders.identifier('rules'),
|
|
286
|
+
builders.objectExpression([
|
|
287
|
+
builders.property(
|
|
288
|
+
'init',
|
|
289
|
+
builders.literal('grafanaPlugins/no-untranslated-strings'),
|
|
290
|
+
builders.literal('warn')
|
|
291
|
+
),
|
|
292
|
+
])
|
|
293
|
+
),
|
|
294
|
+
]);
|
|
295
|
+
|
|
296
|
+
configArray.elements.push(pluginsConfig);
|
|
297
|
+
}
|
|
298
|
+
this.traverse(path);
|
|
299
|
+
},
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
const output = recast.print(ast, {
|
|
303
|
+
tabWidth: 2,
|
|
304
|
+
trailingComma: true,
|
|
305
|
+
lineTerminator: '\n',
|
|
306
|
+
}).code;
|
|
307
|
+
|
|
308
|
+
context.updateFile('eslint.config.mjs', output);
|
|
309
|
+
additionsDebug('Updated eslint.config.mjs with i18n linting rules');
|
|
310
|
+
} catch (error) {
|
|
311
|
+
additionsDebug('Error updating eslint.config.mjs:', error);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function createI18nextConfig(context: Context): void {
|
|
316
|
+
if (context.doesFileExist('i18next.config.ts')) {
|
|
317
|
+
additionsDebug('i18next.config.ts already exists, skipping');
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
const pluginJsonRaw = context.getFile('src/plugin.json');
|
|
322
|
+
if (!pluginJsonRaw) {
|
|
323
|
+
additionsDebug('Cannot create i18next.config.ts without plugin.json');
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
try {
|
|
328
|
+
const pluginJson = JSON.parse(pluginJsonRaw);
|
|
329
|
+
const pluginId = pluginJson.id;
|
|
330
|
+
|
|
331
|
+
if (!pluginId) {
|
|
332
|
+
additionsDebug('No plugin ID found in plugin.json');
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
const i18nextConfig = `import { defineConfig } from 'i18next-cli';
|
|
337
|
+
import pluginJson from './src/plugin.json';
|
|
338
|
+
|
|
339
|
+
export default defineConfig({
|
|
340
|
+
locales: pluginJson.languages,
|
|
341
|
+
extract: {
|
|
342
|
+
input: ['src/**/*.{tsx,ts}'],
|
|
343
|
+
output: 'src/locales/{{language}}/{{namespace}}.json',
|
|
344
|
+
defaultNS: pluginJson.id,
|
|
345
|
+
functions: ['t', '*.t'],
|
|
346
|
+
transComponents: ['Trans'],
|
|
347
|
+
},
|
|
348
|
+
});
|
|
349
|
+
`;
|
|
350
|
+
|
|
351
|
+
context.addFile('i18next.config.ts', i18nextConfig);
|
|
352
|
+
additionsDebug('Created i18next.config.ts');
|
|
353
|
+
} catch (error) {
|
|
354
|
+
additionsDebug('Error creating i18next.config.ts:', error);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
function addI18nInitialization(context: Context): void {
|
|
359
|
+
// Find module.ts or module.tsx
|
|
360
|
+
const moduleTsPath = context.doesFileExist('src/module.ts')
|
|
361
|
+
? 'src/module.ts'
|
|
362
|
+
: context.doesFileExist('src/module.tsx')
|
|
363
|
+
? 'src/module.tsx'
|
|
364
|
+
: null;
|
|
365
|
+
|
|
366
|
+
if (!moduleTsPath) {
|
|
367
|
+
additionsDebug('No module.ts or module.tsx found, skipping i18n initialization');
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
const moduleContent = context.getFile(moduleTsPath);
|
|
372
|
+
if (!moduleContent) {
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
// Check if i18n is already initialized
|
|
377
|
+
if (moduleContent.includes('@grafana/i18n')) {
|
|
378
|
+
additionsDebug('i18n already initialized in module file');
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
try {
|
|
383
|
+
const ast = recast.parse(moduleContent, {
|
|
384
|
+
parser: require('recast/parsers/babel-ts'),
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
// Add import for i18n
|
|
388
|
+
const i18nImport = builders.importDeclaration(
|
|
389
|
+
[builders.importSpecifier(builders.identifier('i18n'))],
|
|
390
|
+
builders.literal('@grafana/i18n')
|
|
391
|
+
);
|
|
392
|
+
|
|
393
|
+
// Add the import after the first import statement
|
|
394
|
+
const firstImportIndex = ast.program.body.findIndex((node: any) => node.type === 'ImportDeclaration');
|
|
395
|
+
if (firstImportIndex !== -1) {
|
|
396
|
+
ast.program.body.splice(firstImportIndex + 1, 0, i18nImport);
|
|
397
|
+
} else {
|
|
398
|
+
ast.program.body.unshift(i18nImport);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
const output = recast.print(ast, {
|
|
402
|
+
tabWidth: 2,
|
|
403
|
+
trailingComma: true,
|
|
404
|
+
lineTerminator: '\n',
|
|
405
|
+
}).code;
|
|
406
|
+
|
|
407
|
+
context.updateFile(moduleTsPath, output);
|
|
408
|
+
additionsDebug(`Updated ${moduleTsPath} with i18n import`);
|
|
409
|
+
} catch (error) {
|
|
410
|
+
additionsDebug('Error updating module file:', error);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
@@ -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
|
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import Enquirer from 'enquirer';
|
|
2
|
+
import { output } from '../../utils/utils.console.js';
|
|
3
|
+
|
|
4
|
+
// Common locales supported by Grafana
|
|
5
|
+
// Reference: https://github.com/grafana/grafana/blob/main/packages/grafana-i18n/src/constants.ts
|
|
6
|
+
const COMMON_LOCALES = [
|
|
7
|
+
{ name: 'en-US', message: 'English (US)' },
|
|
8
|
+
{ name: 'es-ES', message: 'Spanish (Spain)' },
|
|
9
|
+
{ name: 'fr-FR', message: 'French (France)' },
|
|
10
|
+
{ name: 'de-DE', message: 'German (Germany)' },
|
|
11
|
+
{ name: 'zh-Hans', message: 'Chinese (Simplified)' },
|
|
12
|
+
{ name: 'pt-BR', message: 'Portuguese (Brazil)' },
|
|
13
|
+
{ name: 'sv-SE', message: 'Swedish (Sweden)' },
|
|
14
|
+
{ name: 'nl-NL', message: 'Dutch (Netherlands)' },
|
|
15
|
+
{ name: 'ja-JP', message: 'Japanese (Japan)' },
|
|
16
|
+
{ name: 'it-IT', message: 'Italian (Italy)' },
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
export type I18nOptions = {
|
|
20
|
+
locales: string[];
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export async function promptI18nOptions(): Promise<I18nOptions> {
|
|
24
|
+
const enquirer = new Enquirer();
|
|
25
|
+
|
|
26
|
+
output.log({
|
|
27
|
+
title: 'Configure internationalization (i18n) for your plugin',
|
|
28
|
+
body: [
|
|
29
|
+
'Select the locales you want to support. At least one locale must be selected.',
|
|
30
|
+
'Use space to select, enter to continue.',
|
|
31
|
+
],
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const localeChoices = COMMON_LOCALES.map((locale) => ({
|
|
35
|
+
name: locale.name,
|
|
36
|
+
message: locale.message,
|
|
37
|
+
value: locale.name,
|
|
38
|
+
}));
|
|
39
|
+
|
|
40
|
+
let selectedLocales: string[] = [];
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
const result = (await enquirer.prompt({
|
|
44
|
+
type: 'multiselect',
|
|
45
|
+
name: 'locales',
|
|
46
|
+
message: 'Select locales to support:',
|
|
47
|
+
choices: localeChoices,
|
|
48
|
+
initial: [0], // Pre-select en-US by default
|
|
49
|
+
validate(value: string[]) {
|
|
50
|
+
if (value.length === 0) {
|
|
51
|
+
return 'At least one locale must be selected';
|
|
52
|
+
}
|
|
53
|
+
return true;
|
|
54
|
+
},
|
|
55
|
+
} as any)) as { locales: string[] };
|
|
56
|
+
|
|
57
|
+
selectedLocales = result.locales;
|
|
58
|
+
} catch (error) {
|
|
59
|
+
// User cancelled the prompt
|
|
60
|
+
output.warning({ title: 'Addition cancelled by user.' });
|
|
61
|
+
process.exit(0);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Ask if they want to add additional locales
|
|
65
|
+
try {
|
|
66
|
+
const addMoreResult = (await enquirer.prompt({
|
|
67
|
+
type: 'input',
|
|
68
|
+
name: 'additionalLocales',
|
|
69
|
+
message: 'Enter additional locale codes (comma-separated, e.g., "ko-KR,ru-RU") or press enter to skip:',
|
|
70
|
+
} as any)) as { additionalLocales: string };
|
|
71
|
+
|
|
72
|
+
const additionalLocalesInput = addMoreResult.additionalLocales;
|
|
73
|
+
|
|
74
|
+
if (additionalLocalesInput && additionalLocalesInput.trim()) {
|
|
75
|
+
const additionalLocales = additionalLocalesInput
|
|
76
|
+
.split(',')
|
|
77
|
+
.map((locale: string) => locale.trim())
|
|
78
|
+
.filter((locale: string) => locale.length > 0 && !selectedLocales.includes(locale));
|
|
79
|
+
|
|
80
|
+
selectedLocales.push(...additionalLocales);
|
|
81
|
+
}
|
|
82
|
+
} catch (error) {
|
|
83
|
+
// User cancelled, just continue with what we have
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
output.log({
|
|
87
|
+
title: `Selected locales: ${selectedLocales.join(', ')}`,
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
locales: selectedLocales,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { getAdditionByName, getAvailableAdditions, runAddition } from '../additions/manager.js';
|
|
2
|
+
import { isGitDirectory, isGitDirectoryClean } from '../utils/utils.git.js';
|
|
3
|
+
|
|
4
|
+
import { isPluginDirectory } from '../utils/utils.plugin.js';
|
|
5
|
+
import minimist from 'minimist';
|
|
6
|
+
import { output } from '../utils/utils.console.js';
|
|
7
|
+
import { promptI18nOptions } from './add/prompts.js';
|
|
8
|
+
|
|
9
|
+
export const add = async (argv: minimist.ParsedArgs) => {
|
|
10
|
+
const subCommand = argv._[1];
|
|
11
|
+
|
|
12
|
+
if (!subCommand) {
|
|
13
|
+
const availableAdditions = getAvailableAdditions();
|
|
14
|
+
const additionsList = Object.values(availableAdditions).map(
|
|
15
|
+
(addition) => `${addition.name} - ${addition.description}`
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
output.error({
|
|
19
|
+
title: 'No addition specified',
|
|
20
|
+
body: [
|
|
21
|
+
'Usage: npx @grafana/create-plugin add <addition-name>',
|
|
22
|
+
'',
|
|
23
|
+
'Available additions:',
|
|
24
|
+
...output.bulletList(additionsList),
|
|
25
|
+
],
|
|
26
|
+
});
|
|
27
|
+
process.exit(1);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
await performPreAddChecks(argv);
|
|
31
|
+
|
|
32
|
+
const addition = getAdditionByName(subCommand);
|
|
33
|
+
|
|
34
|
+
if (!addition) {
|
|
35
|
+
const availableAdditions = getAvailableAdditions();
|
|
36
|
+
const additionsList = Object.values(availableAdditions).map((addition) => addition.name);
|
|
37
|
+
|
|
38
|
+
output.error({
|
|
39
|
+
title: `Unknown addition: ${subCommand}`,
|
|
40
|
+
body: ['Available additions:', ...output.bulletList(additionsList)],
|
|
41
|
+
});
|
|
42
|
+
process.exit(1);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
// Gather options based on the addition type
|
|
47
|
+
let options = {};
|
|
48
|
+
|
|
49
|
+
switch (addition.name) {
|
|
50
|
+
case 'i18n':
|
|
51
|
+
options = await promptI18nOptions();
|
|
52
|
+
break;
|
|
53
|
+
default:
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const commitChanges = argv.commit;
|
|
58
|
+
await runAddition(addition, options, { commitChanges });
|
|
59
|
+
} catch (error) {
|
|
60
|
+
if (error instanceof Error) {
|
|
61
|
+
output.error({
|
|
62
|
+
title: 'Addition failed',
|
|
63
|
+
body: [error.message],
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
process.exit(1);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
async function performPreAddChecks(argv: minimist.ParsedArgs) {
|
|
71
|
+
if (!(await isGitDirectory()) && !argv.force) {
|
|
72
|
+
output.error({
|
|
73
|
+
title: 'You are not inside a git directory',
|
|
74
|
+
body: [
|
|
75
|
+
`In order to proceed please run ${output.formatCode('git init')} in the root of your project and commit your changes.`,
|
|
76
|
+
`(This check is necessary to make sure that the changes are easy to revert and don't interfere with any changes you currently have.`,
|
|
77
|
+
`In case you want to proceed as is please use the ${output.formatCode('--force')} flag.)`,
|
|
78
|
+
],
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
process.exit(1);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (!(await isGitDirectoryClean()) && !argv.force) {
|
|
85
|
+
output.error({
|
|
86
|
+
title: 'Please clean your repository working tree before adding features.',
|
|
87
|
+
body: [
|
|
88
|
+
'Commit your changes or stash them.',
|
|
89
|
+
`(This check is necessary to make sure that the changes are easy to revert and don't mess with any changes you currently have.`,
|
|
90
|
+
`In case you want to proceed as is please use the ${output.formatCode('--force')} flag.)`,
|
|
91
|
+
],
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
process.exit(1);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (!isPluginDirectory() && !argv.force) {
|
|
98
|
+
output.error({
|
|
99
|
+
title: 'Are you inside a plugin directory?',
|
|
100
|
+
body: [
|
|
101
|
+
`We couldn't find a "src/plugin.json" file under your current directory.`,
|
|
102
|
+
`(Please make sure to run this command from the root of your plugin folder. In case you want to proceed as is please use the ${output.formatCode('--force')} flag.)`,
|
|
103
|
+
],
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
process.exit(1);
|
|
107
|
+
}
|
|
108
|
+
}
|
package/src/commands/index.ts
CHANGED
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"license": "Apache-2.0",
|
|
20
20
|
"devDependencies": {
|
|
21
21
|
{{#if useCypress}} "@grafana/e2e": "^11.0.7",
|
|
22
|
-
"@grafana/e2e-selectors": "^12.
|
|
22
|
+
"@grafana/e2e-selectors": "^12.2.0",{{/if}}
|
|
23
23
|
"@grafana/eslint-config": "^8.2.0",{{#if usePlaywright}}
|
|
24
24
|
"@grafana/plugin-e2e": "^2.2.0",{{/if}}
|
|
25
25
|
"@grafana/tsconfig": "^2.0.0",{{#if usePlaywright}}
|