@grafana/create-plugin 6.2.0-canary.2233.19133609453.0 → 6.2.0-canary.2233.19424871609.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.
Files changed (54) hide show
  1. package/CHANGELOG.md +61 -0
  2. package/CONTRIBUTING.md +3 -0
  3. package/dist/codemods/additions/additions.js +6 -8
  4. package/dist/codemods/additions/scripts/example-addition.js +19 -33
  5. package/dist/codemods/migrations/manager.js +13 -40
  6. package/dist/codemods/migrations/migrations.js +33 -25
  7. package/dist/codemods/migrations/scripts/004-eslint9-flat-config.js +1 -2
  8. package/dist/codemods/migrations/scripts/005-react-18-3.js +20 -0
  9. package/dist/codemods/migrations/scripts/example-migration.js +7 -3
  10. package/dist/codemods/runner.js +38 -0
  11. package/dist/codemods/schema-parser.js +20 -0
  12. package/dist/codemods/utils.js +7 -4
  13. package/dist/commands/add.command.js +24 -55
  14. package/dist/commands/update.command.js +7 -41
  15. package/dist/utils/utils.checks.js +40 -0
  16. package/dist/utils/utils.config.js +1 -16
  17. package/package.json +3 -2
  18. package/src/codemods/additions/additions.test.ts +12 -0
  19. package/src/codemods/additions/additions.ts +7 -21
  20. package/src/codemods/additions/scripts/example-addition.test.ts +14 -33
  21. package/src/codemods/additions/scripts/example-addition.ts +27 -44
  22. package/src/codemods/migrations/fixtures/migrations.ts +19 -18
  23. package/src/codemods/migrations/manager.test.ts +67 -73
  24. package/src/codemods/migrations/manager.ts +17 -50
  25. package/src/codemods/migrations/migrations.test.ts +10 -5
  26. package/src/codemods/migrations/migrations.ts +37 -34
  27. package/src/codemods/migrations/scripts/004-eslint9-flat-config.ts +2 -2
  28. package/src/codemods/migrations/scripts/005-react-18-3.test.ts +145 -0
  29. package/src/codemods/migrations/scripts/005-react-18-3.ts +19 -0
  30. package/src/codemods/migrations/scripts/example-migration.test.ts +1 -1
  31. package/src/codemods/migrations/scripts/example-migration.ts +20 -3
  32. package/src/codemods/runner.ts +57 -0
  33. package/src/codemods/schema-parser.ts +27 -0
  34. package/src/codemods/types.ts +9 -14
  35. package/src/codemods/{migrations/utils.test.ts → utils.test.ts} +8 -7
  36. package/src/codemods/utils.ts +13 -35
  37. package/src/commands/add.command.ts +26 -62
  38. package/src/commands/update.command.ts +8 -47
  39. package/src/migrations/migrations.ts +44 -0
  40. package/src/utils/utils.checks.ts +47 -0
  41. package/src/utils/utils.config.ts +1 -28
  42. package/templates/common/_package.json +7 -5
  43. package/templates/github/workflows/bundle-stats.yml +1 -1
  44. package/templates/github/workflows/ci.yml +11 -11
  45. package/templates/github/workflows/cp-update.yml +9 -14
  46. package/templates/github/workflows/is-compatible.yml +3 -3
  47. package/templates/github/workflows/release.yml +1 -1
  48. package/vitest.config.ts +12 -0
  49. package/dist/codemods/additions/manager.js +0 -115
  50. package/dist/codemods/additions/utils.js +0 -10
  51. package/dist/codemods/migrations/utils.js +0 -10
  52. package/src/codemods/additions/manager.ts +0 -145
  53. package/src/codemods/additions/utils.ts +0 -12
  54. package/src/codemods/migrations/utils.ts +0 -12
package/vitest.config.ts CHANGED
@@ -9,5 +9,17 @@ export default mergeConfig(
9
9
  root: resolve(__dirname),
10
10
  setupFiles: ['./vitest.setup.ts'],
11
11
  },
12
+ plugins: [
13
+ // This plugin is used to convince Vitest the mocked virtual migrations exist.
14
+ // https://vitest.dev/guide/mocking/modules.html#mocking-non-existing-module
15
+ {
16
+ name: 'virtual-migrations',
17
+ resolveId(id) {
18
+ if (id === 'virtual-test-migration.js' || id === 'virtual-test-migration2.js') {
19
+ return id;
20
+ }
21
+ },
22
+ },
23
+ ],
12
24
  })
13
25
  );
