From 977a5c8f96ab98d999eec729ab84bd72b37166a3 Mon Sep 17 00:00:00 2001 From: Marcus Eide Date: Tue, 21 Feb 2023 15:17:52 +0100 Subject: [PATCH] cli: Simplify patching by reading target file from path Signed-off-by: Marcus Eide --- packages/cli/src/commands/admin/auth/files.ts | 20 +------------------ .../src/commands/admin/auth/github/oauth.ts | 16 +++------------ packages/cli/src/commands/admin/auth/patch.ts | 19 ++++++++++++++---- ...x.patch => 01-github.Add-SignInPage.patch} | 2 ++ ...ch => 02-github.Use-signIn-resolver.patch} | 2 ++ 5 files changed, 23 insertions(+), 36 deletions(-) rename packages/cli/src/commands/admin/auth/patches/{01-github.App.tsx.patch => 01-github.Add-SignInPage.patch} (92%) rename packages/cli/src/commands/admin/auth/patches/{02-github.auth.ts.patch => 02-github.Use-signIn-resolver.patch} (87%) diff --git a/packages/cli/src/commands/admin/auth/files.ts b/packages/cli/src/commands/admin/auth/files.ts index f84c77fe60..6ce58dc527 100644 --- a/packages/cli/src/commands/admin/auth/files.ts +++ b/packages/cli/src/commands/admin/auth/files.ts @@ -18,22 +18,9 @@ import { findPaths } from '@backstage/cli-common'; import * as path from 'path'; /* eslint-disable-next-line no-restricted-syntax */ -const { targetRoot, ownDir, resolveTargetRoot } = findPaths(__dirname); +const { targetRoot, ownDir } = findPaths(__dirname); export const APP_CONFIG_FILE = path.join(targetRoot, 'app-config.local.yaml'); export const USER_ENTITY_FILE = path.join(targetRoot, 'user-info.yaml'); - -const APP_TSX_FILE = path.join( - resolveTargetRoot('packages/app'), - 'src', - 'App.tsx', -); -const AUTH_BACKEND_PLUGIN_FILE = path.join( - resolveTargetRoot('packages/backend'), - 'src', - 'plugins', - 'auth.ts', -); - export const PATCH_FOLDER = path.join( ownDir, 'src', @@ -42,8 +29,3 @@ export const PATCH_FOLDER = path.join( 'auth', 'patches', ); - -export const patchMap: Record = { - 'App.tsx': APP_TSX_FILE, - 'auth.ts': AUTH_BACKEND_PLUGIN_FILE, -}; diff --git a/packages/cli/src/commands/admin/auth/github/oauth.ts b/packages/cli/src/commands/admin/auth/github/oauth.ts index 00bfbef23d..1de6b2a4c4 100644 --- a/packages/cli/src/commands/admin/auth/github/oauth.ts +++ b/packages/cli/src/commands/admin/auth/github/oauth.ts @@ -19,15 +19,9 @@ import chalk from 'chalk'; import * as fs from 'fs-extra'; import inquirer from 'inquirer'; import fetch from 'node-fetch'; -import * as path from 'path'; import { Task } from '../../../../lib/tasks'; import { addUserEntity, updateConfigFile } from '../config'; -import { - APP_CONFIG_FILE, - patchMap, - PATCH_FOLDER, - USER_ENTITY_FILE, -} from '../files'; +import { APP_CONFIG_FILE, PATCH_FOLDER, USER_ENTITY_FILE } from '../files'; import { patch } from '../patch'; const validateCredentials = async (clientId: string, clientSecret: string) => { @@ -172,12 +166,8 @@ export const oauth = async () => { const patches = await fs.readdir(PATCH_FOLDER); for (const patchFile of patches) { - const target = patchFile - .replace(/[0-9]+-github\./, '') - .replace('.patch', ''); - - await Task.forItem('Pactching', target, async () => { - await patch(patchMap[target], path.join(PATCH_FOLDER, patchFile)); + await Task.forItem('Patching', patchFile, async () => { + await patch(patchFile); }); } }; diff --git a/packages/cli/src/commands/admin/auth/patch.ts b/packages/cli/src/commands/admin/auth/patch.ts index cf466f9408..2333275064 100644 --- a/packages/cli/src/commands/admin/auth/patch.ts +++ b/packages/cli/src/commands/admin/auth/patch.ts @@ -15,12 +15,23 @@ */ import * as fs from 'fs-extra'; +import * as path from 'path'; import * as differ from 'diff'; +import { PATCH_FOLDER } from './files'; +import { findPaths } from '@backstage/cli-common'; -export const patch = async (file: string, patchFile: string) => { - const patchContent = await fs.readFile(patchFile, 'utf8'); - const oldContent = await fs.readFile(file, 'utf8'); +/* eslint-disable-next-line no-restricted-syntax */ +const { targetRoot } = findPaths(__dirname); + +export const patch = async (patchFile: string) => { + const patchContent = await fs.readFile( + path.join(PATCH_FOLDER, patchFile), + 'utf8', + ); + const targetName = patchContent.split('\n')[0].replace('--- a', ''); + const targetFile = path.join(targetRoot, targetName); + const oldContent = await fs.readFile(targetFile, 'utf8'); const newContent = differ.applyPatch(oldContent, patchContent); - return await fs.writeFile(file, newContent, 'utf8'); + return await fs.writeFile(targetFile, newContent, 'utf8'); }; diff --git a/packages/cli/src/commands/admin/auth/patches/01-github.App.tsx.patch b/packages/cli/src/commands/admin/auth/patches/01-github.Add-SignInPage.patch similarity index 92% rename from packages/cli/src/commands/admin/auth/patches/01-github.App.tsx.patch rename to packages/cli/src/commands/admin/auth/patches/01-github.Add-SignInPage.patch index a7e0c150a5..8a3401fea1 100644 --- a/packages/cli/src/commands/admin/auth/patches/01-github.App.tsx.patch +++ b/packages/cli/src/commands/admin/auth/patches/01-github.Add-SignInPage.patch @@ -1,3 +1,5 @@ +--- a/packages/app/src/App.tsx ++++ b/packages/app/src/App.tsx @@ -30 +30,5 @@ import { Root } from './components/Root'; -import { AlertDisplay, OAuthRequestDialog } from '@backstage/core-components'; +import { diff --git a/packages/cli/src/commands/admin/auth/patches/02-github.auth.ts.patch b/packages/cli/src/commands/admin/auth/patches/02-github.Use-signIn-resolver.patch similarity index 87% rename from packages/cli/src/commands/admin/auth/patches/02-github.auth.ts.patch rename to packages/cli/src/commands/admin/auth/patches/02-github.Use-signIn-resolver.patch index e6e65e7b74..2baaff7cf9 100644 --- a/packages/cli/src/commands/admin/auth/patches/02-github.auth.ts.patch +++ b/packages/cli/src/commands/admin/auth/patches/02-github.Use-signIn-resolver.patch @@ -1,3 +1,5 @@ +--- a/packages/backend/src/plugins/auth.ts ++++ b/packages/backend/src/plugins/auth.ts @@ -40,10 +40 @@ export default async function createPlugin( - resolver(_, ctx) { - const userRef = 'user:default/guest'; // Must be a full entity reference