@grafana/create-plugin 4.15.0 → 4.16.1-canary.994.189ce88.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 CHANGED
@@ -1,3 +1,15 @@
1
+ # v4.16.0 (Mon Jul 08 2024)
2
+
3
+ #### 🚀 Enhancement
4
+
5
+ - Create Plugin: Allow setting feature flags via cli args [#984](https://github.com/grafana/plugin-tools/pull/984) ([@jackw](https://github.com/jackw))
6
+
7
+ #### Authors: 1
8
+
9
+ - Jack Westbrook ([@jackw](https://github.com/jackw))
10
+
11
+ ---
12
+
1
13
  # v4.15.0 (Thu Jul 04 2024)
2
14
 
3
15
  #### 🚀 Enhancement
package/LICENSE CHANGED
@@ -186,7 +186,7 @@
186
186
  same "printed page" as the copyright notice for easier
187
187
  identification within third-party archives.
188
188
 
189
- Copyright [yyyy] [name of copyright owner]
189
+ Copyright 2024 Grafana Labs
190
190
 
191
191
  Licensed under the Apache License, Version 2.0 (the "License");
192
192
  you may not use this file except in compliance with the License.
package/dist/constants.js CHANGED
@@ -60,7 +60,7 @@ export const MIGRATION_CONFIG = {
60
60
  devNpmDependenciesToRemove: ['@grafana/toolkit', '@grafana/runtime', '@grafana/data', '@grafana/ui'],
61
61
  };
62
62
  export const UDPATE_CONFIG = {
63
- filesToOverride: ['.config/'],
63
+ filesToOverride: ['.config/', '.cprc.json'],
64
64
  };
65
65
  export const TEXT = {
66
66
  overrideFilesPrompt: '**The following files will be overriden, would you like to continue?**',
@@ -8,6 +8,7 @@ import { DEFAULT_FEATURE_FLAGS } from '../../constants.js';
8
8
  const mocks = vi.hoisted(() => {
9
9
  return {
10
10
  commandName: 'generate',
11
+ argv: {},
11
12
  };
12
13
  });
13
14
  vi.mock('../utils.cli.js', async () => mocks);
@@ -20,6 +21,7 @@ describe('getConfig', () => {
20
21
  describe('Command: Generate', () => {
21
22
  beforeEach(() => {
22
23
  mocks.commandName = 'generate';
24
+ mocks.argv = {};
23
25
  });
24
26
  it('should give back a default config', async () => {
25
27
  const config = getConfig(tmpDir);
@@ -28,10 +30,21 @@ describe('getConfig', () => {
28
30
  features: DEFAULT_FEATURE_FLAGS,
29
31
  });
30
32
  });
33
+ it('should override default feature flags via cli args', async () => {
34
+ mocks.argv = {
35
+ 'feature-flags': 'bundleGrafanaUI',
36
+ };
37
+ const config = getConfig(tmpDir);
38
+ expect(config).toEqual({
39
+ version: getVersion(),
40
+ features: { ...DEFAULT_FEATURE_FLAGS, bundleGrafanaUI: true },
41
+ });
42
+ });
31
43
  });
32
44
  describe('Command: Update', () => {
33
45
  beforeEach(() => {
34
46
  mocks.commandName = 'update';
47
+ mocks.argv = {};
35
48
  });
36
49
  it('should give back the correct config when there are no config files at all', async () => {
37
50
  const config = getConfig();
@@ -0,0 +1,52 @@
1
+ import { partitionArr } from '../utils.helpers.js';
2
+ describe('partitionArr', () => {
3
+ it('should partition an array of numbers based on a predicate', () => {
4
+ const arr = [1, 2, 3, 4, 5, 6];
5
+ const isEven = (num) => num % 2 === 0;
6
+ const result = partitionArr(arr, isEven);
7
+ expect(result).toEqual([
8
+ [2, 4, 6],
9
+ [1, 3, 5],
10
+ ]);
11
+ });
12
+ it('should partition an array of strings based on a predicate', () => {
13
+ const arr = ['apple', 'banana', 'cherry', 'date'];
14
+ const startsWithA = (str) => str[0] === 'a';
15
+ const result = partitionArr(arr, startsWithA);
16
+ expect(result).toEqual([['apple'], ['banana', 'cherry', 'date']]);
17
+ });
18
+ it('should return two empty arrays if the input array is empty', () => {
19
+ const arr = [];
20
+ const isEven = (num) => num % 2 === 0;
21
+ const result = partitionArr(arr, isEven);
22
+ expect(result).toEqual([[], []]);
23
+ });
24
+ it('should place all items in the first partition if all match the predicate', () => {
25
+ const arr = [2, 4, 6, 8];
26
+ const isEven = (num) => num % 2 === 0;
27
+ const result = partitionArr(arr, isEven);
28
+ expect(result).toEqual([[2, 4, 6, 8], []]);
29
+ });
30
+ it('should place all items in the second partition if none match the predicate', () => {
31
+ const arr = [1, 3, 5, 7];
32
+ const isEven = (num) => num % 2 === 0;
33
+ const result = partitionArr(arr, isEven);
34
+ expect(result).toEqual([[], [1, 3, 5, 7]]);
35
+ });
36
+ it('should work with a complex object array and predicate', () => {
37
+ const arr = [
38
+ { name: 'Alice', age: 25 },
39
+ { name: 'Bob', age: 30 },
40
+ { name: 'Charlie', age: 35 },
41
+ ];
42
+ const isUnder30 = (person) => person.age < 30;
43
+ const result = partitionArr(arr, isUnder30);
44
+ expect(result).toEqual([
45
+ [{ name: 'Alice', age: 25 }],
46
+ [
47
+ { name: 'Bob', age: 30 },
48
+ { name: 'Charlie', age: 35 },
49
+ ],
50
+ ]);
51
+ });
52
+ });
@@ -1,8 +1,11 @@
1
1
  import fs from 'node:fs';
2
2
  import path from 'node:path';
3
3
  import { getVersion } from './utils.version.js';
4
- import { commandName } from './utils.cli.js';
4
+ import { argv, commandName } from './utils.cli.js';
5
5
  import { DEFAULT_FEATURE_FLAGS } from '../constants.js';
6
+ import { printBox } from './utils.console.js';
7
+ import { partitionArr } from './utils.helpers.js';
8
+ let hasShownConfigWarnings = false;
6
9
  export function getConfig(workDir = process.cwd()) {
7
10
  const rootConfig = getRootConfig(workDir);
8
11
  const userConfig = getUserConfig(workDir);
@@ -58,8 +61,24 @@ function readRCFileSync(path) {
58
61
  }
59
62
  }
60
63
  function createFeatureFlags(flags) {
61
- if (commandName === 'generate') {
62
- return DEFAULT_FEATURE_FLAGS;
64
+ const featureFlags = commandName === 'generate' ? DEFAULT_FEATURE_FLAGS : flags ?? {};
65
+ const cliArgFlags = parseFeatureFlagsFromCliArgs();
66
+ return { ...featureFlags, ...cliArgFlags };
67
+ }
68
+ function parseFeatureFlagsFromCliArgs() {
69
+ const flagsfromCliArgs = argv['feature-flags'] ? argv['feature-flags'].split(',') : [];
70
+ const availableFeatureFlags = Object.keys(DEFAULT_FEATURE_FLAGS);
71
+ const [knownFlags, unknownFlags] = partitionArr(flagsfromCliArgs, (item) => availableFeatureFlags.includes(item));
72
+ if (unknownFlags.length > 0 && !hasShownConfigWarnings) {
73
+ printBox({
74
+ title: 'Warning! Unknown feature flags detected.',
75
+ subtitle: ``,
76
+ content: `The following feature-flags are unknown: ${unknownFlags.join(', ')}.\n\nAvailable feature-flags are: ${availableFeatureFlags.join(', ')}`,
77
+ color: 'yellow',
78
+ });
79
+ hasShownConfigWarnings = true;
63
80
  }
64
- return flags ?? {};
81
+ return knownFlags.reduce((acc, flag) => {
82
+ return { ...acc, [flag]: true };
83
+ }, {});
65
84
  }
@@ -0,0 +1,3 @@
1
+ export function partitionArr(arr, pred) {
2
+ return arr.reduce((acc, i) => (acc[pred(i) ? 0 : 1].push(i), acc), [[], []]);
3
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grafana/create-plugin",
3
- "version": "4.15.0",
3
+ "version": "4.16.1-canary.994.189ce88.0",
4
4
  "repository": {
5
5
  "directory": "packages/create-plugin",
6
6
  "url": "https://github.com/grafana/plugin-tools"
@@ -87,5 +87,5 @@
87
87
  "engines": {
88
88
  "node": ">=20"
89
89
  },
90
- "gitHead": "10a4a8648f2f2794df271011c3bd062d47e323d6"
90
+ "gitHead": "189ce8870ba0be391aef344afc4782cbe1d60412"
91
91
  }
package/src/constants.ts CHANGED
@@ -85,7 +85,7 @@ export const MIGRATION_CONFIG = {
85
85
 
86
86
  export const UDPATE_CONFIG = {
87
87
  // Files that should be overriden between configuration version updates.
88
- filesToOverride: ['.config/'],
88
+ filesToOverride: ['.config/', '.cprc.json'],
89
89
  };
90
90
 
91
91
  // prettier-ignore
@@ -9,6 +9,7 @@ import { DEFAULT_FEATURE_FLAGS } from '../../constants.js';
9
9
  const mocks = vi.hoisted(() => {
10
10
  return {
11
11
  commandName: 'generate',
12
+ argv: {},
12
13
  };
13
14
  });
14
15
 
@@ -25,6 +26,7 @@ describe('getConfig', () => {
25
26
  describe('Command: Generate', () => {
26
27
  beforeEach(() => {
27
28
  mocks.commandName = 'generate';
29
+ mocks.argv = {};
28
30
  });
29
31
 
30
32
  it('should give back a default config', async () => {
@@ -35,11 +37,24 @@ describe('getConfig', () => {
35
37
  features: DEFAULT_FEATURE_FLAGS,
36
38
  });
37
39
  });
40
+
41
+ it('should override default feature flags via cli args', async () => {
42
+ mocks.argv = {
43
+ 'feature-flags': 'bundleGrafanaUI',
44
+ };
45
+ const config = getConfig(tmpDir);
46
+
47
+ expect(config).toEqual({
48
+ version: getVersion(),
49
+ features: { ...DEFAULT_FEATURE_FLAGS, bundleGrafanaUI: true },
50
+ });
51
+ });
38
52
  });
39
53
 
40
54
  describe('Command: Update', () => {
41
55
  beforeEach(() => {
42
56
  mocks.commandName = 'update';
57
+ mocks.argv = {};
43
58
  });
44
59
 
45
60
  it('should give back the correct config when there are no config files at all', async () => {
@@ -0,0 +1,58 @@
1
+ import { partitionArr } from '../utils.helpers.js';
2
+
3
+ describe('partitionArr', () => {
4
+ it('should partition an array of numbers based on a predicate', () => {
5
+ const arr = [1, 2, 3, 4, 5, 6];
6
+ const isEven = (num: number) => num % 2 === 0;
7
+ const result = partitionArr(arr, isEven);
8
+ expect(result).toEqual([
9
+ [2, 4, 6],
10
+ [1, 3, 5],
11
+ ]);
12
+ });
13
+
14
+ it('should partition an array of strings based on a predicate', () => {
15
+ const arr = ['apple', 'banana', 'cherry', 'date'];
16
+ const startsWithA = (str: string) => str[0] === 'a';
17
+ const result = partitionArr(arr, startsWithA);
18
+ expect(result).toEqual([['apple'], ['banana', 'cherry', 'date']]);
19
+ });
20
+
21
+ it('should return two empty arrays if the input array is empty', () => {
22
+ const arr: number[] = [];
23
+ const isEven = (num: number) => num % 2 === 0;
24
+ const result = partitionArr(arr, isEven);
25
+ expect(result).toEqual([[], []]);
26
+ });
27
+
28
+ it('should place all items in the first partition if all match the predicate', () => {
29
+ const arr = [2, 4, 6, 8];
30
+ const isEven = (num: number) => num % 2 === 0;
31
+ const result = partitionArr(arr, isEven);
32
+ expect(result).toEqual([[2, 4, 6, 8], []]);
33
+ });
34
+
35
+ it('should place all items in the second partition if none match the predicate', () => {
36
+ const arr = [1, 3, 5, 7];
37
+ const isEven = (num: number) => num % 2 === 0;
38
+ const result = partitionArr(arr, isEven);
39
+ expect(result).toEqual([[], [1, 3, 5, 7]]);
40
+ });
41
+
42
+ it('should work with a complex object array and predicate', () => {
43
+ const arr = [
44
+ { name: 'Alice', age: 25 },
45
+ { name: 'Bob', age: 30 },
46
+ { name: 'Charlie', age: 35 },
47
+ ];
48
+ const isUnder30 = (person: { name: string; age: number }) => person.age < 30;
49
+ const result = partitionArr(arr, isUnder30);
50
+ expect(result).toEqual([
51
+ [{ name: 'Alice', age: 25 }],
52
+ [
53
+ { name: 'Bob', age: 30 },
54
+ { name: 'Charlie', age: 35 },
55
+ ],
56
+ ]);
57
+ });
58
+ });
@@ -1,8 +1,10 @@
1
1
  import fs from 'node:fs';
2
2
  import path from 'node:path';
3
3
  import { getVersion } from './utils.version.js';
4
- import { commandName } from './utils.cli.js';
4
+ import { argv, commandName } from './utils.cli.js';
5
5
  import { DEFAULT_FEATURE_FLAGS } from '../constants.js';
6
+ import { printBox } from './utils.console.js';
7
+ import { partitionArr } from './utils.helpers.js';
6
8
 
7
9
  export type FeatureFlags = {
8
10
  bundleGrafanaUI?: boolean;
@@ -21,6 +23,9 @@ export type UserConfig = {
21
23
  features: FeatureFlags;
22
24
  };
23
25
 
26
+ // TODO: Create a config manager of sorts so we don't call getConfig multiple times rendering multiple warnings.
27
+ let hasShownConfigWarnings = false;
28
+
24
29
  export function getConfig(workDir = process.cwd()): CreatePluginConfig {
25
30
  const rootConfig = getRootConfig(workDir);
26
31
  const userConfig = getUserConfig(workDir);
@@ -83,11 +88,32 @@ function readRCFileSync(path: string): CreatePluginConfig | undefined {
83
88
  }
84
89
  }
85
90
 
91
+ // This function creates feature flags based on the defaults for generate command else flags read from config.
92
+ // In all cases it will override the flags with the featureFlag cli arg values.
86
93
  function createFeatureFlags(flags?: FeatureFlags): FeatureFlags {
87
- // Default values for new scaffoldings
88
- if (commandName === 'generate') {
89
- return DEFAULT_FEATURE_FLAGS;
94
+ const featureFlags = commandName === 'generate' ? DEFAULT_FEATURE_FLAGS : flags ?? {};
95
+ const cliArgFlags = parseFeatureFlagsFromCliArgs();
96
+ return { ...featureFlags, ...cliArgFlags };
97
+ }
98
+
99
+ function parseFeatureFlagsFromCliArgs() {
100
+ const flagsfromCliArgs: string[] = argv['feature-flags'] ? argv['feature-flags'].split(',') : [];
101
+ const availableFeatureFlags = Object.keys(DEFAULT_FEATURE_FLAGS);
102
+ const [knownFlags, unknownFlags] = partitionArr(flagsfromCliArgs, (item) => availableFeatureFlags.includes(item));
103
+
104
+ if (unknownFlags.length > 0 && !hasShownConfigWarnings) {
105
+ printBox({
106
+ title: 'Warning! Unknown feature flags detected.',
107
+ subtitle: ``,
108
+ content: `The following feature-flags are unknown: ${unknownFlags.join(
109
+ ', '
110
+ )}.\n\nAvailable feature-flags are: ${availableFeatureFlags.join(', ')}`,
111
+ color: 'yellow',
112
+ });
113
+ hasShownConfigWarnings = true;
90
114
  }
91
115
 
92
- return flags ?? {};
116
+ return knownFlags.reduce((acc, flag) => {
117
+ return { ...acc, [flag]: true };
118
+ }, {} as FeatureFlags);
93
119
  }
@@ -0,0 +1,3 @@
1
+ export function partitionArr<T>(arr: T[], pred: (item: T) => boolean): [T[], T[]] {
2
+ return arr.reduce<[T[], T[]]>((acc, i) => (acc[pred(i) ? 0 : 1].push(i), acc), [[], []]);
3
+ }
@@ -1,8 +1,6 @@
1
1
  {
2
2
  "features": {
3
- {{#if bundleGrafanaUI}}
4
- "bundleGrafanaUI": true{{/if}}
5
- {{#if useReactRouterV6}}
6
- "useReactRouterV6": true{{/if}}
3
+ "bundleGrafanaUI": {{ bundleGrafanaUI }},
4
+ "useReactRouterV6": {{ useReactRouterV6 }}
7
5
  }
8
6
  }