cli: Simplify patching by reading target file from path
Signed-off-by: Marcus Eide <eide@spotify.com>
This commit is contained in:
committed by
Philipp Hugenroth
parent
20fc473568
commit
977a5c8f96
@@ -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<string, string> = {
|
||||
'App.tsx': APP_TSX_FILE,
|
||||
'auth.ts': AUTH_BACKEND_PLUGIN_FILE,
|
||||
};
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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');
|
||||
};
|
||||
|
||||
+2
@@ -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 {
|
||||
+2
@@ -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
|
||||
Reference in New Issue
Block a user