|
| 1 | +import chalk from "chalk"; |
| 2 | +import { |
| 3 | + getTemplateCategories, |
| 4 | + getTemplatesForCategory, |
| 5 | + getExtensionsGroupedByCategory, |
| 6 | + getCategoryData, |
| 7 | +} from "./templates"; |
| 8 | + |
| 9 | +/** |
| 10 | + * List all available templates grouped by category |
| 11 | + */ |
| 12 | +export const listTemplates = async () => { |
| 13 | + const categories = await getTemplateCategories(); |
| 14 | + |
| 15 | + console.log(chalk.bold.blue("\nAvailable Templates:")); |
| 16 | + |
| 17 | + for (const categorySlug of categories) { |
| 18 | + const categoryData = await getCategoryData(categorySlug); |
| 19 | + const templates = await getTemplatesForCategory(categorySlug); |
| 20 | + |
| 21 | + // Display category name if available, otherwise use the slug |
| 22 | + const categoryName = categoryData?.name || categorySlug; |
| 23 | + console.log(chalk.bold.green(`\n${categoryName}:`)); |
| 24 | + |
| 25 | + // Display category description if available |
| 26 | + if (categoryData?.description) { |
| 27 | + console.log(` ${categoryData.description}`); |
| 28 | + } |
| 29 | + |
| 30 | + templates.forEach((template) => { |
| 31 | + console.log( |
| 32 | + ` ${chalk.yellow(template.name)} (${chalk.cyan(template.slug)})` |
| 33 | + ); |
| 34 | + console.log(` ${template.description}`); |
| 35 | + if (template.labels && template.labels.length > 0) { |
| 36 | + console.log(` Keywords: ${template.labels.join(", ")}`); |
| 37 | + } |
| 38 | + }); |
| 39 | + } |
| 40 | +}; |
| 41 | + |
| 42 | +/** |
| 43 | + * List all available addons grouped by category |
| 44 | + * @param templateSlug Optional template slug to filter compatible addons |
| 45 | + * @param templateType Optional template type to filter compatible addons |
| 46 | + */ |
| 47 | +export const listAddons = async ({ |
| 48 | + templateSlug, |
| 49 | + templateType, |
| 50 | +}: { |
| 51 | + templateSlug?: string; |
| 52 | + templateType?: string; |
| 53 | +}) => { |
| 54 | + // If templateSlug is provided but templateType is not, try to get the template type |
| 55 | + if (templateSlug && !templateType) { |
| 56 | + templateType = await getTemplateTypeFromSlug(templateSlug); |
| 57 | + } |
| 58 | + |
| 59 | + const types = templateType ? [templateType, "all"] : ["all"]; |
| 60 | + const extensionsGroupedByCategory = await getExtensionsGroupedByCategory( |
| 61 | + types |
| 62 | + ); |
| 63 | + |
| 64 | + console.log(chalk.bold.blue("\nAvailable Addons:")); |
| 65 | + |
| 66 | + if (templateSlug) { |
| 67 | + console.log( |
| 68 | + chalk.bold.green(`\nCompatible with template: ${templateSlug}`) |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + for (const [categorySlug, extensions] of Object.entries( |
| 73 | + extensionsGroupedByCategory |
| 74 | + )) { |
| 75 | + const categoryData = await getCategoryData(categorySlug); |
| 76 | + |
| 77 | + // Display category name if available, otherwise use the slug |
| 78 | + const categoryName = categoryData?.name || categorySlug; |
| 79 | + console.log(chalk.bold.green(`\n${categoryName}:`)); |
| 80 | + |
| 81 | + // Display category description if available |
| 82 | + if (categoryData?.description) { |
| 83 | + console.log(` ${categoryData.description}`); |
| 84 | + } |
| 85 | + |
| 86 | + extensions.forEach((extension) => { |
| 87 | + console.log( |
| 88 | + ` ${chalk.yellow(extension.name)} (${chalk.cyan(extension.slug)})` |
| 89 | + ); |
| 90 | + console.log(` ${extension.description}`); |
| 91 | + if (extension.labels && extension.labels.length > 0) { |
| 92 | + console.log(` Keywords: ${extension.labels.join(", ")}`); |
| 93 | + } |
| 94 | + }); |
| 95 | + } |
| 96 | +}; |
| 97 | + |
| 98 | +/** |
| 99 | + * Get template type from template slug |
| 100 | + * @param templateSlug The template slug to look up |
| 101 | + */ |
| 102 | +export const getTemplateTypeFromSlug = async ( |
| 103 | + templateSlug: string |
| 104 | +): Promise<string | undefined> => { |
| 105 | + const categories = await getTemplateCategories(); |
| 106 | + |
| 107 | + for (const category of categories) { |
| 108 | + const templates = await getTemplatesForCategory(category); |
| 109 | + const template = templates.find((t) => t.slug === templateSlug); |
| 110 | + |
| 111 | + if (template) { |
| 112 | + return template.type; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + return undefined; |
| 117 | +}; |
0 commit comments