|
| 1 | +import { Command, EnumType } from '@cliffy/command' |
| 2 | +import { Config } from '../../lib/config.ts' |
| 3 | +import type { GlobalOptions } from '../../lib/types.ts' |
| 4 | +import { createProject } from '../../lib/project.ts' |
| 5 | +import { findFilesByExtension } from '../../lib/utils.ts' |
| 6 | +import * as path from '@std/path' |
| 7 | +import { findPluginFile, readUPluginFile, readUProjectFile } from '../../lib/project-info.ts' |
| 8 | + |
| 9 | +async function getEnabledPlugins(pluginName: string, projectPath: string, enginePath: string, refArray: Array<string>) { |
| 10 | + const pluginArray: Array<string> = [] |
| 11 | + const match = await findPluginFile(pluginName, projectPath, enginePath) |
| 12 | + if (match && match != '') { |
| 13 | + const pluginData = await readUPluginFile(match) |
| 14 | + |
| 15 | + if (pluginData && pluginData.Plugins) { |
| 16 | + for (const plugin of pluginData.Plugins) { |
| 17 | + if (plugin.Enabled) { |
| 18 | + if (![...refArray, ...pluginArray].includes(plugin.Name)) { |
| 19 | + pluginArray.push(plugin.Name) |
| 20 | + } |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + const subPlugins: Array<string> = [] |
| 25 | + for (const pluginName of pluginArray) { |
| 26 | + if (![...refArray, ...subPlugins].includes(pluginName)) { |
| 27 | + const subPluginArray = await getEnabledPlugins(pluginName, projectPath, enginePath, [ |
| 28 | + ...pluginArray, |
| 29 | + ...refArray, |
| 30 | + ...subPlugins, |
| 31 | + ]) |
| 32 | + subPlugins.push(...subPluginArray) |
| 33 | + } |
| 34 | + } |
| 35 | + pluginArray.push(...subPlugins) |
| 36 | + } |
| 37 | + } else { |
| 38 | + console.log(`Unable to find the plugin ${pluginName}`) |
| 39 | + } |
| 40 | + return pluginArray |
| 41 | +} |
| 42 | + |
| 43 | +enum ListTarget { |
| 44 | + All = 'all', |
| 45 | + Referenced = 'referenced', |
| 46 | + Project = 'project', |
| 47 | + Engine = 'engine', |
| 48 | + Default = 'default', |
| 49 | +} |
| 50 | + |
| 51 | +export type ListOptions = typeof list extends Command<void, void, infer Options, infer Argument, GlobalOptions> |
| 52 | + ? Options |
| 53 | + : never |
| 54 | + |
| 55 | +export const list = new Command<GlobalOptions>() |
| 56 | + .description(` |
| 57 | + Lists plugins |
| 58 | + target - defaults to referenced: |
| 59 | + * all - Lists all plugins from the engine and project |
| 60 | + * referenced - Lists all plugins referenced by the project |
| 61 | + * engine - Lists all engine plugins |
| 62 | + * project - Lists only plugins in the project plugins |
| 63 | + * default - Lists all plugins that are enabled by default |
| 64 | + `) |
| 65 | + .type('ListTarget', new EnumType(ListTarget)) |
| 66 | + .arguments('<target:ListTarget>') |
| 67 | + .option('-r, --recursive', 'List all nested plugins that are enabled when used with "referenced" or "default"', { |
| 68 | + default: false, |
| 69 | + }) |
| 70 | + .action(async (options, target = ListTarget.Project) => { |
| 71 | + const { recursive } = options as ListOptions |
| 72 | + const config = Config.getInstance() |
| 73 | + const { engine: { path: enginePath }, project: { path: projectPath } } = config.mergeConfigCLIConfig({ |
| 74 | + cliOptions: options, |
| 75 | + }) |
| 76 | + const project = await createProject(enginePath, projectPath) |
| 77 | + const projectData = await readUProjectFile(project.projectFileVars.projectFullPath) |
| 78 | + |
| 79 | + switch (target) { |
| 80 | + case ListTarget.All: { |
| 81 | + const projectPlugins = await findFilesByExtension(path.join(projectPath, 'Plugins'), 'uplugin', true) |
| 82 | + const enginePlugins = await findFilesByExtension(path.join(enginePath, 'Engine', 'Plugins'), 'uplugin', true) |
| 83 | + console.log('Project Plugins:\n') |
| 84 | + projectPlugins.forEach((plugin) => { |
| 85 | + console.log(path.basename(plugin, '.uplugin')) |
| 86 | + }) |
| 87 | + console.log('Engine Plugins:\n') |
| 88 | + enginePlugins.forEach((plugin) => { |
| 89 | + console.log(path.basename(plugin, '.uplugin')) |
| 90 | + }) |
| 91 | + break |
| 92 | + } |
| 93 | + case ListTarget.Referenced: { |
| 94 | + const allEnabledPlugins: Array<string> = [] |
| 95 | + |
| 96 | + if (projectData && projectData.Plugins) { |
| 97 | + for (const plugin of projectData.Plugins) { |
| 98 | + if (plugin.Enabled) { |
| 99 | + allEnabledPlugins.push(plugin.Name) |
| 100 | + } |
| 101 | + if (recursive) { |
| 102 | + const enabledPlugins = await getEnabledPlugins(plugin.Name, projectPath, enginePath, allEnabledPlugins) |
| 103 | + allEnabledPlugins.push(...enabledPlugins) |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + const uniquePlugins = [...new Set(allEnabledPlugins)] |
| 108 | + console.log('All Referenced Plugins:') |
| 109 | + console.log(uniquePlugins) |
| 110 | + break |
| 111 | + } |
| 112 | + case ListTarget.Project: { |
| 113 | + const projectPlugins = await findFilesByExtension(path.join(projectPath, 'Plugins'), 'uplugin', true) |
| 114 | + console.log('Project Plugins:\n') |
| 115 | + projectPlugins.forEach((plugin) => { |
| 116 | + console.log(path.basename(plugin, '.uplugin')) |
| 117 | + }) |
| 118 | + break |
| 119 | + } |
| 120 | + case ListTarget.Engine: { |
| 121 | + const enginePlugins = await findFilesByExtension(path.join(enginePath, 'Engine', 'Plugins'), 'uplugin', true) |
| 122 | + console.log('Engine Plugins:\n') |
| 123 | + enginePlugins.forEach((plugin) => { |
| 124 | + console.log(path.basename(plugin, '.uplugin')) |
| 125 | + }) |
| 126 | + break |
| 127 | + } |
| 128 | + case ListTarget.Default: { |
| 129 | + const projectPlugins = await findFilesByExtension(path.join(projectPath, 'Plugins'), 'uplugin', true) |
| 130 | + console.log('Project Plugins enabled by default:\n') |
| 131 | + for (const plugin of projectPlugins) { |
| 132 | + const uplugin = await readUPluginFile(plugin) |
| 133 | + if (uplugin && uplugin.EnabledByDefault) { |
| 134 | + console.log(path.basename(plugin, '.uplugin')) |
| 135 | + } |
| 136 | + } |
| 137 | + console.log('Engine Plugins enabled by default:\n') |
| 138 | + const enginePlugins = await findFilesByExtension(path.join(enginePath, 'Engine', 'Plugins'), 'uplugin', true) |
| 139 | + for (const plugin of enginePlugins) { |
| 140 | + const uplugin = await readUPluginFile(plugin) |
| 141 | + if (uplugin && uplugin.EnabledByDefault) { |
| 142 | + console.log(path.basename(plugin, '.uplugin')) |
| 143 | + } |
| 144 | + } |
| 145 | + break |
| 146 | + } |
| 147 | + } |
| 148 | + }) |
0 commit comments