Consistent naming scheme 'eject' -> 'own'

This commit is contained in:
Joseph Garrone 2024-12-23 18:34:03 +01:00
parent 4403f00274
commit cc3d0d61dd
5 changed files with 69 additions and 54 deletions

View File

@ -30,9 +30,10 @@ export async function command(params: {
const uiModuleMetas = await getUiModuleMetas({ buildContext });
const fileRelativePaths = uiModuleMetas
.map(({ files }) =>
files
const arr = uiModuleMetas
.map(uiModuleMeta => ({
uiModuleMeta,
fileRelativePaths: uiModuleMeta.files
.map(({ fileRelativePath }) => fileRelativePath)
.filter(
fileRelativePath =>
@ -42,55 +43,67 @@ export async function command(params: {
filePath: fileRelativePath
})
)
)
.flat();
}))
.filter(({ fileRelativePaths }) => fileRelativePaths.length !== 0);
if (fileRelativePaths.length === 0) {
if (arr.length === 0) {
console.log(
chalk.yellow("There is no UI module files matching the provided path.")
);
process.exit(1);
}
for (const fileRelativePath of fileRelativePaths) {
const uiModuleMeta = uiModuleMetas.find(({ files }) =>
files
.map(({ fileRelativePath }) => fileRelativePath)
.includes(fileRelativePath)
);
const { ownedFilesRelativePaths: ownedFilesRelativePaths_before } =
await readManagedGitignoreFile({
buildContext
});
if (!uiModuleMeta) {
throw new Error(`No UI module found for the file ${fileRelativePath}`);
}
const ownedFilesRelativePaths_toAdd: string[] = [];
for (const { uiModuleMeta, fileRelativePaths } of arr) {
const uiModuleDirPath = await getInstalledModuleDirPath({
moduleName: uiModuleMeta.moduleName,
packageJsonDirPath: pathDirname(buildContext.packageJsonFilePath),
projectDirPath: buildContext.projectDirPath
});
const sourceCode = await getUiModuleFileSourceCodeReadyToBeCopied({
buildContext,
fileRelativePath,
isForEjection: true,
uiModuleName: uiModuleMeta.moduleName,
uiModuleDirPath,
uiModuleVersion: uiModuleMeta.version
});
for (const fileRelativePath of fileRelativePaths) {
if (ownedFilesRelativePaths_before.includes(fileRelativePath)) {
console.log(
chalk.yellow(`You already have ownership over "${fileRelativePath}".`)
);
continue;
}
await fsPr.writeFile(
pathJoin(buildContext.themeSrcDirPath, fileRelativePath),
sourceCode
);
const sourceCode = await getUiModuleFileSourceCodeReadyToBeCopied({
buildContext,
fileRelativePath,
isOwnershipAction: true,
uiModuleName: uiModuleMeta.moduleName,
uiModuleDirPath,
uiModuleVersion: uiModuleMeta.version
});
const { ejectedFilesRelativePaths } = await readManagedGitignoreFile({
buildContext
});
await fsPr.writeFile(
pathJoin(buildContext.themeSrcDirPath, fileRelativePath),
sourceCode
);
await writeManagedGitignoreFile({
buildContext,
uiModuleMetas,
ejectedFilesRelativePaths: [...ejectedFilesRelativePaths, fileRelativePath]
});
ownedFilesRelativePaths_toAdd.push(fileRelativePath);
}
}
if (ownedFilesRelativePaths_toAdd.length === 0) {
console.log(chalk.yellow("No new file claimed."));
process.exit(1);
}
await writeManagedGitignoreFile({
buildContext,
uiModuleMetas,
ownedFilesRelativePaths: [
...ownedFilesRelativePaths_before,
...ownedFilesRelativePaths_toAdd
]
});
}

View File

@ -14,7 +14,7 @@ assert<BuildContext extends BuildContextLike ? true : false>();
export async function getUiModuleFileSourceCodeReadyToBeCopied(params: {
buildContext: BuildContextLike;
fileRelativePath: string;
isForEjection: boolean;
isOwnershipAction: boolean;
uiModuleDirPath: string;
uiModuleName: string;
uiModuleVersion: string;
@ -23,7 +23,7 @@ export async function getUiModuleFileSourceCodeReadyToBeCopied(params: {
buildContext,
uiModuleDirPath,
fileRelativePath,
isForEjection,
isOwnershipAction,
uiModuleName,
uiModuleVersion
} = params;
@ -35,8 +35,10 @@ export async function getUiModuleFileSourceCodeReadyToBeCopied(params: {
sourceCode = addCommentToSourceCode({
sourceCode,
fileRelativePath,
commentLines: isForEjection
? [`This file was ejected from ${uiModuleName} version ${uiModuleVersion}.`]
commentLines: isOwnershipAction
? [
`This file was claimed for ownership from ${uiModuleName} version ${uiModuleVersion}.`
]
: [
`WARNING: Before modifying this file run the following command:`,
``,

View File

@ -17,15 +17,15 @@ export type BuildContextLike = {
assert<BuildContext extends BuildContextLike ? true : false>();
const DELIMITER_START = `# === Ejected files start ===`;
const DELIMITER_END = `# === Ejected files end =====`;
const DELIMITER_START = `# === Owned files start ===`;
const DELIMITER_END = `# === Owned files end =====`;
export async function writeManagedGitignoreFile(params: {
buildContext: BuildContextLike;
uiModuleMetas: UiModuleMeta[];
ejectedFilesRelativePaths: string[];
ownedFilesRelativePaths: string[];
}): Promise<void> {
const { buildContext, uiModuleMetas, ejectedFilesRelativePaths } = params;
const { buildContext, uiModuleMetas, ownedFilesRelativePaths } = params;
if (uiModuleMetas.length === 0) {
return;
@ -38,7 +38,7 @@ export async function writeManagedGitignoreFile(params: {
`# This file is managed by Keycloakify, do not edit it manually.`,
``,
DELIMITER_START,
...ejectedFilesRelativePaths
...ownedFilesRelativePaths
.map(fileRelativePath => fileRelativePath.split(pathSep).join("/"))
.map(line => `# ${line}`),
DELIMITER_END,
@ -50,7 +50,7 @@ export async function writeManagedGitignoreFile(params: {
.map(({ fileRelativePath }) => fileRelativePath)
.filter(
fileRelativePath =>
!ejectedFilesRelativePaths.includes(fileRelativePath)
!ownedFilesRelativePaths.includes(fileRelativePath)
)
.map(
fileRelativePath =>
@ -92,14 +92,14 @@ export async function writeManagedGitignoreFile(params: {
export async function readManagedGitignoreFile(params: {
buildContext: BuildContextLike;
}): Promise<{
ejectedFilesRelativePaths: string[];
ownedFilesRelativePaths: string[];
}> {
const { buildContext } = params;
const filePath = pathJoin(buildContext.themeSrcDirPath, ".gitignore");
if (!(await existsAsync(filePath))) {
return { ejectedFilesRelativePaths: [] };
return { ownedFilesRelativePaths: [] };
}
const contentStr = (await fsPr.readFile(filePath)).toString("utf8");
@ -116,10 +116,10 @@ export async function readManagedGitignoreFile(params: {
})();
if (payload === undefined) {
return { ejectedFilesRelativePaths: [] };
return { ownedFilesRelativePaths: [] };
}
const ejectedFilesRelativePaths = payload
const ownedFilesRelativePaths = payload
.split("\n")
.map(line => line.trim())
.map(line => line.replace(/^# /, ""))
@ -132,5 +132,5 @@ export async function readManagedGitignoreFile(params: {
)
.map(filePath => pathRelative(buildContext.themeSrcDirPath, filePath));
return { ejectedFilesRelativePaths };
return { ownedFilesRelativePaths };
}

View File

@ -22,13 +22,13 @@ export async function command(params: { buildContext: BuildContext }) {
uiModuleMetas
});
const { ejectedFilesRelativePaths } = await readManagedGitignoreFile({
const { ownedFilesRelativePaths } = await readManagedGitignoreFile({
buildContext
});
await writeManagedGitignoreFile({
buildContext,
ejectedFilesRelativePaths,
ownedFilesRelativePaths,
uiModuleMetas
});
@ -38,7 +38,7 @@ export async function command(params: { buildContext: BuildContext }) {
Promise.all(
uiModuleMeta.files.map(
async ({ fileRelativePath, copyableFilePath, hash }) => {
if (ejectedFilesRelativePaths.includes(fileRelativePath)) {
if (ownedFilesRelativePaths.includes(fileRelativePath)) {
return;
}

View File

@ -228,7 +228,7 @@ export async function getUiModuleMetas(params: {
await getUiModuleFileSourceCodeReadyToBeCopied({
buildContext,
fileRelativePath,
isForEjection: false,
isOwnershipAction: false,
uiModuleDirPath: dirPath,
uiModuleName: moduleName,
uiModuleVersion: version