chore: breaking out into function

Signed-off-by: Alper Altay <alper.altay@lego.com>
This commit is contained in:
Alper Altay
2024-06-25 12:38:10 +02:00
parent 1e88954016
commit e053c105f4
3 changed files with 40 additions and 48 deletions
@@ -20,7 +20,7 @@ import camelCase from 'lodash/camelCase';
import { paths } from '../../paths';
import { addCodeownersEntry, getCodeownersFilePath } from '../../codeowners';
import { CreateContext, createFactory } from '../types';
import { addPackageDependency, Task } from '../../tasks';
import { addPackageDependency, addToBackend, Task } from '../../tasks';
import {
moduleIdIdPrompt,
ownerPrompt,
@@ -91,29 +91,9 @@ 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');
}
await addToBackend(name, {
defaultVersion: ctx.defaultVersion,
type: 'module',
});
if (options.owner) {
@@ -20,7 +20,7 @@ import camelCase from 'lodash/camelCase';
import { paths } from '../../paths';
import { addCodeownersEntry, getCodeownersFilePath } from '../../codeowners';
import { CreateContext, createFactory } from '../types';
import { addPackageDependency, Task } from '../../tasks';
import { addPackageDependency, addToBackend, Task } from '../../tasks';
import { ownerPrompt, pluginIdPrompt } from './common/prompts';
import { executePluginPackageTemplate } from './common/tasks';
import { resolvePackageName } from './common/util';
@@ -79,29 +79,9 @@ export const backendPlugin = createFactory<Options>({
);
});
await Task.forItem('backend', 'adding plugin', 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');
}
await addToBackend(name, {
defaultVersion: ctx.defaultVersion,
type: 'plugin',
});
}
+32
View File
@@ -196,3 +196,35 @@ export async function addPackageDependency(
throw new Error(`Failed to add package dependencies, ${error}`);
}
}
export async function addToBackend(
name: string,
ctx: { defaultVersion: string; type: 'plugin' | 'module' },
) {
if (await fs.pathExists(paths.resolveTargetRoot('packages/backend'))) {
await Task.forItem('backend', `adding ${ctx.type}`, 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');
}
});
}
}