From 323562efc9236bd397034d8e24881fa15ea97065 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Tue, 25 Jan 2022 21:35:39 +0100 Subject: [PATCH] cli: switch to keeping track of outputs for each package role Signed-off-by: Patrik Oldsberg --- packages/cli/src/commands/build/command.ts | 9 +-- .../src/commands/migrate/packageScripts.ts | 6 +- packages/cli/src/lib/role/index.ts | 7 ++- .../cli/src/lib/role/packageRoles.test.ts | 4 +- packages/cli/src/lib/role/packageRoles.ts | 60 +++++++++++++++---- packages/cli/src/lib/role/types.ts | 3 +- 6 files changed, 66 insertions(+), 23 deletions(-) diff --git a/packages/cli/src/commands/build/command.ts b/packages/cli/src/commands/build/command.ts index 5db6baa34f..b2e5925119 100644 --- a/packages/cli/src/commands/build/command.ts +++ b/packages/cli/src/commands/build/command.ts @@ -20,9 +20,6 @@ import { PackageRole, findRoleFromCommand, getRoleInfo } from '../../lib/role'; const bundledRoles: PackageRole[] = ['app', 'backend']; -const esmPlatforms = ['web', 'common']; -const cjsPlatforms = ['node', 'common']; - export async function command(cmd: Command): Promise { const role = await findRoleFromCommand(cmd); const roleInfo = getRoleInfo(role); @@ -35,13 +32,13 @@ export async function command(cmd: Command): Promise { const outputs = new Set(); - if (cjsPlatforms.includes(roleInfo.platform)) { + if (roleInfo.output.includes('cjs')) { outputs.add(Output.cjs); } - if (esmPlatforms.includes(roleInfo.platform)) { + if (roleInfo.output.includes('esm')) { outputs.add(Output.esm); } - if (role !== 'cli') { + if (roleInfo.output.includes('types')) { outputs.add(Output.types); } diff --git a/packages/cli/src/commands/migrate/packageScripts.ts b/packages/cli/src/commands/migrate/packageScripts.ts index c54d4ebb31..7a02411690 100644 --- a/packages/cli/src/commands/migrate/packageScripts.ts +++ b/packages/cli/src/commands/migrate/packageScripts.ts @@ -17,9 +17,8 @@ import fs from 'fs-extra'; import { resolve as resolvePath } from 'path'; import { PackageGraph } from '../../lib/monorepo'; -import { getRoleFromPackage, PackageRole } from '../../lib/role'; +import { getRoleFromPackage, getRoleInfo, PackageRole } from '../../lib/role'; -const bundledRoles: PackageRole[] = ['app', 'backend']; const noStartRoles: PackageRole[] = ['cli', 'common-library']; export async function command() { @@ -32,8 +31,9 @@ export async function command() { return; } + const roleInfo = getRoleInfo(role); const hasStart = !noStartRoles.includes(role); - const isBundled = bundledRoles.includes(role); + const isBundled = roleInfo.output.includes('bundle'); const expectedScripts = { ...(hasStart && { start: 'backstage-cli script start' }), diff --git a/packages/cli/src/lib/role/index.ts b/packages/cli/src/lib/role/index.ts index a01be8d11e..4e1047a628 100644 --- a/packages/cli/src/lib/role/index.ts +++ b/packages/cli/src/lib/role/index.ts @@ -14,7 +14,12 @@ * limitations under the License. */ -export type { PackageRoleInfo, PackagePlatform, PackageRole } from './types'; +export type { + PackageRoleInfo, + PackagePlatform, + PackageOutputType, + PackageRole, +} from './types'; export { getRoleInfo, getRoleFromPackage, diff --git a/packages/cli/src/lib/role/packageRoles.test.ts b/packages/cli/src/lib/role/packageRoles.test.ts index db79143e50..f6c9fb418a 100644 --- a/packages/cli/src/lib/role/packageRoles.test.ts +++ b/packages/cli/src/lib/role/packageRoles.test.ts @@ -28,13 +28,13 @@ describe('getRoleInfo', () => { expect(getRoleInfo('web-library')).toEqual({ role: 'web-library', platform: 'web', - bundled: false, + output: ['types', 'esm'], }); expect(getRoleInfo('app')).toEqual({ role: 'app', platform: 'web', - bundled: true, + output: ['bundle'], }); expect(() => getRoleInfo('invalid')).toThrow( diff --git a/packages/cli/src/lib/role/packageRoles.ts b/packages/cli/src/lib/role/packageRoles.ts index c12ca644b9..7c855ee0fd 100644 --- a/packages/cli/src/lib/role/packageRoles.ts +++ b/packages/cli/src/lib/role/packageRoles.ts @@ -21,16 +21,56 @@ import { paths } from '../paths'; import { PackageRole, PackageRoleInfo } from './types'; const packageRoleInfos: PackageRoleInfo[] = [ - { role: 'app', bundled: true, platform: 'web' }, - { role: 'backend', bundled: true, platform: 'node' }, - { role: 'cli', bundled: false, platform: 'node' }, - { role: 'web-library', bundled: false, platform: 'web' }, - { role: 'node-library', bundled: false, platform: 'node' }, - { role: 'common-library', bundled: false, platform: 'common' }, - { role: 'plugin-frontend', bundled: false, platform: 'web' }, - { role: 'plugin-frontend-module', bundled: false, platform: 'web' }, - { role: 'plugin-backend', bundled: false, platform: 'node' }, - { role: 'plugin-backend-module', bundled: false, platform: 'node' }, + { + role: 'app', + platform: 'web', + output: ['bundle'], + }, + { + role: 'backend', + platform: 'node', + output: ['bundle'], + }, + { + role: 'cli', + platform: 'node', + output: ['cjs'], + }, + { + role: 'web-library', + platform: 'web', + output: ['types', 'esm'], + }, + { + role: 'node-library', + platform: 'node', + output: ['types', 'cjs'], + }, + { + role: 'common-library', + platform: 'common', + output: ['types', 'esm', 'cjs'], + }, + { + role: 'plugin-frontend', + platform: 'web', + output: ['types', 'esm'], + }, + { + role: 'plugin-frontend-module', + platform: 'web', + output: ['types', 'esm'], + }, + { + role: 'plugin-backend', + platform: 'node', + output: ['types', 'cjs'], + }, + { + role: 'plugin-backend-module', + platform: 'node', + output: ['types', 'cjs'], + }, ]; export function getRoleInfo(role: string): PackageRoleInfo { diff --git a/packages/cli/src/lib/role/types.ts b/packages/cli/src/lib/role/types.ts index efe2c99fd5..6a89f7bdaf 100644 --- a/packages/cli/src/lib/role/types.ts +++ b/packages/cli/src/lib/role/types.ts @@ -27,9 +27,10 @@ export type PackageRole = | 'plugin-backend-module'; export type PackagePlatform = 'node' | 'web' | 'common'; +export type PackageOutputType = 'bundle' | 'types' | 'esm' | 'cjs'; export interface PackageRoleInfo { role: PackageRole; - bundled: boolean; platform: PackagePlatform; + output: PackageOutputType[]; }