cli: split role detection into read and detect
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -19,4 +19,4 @@ export type {
|
||||
PackagePlatform,
|
||||
PackageRoleName,
|
||||
} from './types';
|
||||
export { detectPackageRole } from './detectPackageRole';
|
||||
export { detectPackageRole, readPackageRole } from './packageRoles';
|
||||
|
||||
+9
-7
@@ -14,12 +14,12 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { detectPackageRole } from './detectPackageRole';
|
||||
import { readPackageRole, detectPackageRole } from './packageRoles';
|
||||
|
||||
describe('detectPackageRole', () => {
|
||||
it('detects explicit package roles', () => {
|
||||
describe('readPackageRole', () => {
|
||||
it('reads explicit package roles', () => {
|
||||
expect(
|
||||
detectPackageRole({
|
||||
readPackageRole({
|
||||
backstage: {
|
||||
role: 'web-library',
|
||||
},
|
||||
@@ -30,7 +30,7 @@ describe('detectPackageRole', () => {
|
||||
});
|
||||
|
||||
expect(
|
||||
detectPackageRole({
|
||||
readPackageRole({
|
||||
backstage: {
|
||||
role: 'app',
|
||||
},
|
||||
@@ -41,20 +41,22 @@ describe('detectPackageRole', () => {
|
||||
});
|
||||
|
||||
expect(() =>
|
||||
detectPackageRole({
|
||||
readPackageRole({
|
||||
name: 'test',
|
||||
backstage: {},
|
||||
}),
|
||||
).toThrow('Package test must specify a role in the "backstage" field');
|
||||
|
||||
expect(() =>
|
||||
detectPackageRole({
|
||||
readPackageRole({
|
||||
name: 'test',
|
||||
backstage: { role: 'invalid' },
|
||||
}),
|
||||
).toThrow(`Unknown role 'invalid' in package test`);
|
||||
});
|
||||
});
|
||||
|
||||
describe('detectPackageRole', () => {
|
||||
it('detects the role of example-app', () => {
|
||||
expect(
|
||||
detectPackageRole({
|
||||
+33
-23
@@ -31,7 +31,38 @@ const packageRoles: PackageRoleInfo[] = [
|
||||
];
|
||||
const roleMap = Object.fromEntries(packageRoles.map(i => [i.role, i]));
|
||||
|
||||
const backstagePackageSchema = z.object({
|
||||
const readSchema = z.object({
|
||||
name: z.string().optional(),
|
||||
backstage: z
|
||||
.object({
|
||||
role: z.string().optional(),
|
||||
})
|
||||
.optional(),
|
||||
});
|
||||
|
||||
export function readPackageRole(pkgJson: unknown): PackageRoleInfo | undefined {
|
||||
const pkg = readSchema.parse(pkgJson);
|
||||
|
||||
// If there's an explicit role, use that.
|
||||
if (pkg.backstage) {
|
||||
const { role } = pkg.backstage;
|
||||
if (!role) {
|
||||
throw new Error(
|
||||
`Package ${pkg.name} must specify a role in the "backstage" field`,
|
||||
);
|
||||
}
|
||||
|
||||
const roleInfo = packageRoles.find(r => r.role === role);
|
||||
if (!roleInfo) {
|
||||
throw new Error(`Unknown role '${role}' in package ${pkg.name}`);
|
||||
}
|
||||
return roleInfo;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const detectionSchema = z.object({
|
||||
name: z.string().optional(),
|
||||
scripts: z
|
||||
.object({
|
||||
@@ -39,11 +70,6 @@ const backstagePackageSchema = z.object({
|
||||
build: z.string().optional(),
|
||||
})
|
||||
.optional(),
|
||||
backstage: z
|
||||
.object({
|
||||
role: z.string().optional(),
|
||||
})
|
||||
.optional(),
|
||||
publishConfig: z
|
||||
.object({
|
||||
main: z.string().optional(),
|
||||
@@ -59,23 +85,7 @@ const backstagePackageSchema = z.object({
|
||||
export function detectPackageRole(
|
||||
pkgJson: unknown,
|
||||
): PackageRoleInfo | undefined {
|
||||
const pkg = backstagePackageSchema.parse(pkgJson);
|
||||
|
||||
// If there's an explicit role, use that.
|
||||
if (pkg.backstage) {
|
||||
const { role } = pkg.backstage;
|
||||
if (!role) {
|
||||
throw new Error(
|
||||
`Package ${pkg.name} must specify a role in the "backstage" field`,
|
||||
);
|
||||
}
|
||||
|
||||
const roleInfo = packageRoles.find(r => r.role === role);
|
||||
if (!roleInfo) {
|
||||
throw new Error(`Unknown role '${role}' in package ${pkg.name}`);
|
||||
}
|
||||
return roleInfo;
|
||||
}
|
||||
const pkg = detectionSchema.parse(pkgJson);
|
||||
|
||||
if (pkg.scripts?.start?.includes('app:serve')) {
|
||||
return roleMap.app;
|
||||
Reference in New Issue
Block a user