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: Ignore grafana dependencies when migrating #119

Merged
merged 1 commit into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 6 additions & 2 deletions packages/create-plugin/src/commands/migrate.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ export const migrate = async () => {
// (skipped automatically if there is nothing to update)
// ------------------------------------------------
if (hasNpmDependenciesToUpdate()) {
if (await confirmPrompt(TEXT.updateNpmDependenciesPrompt + getPackageJsonUpdatesAsText())) {
updatePackageJson();
if (
await confirmPrompt(
TEXT.updateNpmDependenciesPrompt + getPackageJsonUpdatesAsText({ ignoreGrafanaDependencies: true })
)
) {
updatePackageJson({ ignoreGrafanaDependencies: true });
printSuccessMessage(TEXT.updateNpmDependenciesSuccess);
} else {
printMessage(TEXT.updateNpmDependenciesAborted);
Expand Down
9 changes: 9 additions & 0 deletions packages/create-plugin/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ export const EXTRA_TEMPLATE_VARIABLES = {
grafanaVersion: '9.1.2',
};

export const GRAFANA_FE_PACKAGES = [
'@grafana/data',
'@grafana/e2e-selectors',
'@grafana/e2e',
'@grafana/runtime',
'@grafana/schema',
'@grafana/ui',
];

export const MIGRATION_CONFIG = {
// Files that should be overriden during a migration.
// (paths are relative to the scaffolded projects root)
Expand Down
38 changes: 36 additions & 2 deletions packages/create-plugin/src/utils/utils.npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import fs from 'fs';
import semver from 'semver';
import { readJsonFile } from './utils.files';
import { renderTemplateFromFile, getTemplateData } from './utils.templates';
import { TEMPLATE_PATHS } from '../constants';
import { GRAFANA_FE_PACKAGES, TEMPLATE_PATHS } from '../constants';

type UpdateSummary = Record<string, { prev: string | null; next: string | null }>;

type UpdateOptions = {
onlyOutdated?: Boolean;
ignoreGrafanaDependencies?: boolean;
};

type PackageJson = {
Expand All @@ -35,14 +36,15 @@ export function writePackageJson(json: PackageJson) {

export function getNpmDependencyUpdatesAsText(dependencyUpdates: UpdateSummary) {
return Object.entries(dependencyUpdates)
.filter(([packageName, { prev, next }]) => prev !== next)
.map(([packageName, { prev, next }]) => {
// New package
if (!prev) {
return `\`${packageName}\` - \`${next}\` (new)`;
}

// Updated package
return `\`${packageName}\` - \`${prev}\` -> \`${next}\``;
return `\`${packageName}\` - \`${prev}\` -> \`${next}\` (updated)`;
})
.join('\n ');
}
Expand Down Expand Up @@ -100,12 +102,44 @@ export function getPackageJsonUpdates(options: UpdateOptions = {}) {
const dependencyUpdates = getUpdatableNpmDependencies(dependencies, newDependencies, options);
const devDependencyUpdates = getUpdatableNpmDependencies(devDependencies, newDevDependencies, options);

if (options.ignoreGrafanaDependencies) {
const prevGrafanaDependencies = Object.entries({ ...dependencies, ...devDependencies }).reduce<
Record<string, string>
>((acc, [key, value]) => {
if (GRAFANA_FE_PACKAGES.includes(key)) {
acc[key] = value;
}
return acc;
}, {});
return {
dependencyUpdates: ignoreGrafanaDependencies(dependencyUpdates, prevGrafanaDependencies),
devDependencyUpdates: ignoreGrafanaDependencies(devDependencyUpdates, prevGrafanaDependencies),
};
}

return {
dependencyUpdates,
devDependencyUpdates,
};
}

function ignoreGrafanaDependencies(dependencyUpdates: UpdateSummary, prevGrafanaDependencies: Record<string, string>) {
const whatever = Object.keys(prevGrafanaDependencies);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whatever! 😬

const result = Object.entries(dependencyUpdates).reduce<UpdateSummary>((acc, [packageName, value]) => {
if (whatever.includes(packageName)) {
acc[packageName] = {
prev: prevGrafanaDependencies[packageName],
next: prevGrafanaDependencies[packageName],
};
} else {
acc[packageName] = value;
}
return acc;
}, {});

return result;
}

export function getUpdatableNpmDependencies(
prevDeps: Record<string, string>,
nextDeps: Record<string, string>,
Expand Down