@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
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { addDependenciesToPackageJson } from '../../utils.js';
|
|
2
|
+
|
|
3
|
+
const flags = [
|
|
4
|
+
{
|
|
5
|
+
name: "feature-name",
|
|
6
|
+
description: "Name of the feature to add",
|
|
7
|
+
required: false
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
name: "enabled",
|
|
11
|
+
description: "Whether the feature should be enabled by default",
|
|
12
|
+
required: false
|
|
13
|
+
}
|
|
14
|
+
];
|
|
15
|
+
function parseFlags(argv) {
|
|
16
|
+
return {
|
|
17
|
+
featureName: argv["feature-name"] || "myFeature",
|
|
18
|
+
enabled: argv.enabled === "true" || argv.enabled === true
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function migrate(context, options = { featureName: "myFeature", enabled: true }) {
|
|
22
|
+
const { featureName, enabled } = options;
|
|
23
|
+
const rawPkgJson = context.getFile("./package.json") ?? "{}";
|
|
24
|
+
const packageJson = JSON.parse(rawPkgJson);
|
|
25
|
+
if (packageJson.scripts && !packageJson.scripts["example-script"]) {
|
|
26
|
+
packageJson.scripts["example-script"] = `echo "Running ${featureName}"`;
|
|
27
|
+
context.updateFile("./package.json", JSON.stringify(packageJson, null, 2));
|
|
28
|
+
}
|
|
29
|
+
addDependenciesToPackageJson(context, {}, { "example-dev-dep": "^1.0.0" });
|
|
30
|
+
if (!context.doesFileExist("./src/config.json")) {
|
|
31
|
+
const config = {
|
|
32
|
+
features: {
|
|
33
|
+
[featureName]: {
|
|
34
|
+
enabled,
|
|
35
|
+
description: "Example feature configuration"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
context.addFile("./src/config.json", JSON.stringify(config, null, 2));
|
|
40
|
+
}
|
|
41
|
+
if (!context.doesFileExist(`./src/features/${featureName}.ts`)) {
|
|
42
|
+
const featureCode = `export const ${featureName} = {
|
|
43
|
+
name: '${featureName}',
|
|
44
|
+
enabled: ${enabled},
|
|
45
|
+
init() {
|
|
46
|
+
console.log('${featureName} initialized');
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
`;
|
|
50
|
+
context.addFile(`./src/features/${featureName}.ts`, featureCode);
|
|
51
|
+
}
|
|
52
|
+
if (context.doesFileExist("./src/deprecated.ts")) {
|
|
53
|
+
context.deleteFile("./src/deprecated.ts");
|
|
54
|
+
}
|
|
55
|
+
if (context.doesFileExist("./src/old-config.json")) {
|
|
56
|
+
context.renameFile("./src/old-config.json", "./src/new-config.json");
|
|
57
|
+
}
|
|
58
|
+
return context;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export { migrate as default, flags, parseFlags };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@grafana/create-plugin",
|
|
3
|
-
"version": "6.2.0-canary.2233.
|
|
3
|
+
"version": "6.2.0-canary.2233.19133609453.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"directory": "packages/create-plugin",
|
|
6
6
|
"url": "https://github.com/grafana/plugin-tools"
|
|
@@ -59,5 +59,5 @@
|
|
|
59
59
|
"engines": {
|
|
60
60
|
"node": ">=20"
|
|
61
61
|
},
|
|
62
|
-
"gitHead": "
|
|
62
|
+
"gitHead": "d84f5bcb2be40efa1f28ad4a86b52a8a1566e85c"
|
|
63
63
|
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { Context } from '../../context.js';
|
|
4
|
+
import migrate from './example-addition.js';
|
|
5
|
+
|
|
6
|
+
describe('example-addition', () => {
|
|
7
|
+
it('should be idempotent', async () => {
|
|
8
|
+
const context = new Context('/virtual');
|
|
9
|
+
|
|
10
|
+
// Set up a minimal project structure
|
|
11
|
+
context.addFile('package.json', JSON.stringify({ scripts: {}, dependencies: {}, devDependencies: {} }));
|
|
12
|
+
context.addFile('src/index.ts', 'export const foo = "bar";');
|
|
13
|
+
|
|
14
|
+
const migrateWithOptions = (ctx: Context) => migrate(ctx, { featureName: 'testFeature', enabled: true });
|
|
15
|
+
await expect(migrateWithOptions).toBeIdempotent(context);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('should add example script to package.json', () => {
|
|
19
|
+
const context = new Context('/virtual');
|
|
20
|
+
|
|
21
|
+
context.addFile('package.json', JSON.stringify({ scripts: {}, dependencies: {}, devDependencies: {} }));
|
|
22
|
+
|
|
23
|
+
const result = migrate(context, { featureName: 'testFeature', enabled: true });
|
|
24
|
+
|
|
25
|
+
const packageJson = JSON.parse(result.getFile('package.json') || '{}');
|
|
26
|
+
expect(packageJson.scripts['example-script']).toBe('echo "Running testFeature"');
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('should add dev dependency', () => {
|
|
30
|
+
const context = new Context('/virtual');
|
|
31
|
+
|
|
32
|
+
context.addFile('package.json', JSON.stringify({ scripts: {}, dependencies: {}, devDependencies: {} }));
|
|
33
|
+
|
|
34
|
+
const result = migrate(context, { featureName: 'myFeature', enabled: false });
|
|
35
|
+
|
|
36
|
+
const packageJson = JSON.parse(result.getFile('package.json') || '{}');
|
|
37
|
+
expect(packageJson.devDependencies['example-dev-dep']).toBe('^1.0.0');
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('should create config file with feature settings', () => {
|
|
41
|
+
const context = new Context('/virtual');
|
|
42
|
+
|
|
43
|
+
context.addFile('package.json', JSON.stringify({ scripts: {}, dependencies: {}, devDependencies: {} }));
|
|
44
|
+
|
|
45
|
+
const result = migrate(context, { featureName: 'coolFeature', enabled: true });
|
|
46
|
+
|
|
47
|
+
expect(result.doesFileExist('src/config.json')).toBe(true);
|
|
48
|
+
const config = JSON.parse(result.getFile('src/config.json') || '{}');
|
|
49
|
+
expect(config.features.coolFeature).toEqual({
|
|
50
|
+
enabled: true,
|
|
51
|
+
description: 'Example feature configuration',
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('should create feature TypeScript file', () => {
|
|
56
|
+
const context = new Context('/virtual');
|
|
57
|
+
|
|
58
|
+
context.addFile('package.json', JSON.stringify({ scripts: {}, dependencies: {}, devDependencies: {} }));
|
|
59
|
+
|
|
60
|
+
const result = migrate(context, { featureName: 'myFeature', enabled: false });
|
|
61
|
+
|
|
62
|
+
expect(result.doesFileExist('src/features/myFeature.ts')).toBe(true);
|
|
63
|
+
const featureCode = result.getFile('src/features/myFeature.ts');
|
|
64
|
+
expect(featureCode).toContain('export const myFeature');
|
|
65
|
+
expect(featureCode).toContain('enabled: false');
|
|
66
|
+
expect(featureCode).toContain('myFeature initialized');
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('should delete deprecated file if it exists', () => {
|
|
70
|
+
const context = new Context('/virtual');
|
|
71
|
+
|
|
72
|
+
context.addFile('package.json', JSON.stringify({ scripts: {}, dependencies: {}, devDependencies: {} }));
|
|
73
|
+
context.addFile('src/deprecated.ts', 'export const old = true;');
|
|
74
|
+
|
|
75
|
+
const result = migrate(context, { featureName: 'testFeature', enabled: true });
|
|
76
|
+
|
|
77
|
+
expect(result.doesFileExist('src/deprecated.ts')).toBe(false);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('should rename old-config.json if it exists', () => {
|
|
81
|
+
const context = new Context('/virtual');
|
|
82
|
+
|
|
83
|
+
context.addFile('package.json', JSON.stringify({ scripts: {}, dependencies: {}, devDependencies: {} }));
|
|
84
|
+
context.addFile('src/old-config.json', JSON.stringify({ old: true }));
|
|
85
|
+
|
|
86
|
+
const result = migrate(context, { featureName: 'testFeature', enabled: true });
|
|
87
|
+
|
|
88
|
+
expect(result.doesFileExist('src/old-config.json')).toBe(false);
|
|
89
|
+
expect(result.doesFileExist('src/new-config.json')).toBe(true);
|
|
90
|
+
const newConfig = JSON.parse(result.getFile('src/new-config.json') || '{}');
|
|
91
|
+
expect(newConfig.old).toBe(true);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('should not add script if it already exists', () => {
|
|
95
|
+
const context = new Context('/virtual');
|
|
96
|
+
|
|
97
|
+
context.addFile(
|
|
98
|
+
'package.json',
|
|
99
|
+
JSON.stringify({
|
|
100
|
+
scripts: { 'example-script': 'existing command' },
|
|
101
|
+
dependencies: {},
|
|
102
|
+
devDependencies: {},
|
|
103
|
+
})
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
const result = migrate(context, { featureName: 'testFeature', enabled: true });
|
|
107
|
+
|
|
108
|
+
const packageJson = JSON.parse(result.getFile('package.json') || '{}');
|
|
109
|
+
expect(packageJson.scripts['example-script']).toBe('existing command');
|
|
110
|
+
});
|
|
111
|
+
});
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { Context } from '../../context.js';
|
|
2
|
+
import type { FlagDefinition } from '../../types.js';
|
|
3
|
+
import { addDependenciesToPackageJson } from '../../utils.js';
|
|
4
|
+
|
|
5
|
+
export type ExampleOptions = {
|
|
6
|
+
featureName: string;
|
|
7
|
+
enabled: boolean;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export const flags: FlagDefinition[] = [
|
|
11
|
+
{
|
|
12
|
+
name: 'feature-name',
|
|
13
|
+
description: 'Name of the feature to add',
|
|
14
|
+
required: false,
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
name: 'enabled',
|
|
18
|
+
description: 'Whether the feature should be enabled by default',
|
|
19
|
+
required: false,
|
|
20
|
+
},
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
export function parseFlags(argv: any): ExampleOptions {
|
|
24
|
+
return {
|
|
25
|
+
featureName: argv['feature-name'] || 'myFeature',
|
|
26
|
+
enabled: argv.enabled === 'true' || argv.enabled === true,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export default function migrate(
|
|
31
|
+
context: Context,
|
|
32
|
+
options: ExampleOptions = { featureName: 'myFeature', enabled: true }
|
|
33
|
+
): Context {
|
|
34
|
+
const { featureName, enabled } = options;
|
|
35
|
+
|
|
36
|
+
const rawPkgJson = context.getFile('./package.json') ?? '{}';
|
|
37
|
+
const packageJson = JSON.parse(rawPkgJson);
|
|
38
|
+
|
|
39
|
+
if (packageJson.scripts && !packageJson.scripts['example-script']) {
|
|
40
|
+
packageJson.scripts['example-script'] = `echo "Running ${featureName}"`;
|
|
41
|
+
context.updateFile('./package.json', JSON.stringify(packageJson, null, 2));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
addDependenciesToPackageJson(context, {}, { 'example-dev-dep': '^1.0.0' });
|
|
45
|
+
|
|
46
|
+
if (!context.doesFileExist('./src/config.json')) {
|
|
47
|
+
const config = {
|
|
48
|
+
features: {
|
|
49
|
+
[featureName]: {
|
|
50
|
+
enabled,
|
|
51
|
+
description: 'Example feature configuration',
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
context.addFile('./src/config.json', JSON.stringify(config, null, 2));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (!context.doesFileExist(`./src/features/${featureName}.ts`)) {
|
|
59
|
+
const featureCode = `export const ${featureName} = {
|
|
60
|
+
name: '${featureName}',
|
|
61
|
+
enabled: ${enabled},
|
|
62
|
+
init() {
|
|
63
|
+
console.log('${featureName} initialized');
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
`;
|
|
67
|
+
context.addFile(`./src/features/${featureName}.ts`, featureCode);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (context.doesFileExist('./src/deprecated.ts')) {
|
|
71
|
+
context.deleteFile('./src/deprecated.ts');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (context.doesFileExist('./src/old-config.json')) {
|
|
75
|
+
context.renameFile('./src/old-config.json', './src/new-config.json');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return context;
|
|
79
|
+
}
|