From cc3d0d61ddcc2c7fad4a45d46ba79f52da5ccf34 Mon Sep 17 00:00:00 2001 From: Joseph Garrone Date: Mon, 23 Dec 2024 18:34:03 +0100 Subject: [PATCH] Consistent naming scheme 'eject' -> 'own' --- src/bin/own.ts | 83 +++++++++++-------- ...etUiModuleFileSourceCodeReadyToBeCopied.ts | 10 ++- src/bin/postinstall/managedGitignoreFile.ts | 22 ++--- src/bin/postinstall/postinstall.ts | 6 +- src/bin/postinstall/uiModuleMeta.ts | 2 +- 5 files changed, 69 insertions(+), 54 deletions(-) diff --git a/src/bin/own.ts b/src/bin/own.ts index 2cbf52c8..1f555a9a 100644 --- a/src/bin/own.ts +++ b/src/bin/own.ts @@ -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 + ] + }); } diff --git a/src/bin/postinstall/getUiModuleFileSourceCodeReadyToBeCopied.ts b/src/bin/postinstall/getUiModuleFileSourceCodeReadyToBeCopied.ts index a89605b0..d928b5e4 100644 --- a/src/bin/postinstall/getUiModuleFileSourceCodeReadyToBeCopied.ts +++ b/src/bin/postinstall/getUiModuleFileSourceCodeReadyToBeCopied.ts @@ -14,7 +14,7 @@ assert(); 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:`, ``, diff --git a/src/bin/postinstall/managedGitignoreFile.ts b/src/bin/postinstall/managedGitignoreFile.ts index 754215f2..0c0ea791 100644 --- a/src/bin/postinstall/managedGitignoreFile.ts +++ b/src/bin/postinstall/managedGitignoreFile.ts @@ -17,15 +17,15 @@ export type BuildContextLike = { assert(); -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 { - 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 }; } diff --git a/src/bin/postinstall/postinstall.ts b/src/bin/postinstall/postinstall.ts index bea41701..5f040c12 100644 --- a/src/bin/postinstall/postinstall.ts +++ b/src/bin/postinstall/postinstall.ts @@ -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; } diff --git a/src/bin/postinstall/uiModuleMeta.ts b/src/bin/postinstall/uiModuleMeta.ts index fbdc8db2..9c978f12 100644 --- a/src/bin/postinstall/uiModuleMeta.ts +++ b/src/bin/postinstall/uiModuleMeta.ts @@ -228,7 +228,7 @@ export async function getUiModuleMetas(params: { await getUiModuleFileSourceCodeReadyToBeCopied({ buildContext, fileRelativePath, - isForEjection: false, + isOwnershipAction: false, uiModuleDirPath: dirPath, uiModuleName: moduleName, uiModuleVersion: version