|
| 1 | +import * as _ from "lodash"; |
| 2 | +import { IProjectData } from "../../definitions/project"; |
| 3 | +import { IPluginsService, IPluginData } from "../../definitions/plugins"; |
| 4 | +import { ICommand, ICommandParameter } from "../../common/definitions/commands"; |
| 5 | +import { IErrors, IFileSystem } from "../../common/declarations"; |
| 6 | +import { injector } from "../../common/yok"; |
| 7 | +import path = require("path"); |
| 8 | +import { HOOKS_DIR_NAME } from "../../constants"; |
| 9 | +import { createTable } from "../../common/helpers"; |
| 10 | +import nsHooks = require("@nativescript/hook"); |
| 11 | +export class HooksPluginCommand implements ICommand { |
| 12 | + public allowedParameters: ICommandParameter[] = []; |
| 13 | + |
| 14 | + constructor( |
| 15 | + private $pluginsService: IPluginsService, |
| 16 | + private $projectData: IProjectData, |
| 17 | + private $errors: IErrors, |
| 18 | + private $fs: IFileSystem, |
| 19 | + private $logger: ILogger, |
| 20 | + ) { |
| 21 | + this.$projectData.initializeProjectData(); |
| 22 | + } |
| 23 | + |
| 24 | + public async execute(args: string[]): Promise<void> { |
| 25 | + const isList: boolean = |
| 26 | + args.length > 0 && args[0] === "list" ? true : false; |
| 27 | + const plugins: IPluginData[] = |
| 28 | + await this.$pluginsService.getAllInstalledPlugins(this.$projectData); |
| 29 | + if (plugins && plugins.length > 0) { |
| 30 | + const hooksDir = path.join(this.$projectData.projectDir, HOOKS_DIR_NAME); |
| 31 | + console.log(hooksDir); |
| 32 | + const pluginsWithHooks: IPluginData[] = []; |
| 33 | + for (const plugin of plugins) { |
| 34 | + if (plugin.nativescript?.hooks?.length > 0) { |
| 35 | + pluginsWithHooks.push(plugin); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + if (isList) { |
| 40 | + const headers: string[] = ["Plugin", "HookName", "HookPath"]; |
| 41 | + const hookDataData: string[][] = pluginsWithHooks.flatMap((plugin) => |
| 42 | + plugin.nativescript.hooks.map( |
| 43 | + (hook: { type: string; script: string }) => { |
| 44 | + return [plugin.name, hook.type, hook.script]; |
| 45 | + }, |
| 46 | + ), |
| 47 | + ); |
| 48 | + const hookDataTable: any = createTable(headers, hookDataData); |
| 49 | + this.$logger.info("Hooks:"); |
| 50 | + this.$logger.info(hookDataTable.toString()); |
| 51 | + } else { |
| 52 | + if (pluginsWithHooks.length === 0) { |
| 53 | + if (!this.$fs.exists(hooksDir)) { |
| 54 | + this.$fs.createDirectory(hooksDir); |
| 55 | + } |
| 56 | + } |
| 57 | + for (const plugin of pluginsWithHooks) { |
| 58 | + nsHooks(plugin.fullPath).postinstall(); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public async canExecute(args: string[]): Promise<boolean> { |
| 65 | + if (args?.length > 50) { |
| 66 | + this.$errors.fail(`Plugin is already installed.`); |
| 67 | + } |
| 68 | + return true; |
| 69 | + } |
| 70 | +} |
| 71 | +injector.registerCommand(["plugin|hooks"], HooksPluginCommand); |
0 commit comments