cli: added repository field check to repo fix

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-02-07 15:19:00 +01:00
parent eab6182032
commit d557d47cb5
5 changed files with 83 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/cli': patch
---
Added check for the `repository` field in the `repo fix` command.
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/cli-node': patch
---
Added `repository` field to `BackstagePackageJson` type.
+8
View File
@@ -55,6 +55,14 @@ export interface BackstagePackageJson {
registry?: string;
};
// (undocumented)
repository?:
| string
| {
type: string;
url: string;
directory: string;
};
// (undocumented)
scripts?: {
[key: string]: string;
};
@@ -58,6 +58,14 @@ export interface BackstagePackageJson {
registry?: string;
};
repository?:
| string
| {
type: string;
url: string;
directory: string;
};
dependencies?: {
[key: string]: string;
};
+57 -1
View File
@@ -22,7 +22,11 @@ import {
} from '@backstage/cli-node';
import { OptionValues } from 'commander';
import fs from 'fs-extra';
import { resolve as resolvePath } from 'path';
import {
resolve as resolvePath,
join as joinPath,
relative as relativePath,
} from 'path';
import { paths } from '../../lib/paths';
/**
@@ -189,12 +193,64 @@ export function fixSideEffects(pkg: FixablePackage) {
pkg.changed = true;
}
export function createRepositoryFieldFixer() {
const rootPkg = require(paths.resolveTargetRoot('package.json'));
const rootRepoField = rootPkg.repository;
if (!rootRepoField) {
return () => {};
}
const rootType = rootRepoField.type || 'git';
const rootUrl = rootRepoField.url;
const rootDir = rootRepoField.directory || '';
return (pkg: FixablePackage) => {
const expectedPath = joinPath(
rootDir,
relativePath(paths.targetRoot, pkg.dir),
);
const repoField = pkg.packageJson.repository;
if (!repoField || typeof repoField === 'string') {
const pkgEntries = Object.entries(pkg.packageJson);
pkgEntries.splice(
// Place it just above the backstage field
pkgEntries.findIndex(([name]) => name === 'backstage'),
0,
[
'repository',
{
type: rootType,
url: rootUrl,
directory: expectedPath,
},
],
);
pkg.packageJson = Object.fromEntries(pkgEntries) as BackstagePackageJson;
pkg.changed = true;
return;
}
// If there's a type or URL mismatch, leave the field as is
if (repoField.type !== rootType || repoField.url !== rootUrl) {
return;
}
if (repoField.directory !== expectedPath) {
repoField.directory = expectedPath;
pkg.changed = true;
}
};
}
export async function command(opts: OptionValues): Promise<void> {
const packages = await readFixablePackages();
const fixRepositoryField = createRepositoryFieldFixer();
for (const pkg of packages) {
fixPackageExports(pkg);
fixSideEffects(pkg);
fixRepositoryField(pkg);
}
if (opts.check) {