2023-03-19 15:52:41 +01:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2024-02-11 20:15:18 +01:00
|
|
|
import { getThisCodebaseRootDirPath } from "./tools/getThisCodebaseRootDirPath";
|
2023-03-19 15:52:41 +01:00
|
|
|
import cliSelect from "cli-select";
|
2024-05-15 05:14:01 +02:00
|
|
|
import { loginThemePageIds, accountThemePageIds, type LoginThemePageId, type AccountThemePageId } from "./shared/pageIds";
|
2023-03-19 15:52:41 +01:00
|
|
|
import { capitalize } from "tsafe/capitalize";
|
|
|
|
import { readFile, writeFile } from "fs/promises";
|
|
|
|
import { existsSync } from "fs";
|
|
|
|
import { join as pathJoin, relative as pathRelative } from "path";
|
|
|
|
import { kebabCaseToCamelCase } from "./tools/kebabCaseToSnakeCase";
|
2023-03-20 00:03:15 +01:00
|
|
|
import { assert, Equals } from "tsafe/assert";
|
2024-05-15 05:14:01 +02:00
|
|
|
import { getThemeSrcDirPath } from "./shared/getThemeSrcDirPath";
|
|
|
|
import { themeTypes, type ThemeType } from "./shared/constants";
|
|
|
|
import type { CliCommandOptions } from "./main";
|
|
|
|
import { readBuildOptions } from "./shared/buildOptions";
|
2023-03-19 15:52:41 +01:00
|
|
|
|
2024-05-15 05:14:01 +02:00
|
|
|
export async function command(params: { cliCommandOptions: CliCommandOptions }) {
|
|
|
|
const { cliCommandOptions } = params;
|
2023-03-20 00:03:15 +01:00
|
|
|
|
2024-05-15 05:14:01 +02:00
|
|
|
const buildOptions = readBuildOptions({
|
|
|
|
cliCommandOptions
|
2024-02-11 16:17:38 +01:00
|
|
|
});
|
2023-09-03 23:26:34 +02:00
|
|
|
|
2024-05-15 05:14:01 +02:00
|
|
|
console.log("Select a theme type");
|
|
|
|
|
2023-03-20 00:03:15 +01:00
|
|
|
const { value: themeType } = await cliSelect<ThemeType>({
|
|
|
|
"values": [...themeTypes]
|
2023-03-19 15:52:41 +01:00
|
|
|
}).catch(() => {
|
|
|
|
console.log("Aborting");
|
|
|
|
|
|
|
|
process.exit(-1);
|
|
|
|
});
|
|
|
|
|
2023-03-20 00:03:15 +01:00
|
|
|
console.log("Select a page you would like to eject");
|
|
|
|
|
|
|
|
const { value: pageId } = await cliSelect<LoginThemePageId | AccountThemePageId>({
|
|
|
|
"values": (() => {
|
|
|
|
switch (themeType) {
|
|
|
|
case "login":
|
|
|
|
return [...loginThemePageIds];
|
|
|
|
case "account":
|
|
|
|
return [...accountThemePageIds];
|
|
|
|
}
|
|
|
|
assert<Equals<typeof themeType, never>>(false);
|
|
|
|
})()
|
|
|
|
}).catch(() => {
|
|
|
|
console.log("Aborting");
|
2023-03-19 15:52:41 +01:00
|
|
|
|
2023-03-20 00:03:15 +01:00
|
|
|
process.exit(-1);
|
|
|
|
});
|
|
|
|
|
|
|
|
const pageBasename = capitalize(kebabCaseToCamelCase(pageId)).replace(/ftl$/, "tsx");
|
2023-03-19 15:52:41 +01:00
|
|
|
|
2024-05-15 05:14:01 +02:00
|
|
|
const { themeSrcDirPath } = getThemeSrcDirPath({ "reactAppRootDirPath": buildOptions.reactAppRootDirPath });
|
2023-03-25 04:56:17 +01:00
|
|
|
|
|
|
|
const targetFilePath = pathJoin(themeSrcDirPath, themeType, "pages", pageBasename);
|
2023-03-19 15:52:41 +01:00
|
|
|
|
|
|
|
if (existsSync(targetFilePath)) {
|
|
|
|
console.log(`${pageId} is already ejected, ${pathRelative(process.cwd(), targetFilePath)} already exists`);
|
|
|
|
|
|
|
|
process.exit(-1);
|
|
|
|
}
|
|
|
|
|
2024-02-11 20:15:18 +01:00
|
|
|
await writeFile(targetFilePath, await readFile(pathJoin(getThisCodebaseRootDirPath(), "src", themeType, "pages", pageBasename)));
|
2023-03-19 15:52:41 +01:00
|
|
|
|
|
|
|
console.log(`${pathRelative(process.cwd(), targetFilePath)} created`);
|
2024-05-15 05:14:01 +02:00
|
|
|
}
|