feat: auto add modules to index.ts
Signed-off-by: Alper Altay <alper.altay@lego.com>
This commit is contained in:
@@ -26,6 +26,14 @@ import {
|
||||
import { backendModule } from './backendModule';
|
||||
import { createMockDirectory } from '@backstage/backend-test-utils';
|
||||
|
||||
const backendIndexTsContent = `
|
||||
import { createBackend } from '@backstage/backend-defaults';
|
||||
|
||||
const backend = createBackend();
|
||||
|
||||
backend.start();
|
||||
`;
|
||||
|
||||
describe('backendModule factory', () => {
|
||||
const mockDir = createMockDirectory();
|
||||
|
||||
@@ -44,6 +52,9 @@ describe('backendModule factory', () => {
|
||||
packages: {
|
||||
backend: {
|
||||
'package.json': JSON.stringify({}),
|
||||
src: {
|
||||
'index.ts': backendIndexTsContent,
|
||||
},
|
||||
},
|
||||
},
|
||||
plugins: {},
|
||||
@@ -86,8 +97,20 @@ describe('backendModule factory', () => {
|
||||
'Installing:',
|
||||
`moving plugins${sep}test-backend-module-tester-two`,
|
||||
'backend adding dependency',
|
||||
'backend adding module',
|
||||
]);
|
||||
|
||||
await expect(
|
||||
fs.readFile(mockDir.resolve('packages/backend/src/index.ts'), 'utf8'),
|
||||
).resolves.toBe(`
|
||||
import { createBackend } from '@backstage/backend-defaults';
|
||||
|
||||
const backend = createBackend();
|
||||
|
||||
backend.add(import('backstage-plugin-test-backend-module-tester-two'));
|
||||
backend.start();
|
||||
`);
|
||||
|
||||
await expect(
|
||||
fs.readJson(mockDir.resolve('packages/backend/package.json')),
|
||||
).resolves.toEqual({
|
||||
|
||||
@@ -91,6 +91,31 @@ export const backendModule = createFactory<Options>({
|
||||
});
|
||||
}
|
||||
|
||||
await Task.forItem('backend', 'adding module', async () => {
|
||||
const backendFilePath = paths.resolveTargetRoot(
|
||||
'packages/backend/src/index.ts',
|
||||
);
|
||||
if (!(await fs.pathExists(backendFilePath))) {
|
||||
return;
|
||||
}
|
||||
|
||||
const content = await fs.readFile(backendFilePath, 'utf8');
|
||||
const lines = content.split('\n');
|
||||
const backendAddLine = `backend.add(import('${name}'));`;
|
||||
|
||||
const backendStartIndex = lines.findIndex(line =>
|
||||
line.match(/backend.start/),
|
||||
);
|
||||
|
||||
if (backendStartIndex !== -1) {
|
||||
const [indentation] = lines[backendStartIndex].match(/^\s*/) ?? [];
|
||||
lines.splice(backendStartIndex, 0, indentation + backendAddLine);
|
||||
|
||||
const newContent = lines.join('\n');
|
||||
await fs.writeFile(backendFilePath, newContent, 'utf8');
|
||||
}
|
||||
});
|
||||
|
||||
if (options.owner) {
|
||||
await addCodeownersEntry(`/plugins/${dirName}`, options.owner);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user