Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Plugin: Allow setting feature flags via cli args #984

Merged
merged 11 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/create-plugin/src/utils/tests/utils.config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { DEFAULT_FEATURE_FLAGS } from '../../constants.js';
const mocks = vi.hoisted(() => {
return {
commandName: 'generate',
argv: {},
};
});

Expand All @@ -25,6 +26,7 @@ describe('getConfig', () => {
describe('Command: Generate', () => {
beforeEach(() => {
mocks.commandName = 'generate';
mocks.argv = {};
});

it('should give back a default config', async () => {
Expand All @@ -35,11 +37,25 @@ describe('getConfig', () => {
features: DEFAULT_FEATURE_FLAGS,
});
});

it('should override default feature flags via cli args', async () => {
mocks.argv = {
useReactRouterV6: false,
bundleGrafanaUI: true,
};
const config = getConfig(tmpDir);

expect(config).toEqual({
version: getVersion(),
features: { ...DEFAULT_FEATURE_FLAGS, ...mocks.argv },
});
});
});

describe('Command: Update', () => {
beforeEach(() => {
mocks.commandName = 'update';
mocks.argv = {};
});

it('should give back the correct config when there are no config files at all', async () => {
Expand Down
17 changes: 11 additions & 6 deletions packages/create-plugin/src/utils/utils.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'node:fs';
import path from 'node:path';
import { getVersion } from './utils.version.js';
import { commandName } from './utils.cli.js';
import { argv, commandName } from './utils.cli.js';
import { DEFAULT_FEATURE_FLAGS } from '../constants.js';

export type FeatureFlags = {
Expand Down Expand Up @@ -83,11 +83,16 @@ function readRCFileSync(path: string): CreatePluginConfig | undefined {
}
}

// This function creates feature flags based on the defaults for generate command else flags read from config.
// In both cases it will override the flags if any cli args with the same name are passed in.
function createFeatureFlags(flags?: FeatureFlags): FeatureFlags {
// Default values for new scaffoldings
if (commandName === 'generate') {
return DEFAULT_FEATURE_FLAGS;
}
const featureFlags = commandName === 'generate' ? DEFAULT_FEATURE_FLAGS : flags ?? {};

return flags ?? {};
return Object.entries(featureFlags).reduce((acc, [flag, value]) => {
if (argv.hasOwnProperty(flag)) {
return { ...acc, [flag]: argv[flag] };
} else {
return { ...acc, [flag]: value };
}
}, {} as FeatureFlags);
}
Loading