@@ -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,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 };
@@ -1,145 +0,0 @@
1
- import type { AdditionModule, FlagDefinition } from '../types.js';
2
- import { additionsDebug, printChanges } from './utils.js';
3
- import defaultAdditions, { AdditionMeta } from './additions.js';
4
- import { flushChanges, formatFiles, installNPMDependencies } from '../utils.js';
5
- import { getConfig, isFeatureEnabled, setFeatureFlag } from '../../utils/utils.config.js';
6
-
7
- import { Context } from '../context.js';
8
- import { output } from '../../utils/utils.console.js';
9
-
10
- export type AdditionOptions = Record<string, any>;
11
-
12
- export function getAvailableAdditions(
13
- additions: Record<string, AdditionMeta> = defaultAdditions.additions
14
- ): Record<string, AdditionMeta> {
15
- return additions;
16
- }
17
-
18
- export function getAdditionByName(
19
- name: string,
20
- additions: Record<string, AdditionMeta> = defaultAdditions.additions
21
- ): AdditionMeta | undefined {
22
- return additions[name];
23
- }
24
-
25
- async function loadAdditionModule(addition: AdditionMeta): Promise<AdditionModule | null> {
26
- try {
27
- const module = (await import(addition.scriptPath)) as AdditionModule;
28
- return module;
29
- } catch (error) {
30
- additionsDebug('Failed to load addition module for "%s" from %s: %O', addition.name, addition.scriptPath, error);
31
- return null;
32
- }
33
- }
34
-
35
- export async function getAdditionFlags(addition: AdditionMeta): Promise<FlagDefinition[]> {
36
- const module = await loadAdditionModule(addition);
37
- return module?.flags || [];
38
- }
39
-
40
- export async function parseAdditionFlags(addition: AdditionMeta, argv: any): Promise<AdditionOptions> {
41
- const module = await loadAdditionModule(addition);
42
- if (module?.parseFlags) {
43
- return module.parseFlags(argv);
44
- }
45
- return {};
46
- }
47
-
48
- async function validateAdditionOptions(addition: AdditionMeta, options: AdditionOptions): Promise<void> {
49
- const flags = await getAdditionFlags(addition);
50
-
51
- if (!flags || flags.length === 0) {
52
- return;
53
- }
54
-
55
- const missingFlags: string[] = [];
56
-
57
- for (const flag of flags) {
58
- if (flag.required) {
59
- const value = options[flag.name];
60
- if (value === undefined || value === null || (Array.isArray(value) && value.length === 0)) {
61
- missingFlags.push(flag.name);
62
- }
63
- }
64
- }
65
-
66
- if (missingFlags.length > 0) {
67
- const flagDocs = flags.filter((f) => missingFlags.includes(f.name)).map((f) => ` --${f.name}: ${f.description}`);
68
-
69
- throw new Error(
70
- `Missing required flag${missingFlags.length > 1 ? 's' : ''}:\n\n` +
71
- flagDocs.join('\n') +
72
- `\n\nExample: npx @grafana/create-plugin add ${addition.name} --${missingFlags[0]}=value`
73
- );
74
- }
75
- }
76
-
77
- export async function runAdditionByName(additionName: string, argv: any): Promise<void> {
78
- const addition = getAdditionByName(additionName);
79
- if (!addition) {
80
- const availableAdditions = getAvailableAdditions();
81
- const additionsList = Object.keys(availableAdditions);
82
- throw new Error(`Unknown addition: ${additionName}\n\nAvailable additions: ${additionsList.join(', ')}`);
83
- }
84
-
85
- const options = await parseAdditionFlags(addition, argv);
86
- await validateAdditionOptions(addition, options);
87
- await runAddition(addition, options);
88
- }
89
-
90
- export async function runAddition(addition: AdditionMeta, additionOptions: AdditionOptions = {}): Promise<void> {
91
- const basePath = process.cwd();
92
-
93
- // Check if the feature is already enabled
94
- const config = getConfig();
95
- if (isFeatureEnabled(config.features, addition.featureName)) {
96
- output.log({
97
- title: `Addition '${addition.name}' is already enabled`,
98
- body: [`The feature flag '${addition.featureName}' is already set to true in .cprc.json.`, 'No changes needed.'],
99
- });
100
- return;
101
- }
102
-
103
- output.log({
104
- title: `Running addition: ${addition.name}`,
105
- body: [addition.description],
106
- });
107
-
108
- try {
109
- const context = new Context(basePath);
110
- const updatedContext = await executeAddition(addition, context, additionOptions);
111
-
112
- additionsDebug(`context for "${addition.name} (${addition.scriptPath})":`);
113
- additionsDebug('%O', updatedContext.listChanges());
114
-
115
- await formatFiles(updatedContext);
116
- flushChanges(updatedContext);
117
- printChanges(updatedContext, addition.name, addition);
118
-
119
- installNPMDependencies(updatedContext);
120
-
121
- await setFeatureFlag(addition.featureName, true);
122
- additionsDebug(`Set feature flag '${addition.featureName}' to true in .cprc.json`);
123
-
124
- output.success({
125
- title: `Successfully added ${addition.name} to your plugin.`,
126
- });
127
- } catch (error) {
128
- if (error instanceof Error) {
129
- throw new Error(`Error running addition "${addition.name} (${addition.scriptPath})": ${error.message}`);
130
- }
131
- throw error;
132
- }
133
- }
134
-
135
- export async function executeAddition(
136
- addition: AdditionMeta,
137
- context: Context,
138
- options: AdditionOptions = {}
139
- ): Promise<Context> {
140
- const module = await loadAdditionModule(addition);
141
- if (!module) {
142
- throw new Error(`Failed to load addition module for ${addition.name}`);
143
- }
144
- return module.default(context, options);
145
- }
@@ -1,12 +0,0 @@
1
- import type { AdditionMeta } from './additions.js';
2
- import type { Context } from '../context.js';
3
- import { debug } from '../../utils/utils.cli.js';
4
- import { printChanges as sharedPrintChanges } from '../utils.js';
5
-
6
- export const additionsDebug = debug.extend('additions');
7
-
8
- // addition-specific wrapper for printChanges.
9
- export function printChanges(context: Context, key: string, addition: AdditionMeta) {
10
- additionsDebug('printChanges for addition: %s', key);
11
- sharedPrintChanges(context, key, addition);
12
- }
@@ -1,12 +0,0 @@
1
- import type { Context } from '../context.js';
2
- import type { MigrationMeta } from './migrations.js';
3
- import { debug } from '../../utils/utils.cli.js';
4
- import { printChanges as sharedPrintChanges } from '../utils.js';
5
-
6
- export const migrationsDebug = debug.extend('migrations');
7
-
8
- // migration-specific wrapper for printChanges
9
- export function printChanges(context: Context, key: string, migration: MigrationMeta) {
10
- migrationsDebug('printChanges for migration: %s', key);
11
- sharedPrintChanges(context, key, migration);
12
- }