Reneame the 'eject-file' command to 'own'

This commit is contained in:
Joseph Garrone 2024-12-23 17:55:40 +01:00
parent eddfb8e634
commit 4403f00274
5 changed files with 110 additions and 83 deletions

View File

@ -1,68 +0,0 @@
import type { BuildContext } from "./shared/buildContext";
import { getUiModuleFileSourceCodeReadyToBeCopied } from "./postinstall/getUiModuleFileSourceCodeReadyToBeCopied";
import { getAbsoluteAndInOsFormatPath } from "./tools/getAbsoluteAndInOsFormatPath";
import { relative as pathRelative, dirname as pathDirname, join as pathJoin } from "path";
import { getUiModuleMetas } from "./postinstall/uiModuleMeta";
import { getInstalledModuleDirPath } from "./tools/getInstalledModuleDirPath";
import * as fsPr from "fs/promises";
import {
readManagedGitignoreFile,
writeManagedGitignoreFile
} from "./postinstall/managedGitignoreFile";
export async function command(params: {
buildContext: BuildContext;
cliCommandOptions: {
file: string;
};
}) {
const { buildContext, cliCommandOptions } = params;
const fileRelativePath = pathRelative(
buildContext.themeSrcDirPath,
getAbsoluteAndInOsFormatPath({
cwd: buildContext.themeSrcDirPath,
pathIsh: cliCommandOptions.file
})
);
const uiModuleMetas = await getUiModuleMetas({ buildContext });
const uiModuleMeta = uiModuleMetas.find(({ files }) =>
files.map(({ fileRelativePath }) => fileRelativePath).includes(fileRelativePath)
);
if (!uiModuleMeta) {
throw new Error(`No UI module found for the file ${fileRelativePath}`);
}
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
});
await fsPr.writeFile(
pathJoin(buildContext.themeSrcDirPath, fileRelativePath),
sourceCode
);
const { ejectedFilesRelativePaths } = await readManagedGitignoreFile({
buildContext
});
await writeManagedGitignoreFile({
buildContext,
uiModuleMetas,
ejectedFilesRelativePaths: [...ejectedFilesRelativePaths, fileRelativePath]
});
}

View File

@ -67,9 +67,7 @@ export async function command(params: { buildContext: BuildContext }) {
})(); })();
if (themeType === "admin") { if (themeType === "admin") {
console.log( console.log("Use `npx keycloakify own` command instead, see documentation");
"Use `npx keycloakify eject-file` command instead, see documentation"
);
process.exit(-1); process.exit(-1);
} }

View File

@ -262,37 +262,38 @@ program
program program
.command<{ .command<{
file: string; path: string;
}>({ }>({
name: "eject-file", name: "own",
description: [ description: [
"WARNING: Not usable yet, will be used for future features", "WARNING: Not usable yet, will be used for future features",
"Take ownership over a given file" "Take ownership over a given file"
].join(" ") ].join(" ")
}) })
.option({ .option({
key: "file", key: "path",
name: (() => { name: (() => {
const long = "file"; const long = "path";
const short = "f"; const short = "p";
optionsKeys.push(long, short); optionsKeys.push(long, short);
return { long, short }; return { long, short };
})(), })(),
description: [ description: [
"Relative path of the file relative to the directory of your keycloak theme source", "Relative path of the file or the directory that you want to take ownership over.",
"Example `--file src/login/page/Login.tsx`" "The path is relative to your theme directory.",
"Example `--path admin/page/Login.tsx`"
].join(" ") ].join(" ")
}) })
.task({ .task({
skip, skip,
handler: async ({ projectDirPath, file }) => { handler: async ({ projectDirPath, path }) => {
const { command } = await import("./eject-file"); const { command } = await import("./own");
await command({ await command({
buildContext: getBuildContext({ projectDirPath }), buildContext: getBuildContext({ projectDirPath }),
cliCommandOptions: { file } cliCommandOptions: { path }
}); });
} }
}); });

96
src/bin/own.ts Normal file
View File

@ -0,0 +1,96 @@
import type { BuildContext } from "./shared/buildContext";
import { getUiModuleFileSourceCodeReadyToBeCopied } from "./postinstall/getUiModuleFileSourceCodeReadyToBeCopied";
import { getAbsoluteAndInOsFormatPath } from "./tools/getAbsoluteAndInOsFormatPath";
import { relative as pathRelative, dirname as pathDirname, join as pathJoin } from "path";
import { getUiModuleMetas } from "./postinstall/uiModuleMeta";
import { getInstalledModuleDirPath } from "./tools/getInstalledModuleDirPath";
import * as fsPr from "fs/promises";
import {
readManagedGitignoreFile,
writeManagedGitignoreFile
} from "./postinstall/managedGitignoreFile";
import { isInside } from "./tools/isInside";
import chalk from "chalk";
export async function command(params: {
buildContext: BuildContext;
cliCommandOptions: {
path: string;
};
}) {
const { buildContext, cliCommandOptions } = params;
const fileOrDirectoryRelativePath = pathRelative(
buildContext.themeSrcDirPath,
getAbsoluteAndInOsFormatPath({
cwd: buildContext.themeSrcDirPath,
pathIsh: cliCommandOptions.path
})
);
const uiModuleMetas = await getUiModuleMetas({ buildContext });
const fileRelativePaths = uiModuleMetas
.map(({ files }) =>
files
.map(({ fileRelativePath }) => fileRelativePath)
.filter(
fileRelativePath =>
fileRelativePath === fileOrDirectoryRelativePath ||
isInside({
dirPath: fileOrDirectoryRelativePath,
filePath: fileRelativePath
})
)
)
.flat();
if (fileRelativePaths.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)
);
if (!uiModuleMeta) {
throw new Error(`No UI module found for the file ${fileRelativePath}`);
}
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
});
await fsPr.writeFile(
pathJoin(buildContext.themeSrcDirPath, fileRelativePath),
sourceCode
);
const { ejectedFilesRelativePaths } = await readManagedGitignoreFile({
buildContext
});
await writeManagedGitignoreFile({
buildContext,
uiModuleMetas,
ejectedFilesRelativePaths: [...ejectedFilesRelativePaths, fileRelativePath]
});
}
}

View File

@ -40,7 +40,7 @@ export async function getUiModuleFileSourceCodeReadyToBeCopied(params: {
: [ : [
`WARNING: Before modifying this file run the following command:`, `WARNING: Before modifying this file run the following command:`,
``, ``,
`$ npx keycloakify eject-file --file '${fileRelativePath.split(pathSep).join("/")}'`, `$ npx keycloakify own --path '${fileRelativePath.split(pathSep).join("/")}'`,
``, ``,
`This file comes from ${uiModuleName} version ${uiModuleVersion}.`, `This file comes from ${uiModuleName} version ${uiModuleVersion}.`,
`This file has been copied over to your repo by your postinstall script: \`npx keycloakify postinstall\`` `This file has been copied over to your repo by your postinstall script: \`npx keycloakify postinstall\``
@ -93,7 +93,7 @@ function addCommentToSourceCode(params: {
`<!--`, `<!--`,
...commentLines.map( ...commentLines.map(
line => line =>
` ${line.replace("--file", "-f").replace("Before modifying", "Before modifying or replacing")}` ` ${line.replace("--path", "-p").replace("Before modifying", "Before modifying or replacing")}`
), ),
`-->` `-->`
].join("\n"); ].join("\n");