Eject file command implementation

This commit is contained in:
Joseph Garrone 2024-11-09 20:33:53 +01:00
parent a73281d46d
commit 63877d53be
3 changed files with 107 additions and 5 deletions

68
src/bin/eject-file.ts Normal file
View File

@ -0,0 +1,68 @@
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

@ -241,6 +241,39 @@ program
}
});
program
.command<{
file: string;
}>({
name: "eject-file",
description: "Take ownership over a given file"
})
.option({
key: "file",
name: (() => {
const name = "file";
optionsKeys.push(name);
return name;
})(),
description: [
"Relative path of the file relative to the directory of your keycloak theme source",
"Example `--file src/login/page/Login.tsx`"
].join(" ")
})
.task({
skip,
handler: async ({ projectDirPath, file }) => {
const { command } = await import("./eject-file");
await command({
buildContext: getBuildContext({ projectDirPath }),
cliCommandOptions: { file }
});
}
});
// Fallback to build command if no command is provided
{
const [, , ...rest] = process.argv;

View File

@ -36,16 +36,17 @@ export async function getUiModuleFileSourceCodeReadyToBeCopied(params: {
if (isForEjection) {
return [
`/*`,
` This file was ejected from ${uiModuleName} version ${uiModuleVersion}.`,
`This file was ejected from ${uiModuleName} version ${uiModuleVersion}.`,
`*/`
].join("\n");
} else {
return [
`/*`,
` WARNING: Before modifying this file run the following command:`,
` \`npx keycloakify eject-file ${fileRelativePath.split(pathSep).join("/")}\``,
` `,
` This file comes from ${uiModuleName} version ${uiModuleVersion}.`,
`WARNING: Before modifying this file run the following command:`,
``,
`npx keycloakify eject-file --file ${fileRelativePath.split(pathSep).join("/")}\``,
``,
`This file comes from ${uiModuleName} version ${uiModuleVersion}.`,
`*/`
];
}