@grafana/create-plugin 6.2.0-canary.2233.19097561440.0 → 6.2.0-canary.2233.19368311379.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 +61 -0
- package/CONTRIBUTING.md +3 -0
- package/dist/codemods/additions/additions.js +8 -8
- package/dist/codemods/additions/scripts/example-addition.js +47 -0
- package/dist/codemods/migrations/manager.js +13 -40
- package/dist/codemods/migrations/migrations.js +34 -25
- package/dist/codemods/migrations/scripts/004-eslint9-flat-config.js +1 -2
- package/dist/codemods/migrations/scripts/005-react-18-3.js +20 -0
- package/dist/codemods/migrations/scripts/example-migration.js +7 -3
- package/dist/codemods/runner.js +38 -0
- package/dist/codemods/schema-parser.js +20 -0
- package/dist/codemods/utils.js +15 -6
- package/dist/commands/add.command.js +24 -55
- package/dist/commands/update.command.js +7 -41
- package/dist/utils/utils.checks.js +40 -0
- package/dist/utils/utils.config.js +1 -16
- package/package.json +3 -2
- package/src/codemods/additions/additions.test.ts +12 -0
- package/src/codemods/additions/additions.ts +9 -22
- package/src/codemods/additions/scripts/example-addition.test.ts +92 -0
- package/src/codemods/additions/scripts/example-addition.ts +62 -0
- package/src/codemods/migrations/fixtures/migrations.ts +19 -18
- package/src/codemods/migrations/manager.test.ts +67 -73
- package/src/codemods/migrations/manager.ts +17 -50
- package/src/codemods/migrations/migrations.test.ts +8 -5
- package/src/codemods/migrations/migrations.ts +38 -34
- package/src/codemods/migrations/scripts/004-eslint9-flat-config.ts +2 -2
- package/src/codemods/migrations/scripts/005-react-18-3.test.ts +145 -0
- package/src/codemods/migrations/scripts/005-react-18-3.ts +19 -0
- package/src/codemods/migrations/scripts/example-migration.test.ts +1 -1
- package/src/codemods/migrations/scripts/example-migration.ts +20 -3
- package/src/codemods/runner.ts +57 -0
- package/src/codemods/schema-parser.ts +27 -0
- package/src/codemods/types.ts +9 -14
- package/src/codemods/{migrations/utils.test.ts → utils.test.ts} +8 -7
- package/src/codemods/utils.ts +28 -36
- package/src/commands/add.command.ts +26 -62
- package/src/commands/update.command.ts +8 -47
- package/src/migrations/migrations.ts +44 -0
- package/src/utils/utils.checks.ts +47 -0
- package/src/utils/utils.config.ts +1 -28
- package/templates/common/_package.json +7 -5
- package/templates/github/workflows/bundle-stats.yml +1 -1
- package/templates/github/workflows/ci.yml +11 -11
- package/templates/github/workflows/cp-update.yml +9 -14
- package/templates/github/workflows/is-compatible.yml +3 -3
- package/templates/github/workflows/release.yml +1 -1
- package/vitest.config.ts +12 -0
- package/dist/codemods/additions/manager.js +0 -115
- package/dist/codemods/additions/scripts/add-i18n.js +0 -445
- package/dist/codemods/additions/utils.js +0 -10
- package/dist/codemods/migrations/utils.js +0 -10
- package/src/codemods/additions/manager.ts +0 -145
- package/src/codemods/additions/scripts/add-i18n.test.ts +0 -347
- package/src/codemods/additions/scripts/add-i18n.ts +0 -584
- package/src/codemods/additions/utils.ts +0 -12
- package/src/codemods/migrations/utils.ts +0 -12
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import { additionsDebug, printChanges } from './utils.js';
|
|
2
|
-
import defaultAdditions from './additions.js';
|
|
3
|
-
import { formatFiles, flushChanges, installNPMDependencies } from '../utils.js';
|
|
4
|
-
import { getConfig, isFeatureEnabled, setFeatureFlag } from '../../utils/utils.config.js';
|
|
5
|
-
import { Context } from '../context.js';
|
|
6
|
-
import { output } from '../../utils/utils.console.js';
|
|
7
|
-
|
|
8
|
-
function getAvailableAdditions(additions = defaultAdditions.additions) {
|
|
9
|
-
return additions;
|
|
10
|
-
}
|
|
11
|
-
function getAdditionByName(name, additions = defaultAdditions.additions) {
|
|
12
|
-
return additions[name];
|
|
13
|
-
}
|
|
14
|
-
async function loadAdditionModule(addition) {
|
|
15
|
-
try {
|
|
16
|
-
const module = await import(addition.scriptPath);
|
|
17
|
-
return module;
|
|
18
|
-
} catch (error) {
|
|
19
|
-
additionsDebug('Failed to load addition module for "%s" from %s: %O', addition.name, addition.scriptPath, error);
|
|
20
|
-
return null;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
async function getAdditionFlags(addition) {
|
|
24
|
-
const module = await loadAdditionModule(addition);
|
|
25
|
-
return module?.flags || [];
|
|
26
|
-
}
|
|
27
|
-
async function parseAdditionFlags(addition, argv) {
|
|
28
|
-
const module = await loadAdditionModule(addition);
|
|
29
|
-
if (module?.parseFlags) {
|
|
30
|
-
return module.parseFlags(argv);
|
|
31
|
-
}
|
|
32
|
-
return {};
|
|
33
|
-
}
|
|
34
|
-
async function validateAdditionOptions(addition, options) {
|
|
35
|
-
const flags = await getAdditionFlags(addition);
|
|
36
|
-
if (!flags || flags.length === 0) {
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
const missingFlags = [];
|
|
40
|
-
for (const flag of flags) {
|
|
41
|
-
if (flag.required) {
|
|
42
|
-
const value = options[flag.name];
|
|
43
|
-
if (value === void 0 || value === null || Array.isArray(value) && value.length === 0) {
|
|
44
|
-
missingFlags.push(flag.name);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
if (missingFlags.length > 0) {
|
|
49
|
-
const flagDocs = flags.filter((f) => missingFlags.includes(f.name)).map((f) => ` --${f.name}: ${f.description}`);
|
|
50
|
-
throw new Error(
|
|
51
|
-
`Missing required flag${missingFlags.length > 1 ? "s" : ""}:
|
|
52
|
-
|
|
53
|
-
` + flagDocs.join("\n") + `
|
|
54
|
-
|
|
55
|
-
Example: npx @grafana/create-plugin add ${addition.name} --${missingFlags[0]}=value`
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
async function runAdditionByName(additionName, argv) {
|
|
60
|
-
const addition = getAdditionByName(additionName);
|
|
61
|
-
if (!addition) {
|
|
62
|
-
const availableAdditions = getAvailableAdditions();
|
|
63
|
-
const additionsList = Object.keys(availableAdditions);
|
|
64
|
-
throw new Error(`Unknown addition: ${additionName}
|
|
65
|
-
|
|
66
|
-
Available additions: ${additionsList.join(", ")}`);
|
|
67
|
-
}
|
|
68
|
-
const options = await parseAdditionFlags(addition, argv);
|
|
69
|
-
await validateAdditionOptions(addition, options);
|
|
70
|
-
await runAddition(addition, options);
|
|
71
|
-
}
|
|
72
|
-
async function runAddition(addition, additionOptions = {}) {
|
|
73
|
-
const basePath = process.cwd();
|
|
74
|
-
const config = getConfig();
|
|
75
|
-
if (isFeatureEnabled(config.features, addition.featureName)) {
|
|
76
|
-
output.log({
|
|
77
|
-
title: `Addition '${addition.name}' is already enabled`,
|
|
78
|
-
body: [`The feature flag '${addition.featureName}' is already set to true in .cprc.json.`, "No changes needed."]
|
|
79
|
-
});
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
output.log({
|
|
83
|
-
title: `Running addition: ${addition.name}`,
|
|
84
|
-
body: [addition.description]
|
|
85
|
-
});
|
|
86
|
-
try {
|
|
87
|
-
const context = new Context(basePath);
|
|
88
|
-
const updatedContext = await executeAddition(addition, context, additionOptions);
|
|
89
|
-
additionsDebug(`context for "${addition.name} (${addition.scriptPath})":`);
|
|
90
|
-
additionsDebug("%O", updatedContext.listChanges());
|
|
91
|
-
await formatFiles(updatedContext);
|
|
92
|
-
flushChanges(updatedContext);
|
|
93
|
-
printChanges(updatedContext, addition.name, addition);
|
|
94
|
-
installNPMDependencies(updatedContext);
|
|
95
|
-
await setFeatureFlag(addition.featureName, true);
|
|
96
|
-
additionsDebug(`Set feature flag '${addition.featureName}' to true in .cprc.json`);
|
|
97
|
-
output.success({
|
|
98
|
-
title: `Successfully added ${addition.name} to your plugin.`
|
|
99
|
-
});
|
|
100
|
-
} catch (error) {
|
|
101
|
-
if (error instanceof Error) {
|
|
102
|
-
throw new Error(`Error running addition "${addition.name} (${addition.scriptPath})": ${error.message}`);
|
|
103
|
-
}
|
|
104
|
-
throw error;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
async function executeAddition(addition, context, options = {}) {
|
|
108
|
-
const module = await loadAdditionModule(addition);
|
|
109
|
-
if (!module) {
|
|
110
|
-
throw new Error(`Failed to load addition module for ${addition.name}`);
|
|
111
|
-
}
|
|
112
|
-
return module.default(context, options);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
export { executeAddition, getAdditionByName, getAdditionFlags, getAvailableAdditions, parseAdditionFlags, runAddition, runAdditionByName };
|
|
@@ -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 };
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { debug } from '../../utils/utils.cli.js';
|
|
2
|
-
import { printChanges as printChanges$1 } from '../utils.js';
|
|
3
|
-
|
|
4
|
-
const additionsDebug = debug.extend("additions");
|
|
5
|
-
function printChanges(context, key, addition) {
|
|
6
|
-
additionsDebug("printChanges for addition: %s", key);
|
|
7
|
-
printChanges$1(context, key, addition);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export { additionsDebug, printChanges };
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { debug } from '../../utils/utils.cli.js';
|
|
2
|
-
import { printChanges as printChanges$1 } from '../utils.js';
|
|
3
|
-
|
|
4
|
-
const migrationsDebug = debug.extend("migrations");
|
|
5
|
-
function printChanges(context, key, migration) {
|
|
6
|
-
migrationsDebug("printChanges for migration: %s", key);
|
|
7
|
-
printChanges$1(context, key, migration);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export { migrationsDebug, printChanges };
|