|
| 1 | +import { IProjectData } from "../../definitions/project"; |
| 2 | +import { IPluginsService, IPluginData } from "../../definitions/plugins"; |
| 3 | +import { ICommand, ICommandParameter } from "../../common/definitions/commands"; |
| 4 | +import { IErrors, IFileSystem } from "../../common/declarations"; |
| 5 | +import { injector } from "../../common/yok"; |
| 6 | +import path = require("path"); |
| 7 | +import * as crypto from "crypto"; |
| 8 | +import { |
| 9 | + HooksVerify, |
| 10 | + LOCK_FILE_NAME, |
| 11 | + OutputHook, |
| 12 | + OutputPlugin, |
| 13 | +} from "./common"; |
| 14 | + |
| 15 | +export class HooksLockPluginCommand implements ICommand { |
| 16 | + public allowedParameters: ICommandParameter[] = []; |
| 17 | + |
| 18 | + constructor( |
| 19 | + private $pluginsService: IPluginsService, |
| 20 | + private $projectData: IProjectData, |
| 21 | + private $errors: IErrors, |
| 22 | + private $fs: IFileSystem, |
| 23 | + private $logger: ILogger, |
| 24 | + ) { |
| 25 | + this.$projectData.initializeProjectData(); |
| 26 | + } |
| 27 | + |
| 28 | + public async execute(): Promise<void> { |
| 29 | + const plugins: IPluginData[] = |
| 30 | + await this.$pluginsService.getAllInstalledPlugins(this.$projectData); |
| 31 | + if (plugins && plugins.length > 0) { |
| 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 | + await this.writeHooksLockFile( |
| 40 | + pluginsWithHooks, |
| 41 | + this.$projectData.projectDir, |
| 42 | + ); |
| 43 | + } else { |
| 44 | + this.$logger.info("No plugins with hooks found."); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public async canExecute(args: string[]): Promise<boolean> { |
| 49 | + return true; |
| 50 | + } |
| 51 | + |
| 52 | + private async writeHooksLockFile( |
| 53 | + plugins: IPluginData[], |
| 54 | + outputDir: string, |
| 55 | + ): Promise<void> { |
| 56 | + const output: OutputPlugin[] = []; |
| 57 | + |
| 58 | + for (const plugin of plugins) { |
| 59 | + const hooks: OutputHook[] = []; |
| 60 | + |
| 61 | + for (const hook of plugin.nativescript?.hooks || []) { |
| 62 | + try { |
| 63 | + const fileContent = this.$fs.readFile( |
| 64 | + path.join(plugin.fullPath, hook.script), |
| 65 | + ); |
| 66 | + const hash = crypto |
| 67 | + .createHash("sha256") |
| 68 | + .update(fileContent) |
| 69 | + .digest("hex"); |
| 70 | + |
| 71 | + hooks.push({ |
| 72 | + type: hook.type, |
| 73 | + hash, |
| 74 | + }); |
| 75 | + } catch (err) { |
| 76 | + this.$logger.warn( |
| 77 | + `Warning: Failed to read script '${hook.script}' for plugin '${plugin.name}'. Skipping this hook.`, |
| 78 | + ); |
| 79 | + continue; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + output.push({ name: plugin.name, hooks }); |
| 84 | + } |
| 85 | + |
| 86 | + const filePath = path.resolve(outputDir, LOCK_FILE_NAME); |
| 87 | + |
| 88 | + try { |
| 89 | + this.$fs.writeFile(filePath, JSON.stringify(output, null, 2), "utf8"); |
| 90 | + this.$logger.info(`✅ ${LOCK_FILE_NAME} written to: ${filePath}`); |
| 91 | + } catch (err) { |
| 92 | + this.$errors.fail(`❌ Failed to write ${LOCK_FILE_NAME}: ${err}`); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +export class HooksVerifyPluginCommand extends HooksVerify implements ICommand { |
| 98 | + public allowedParameters: ICommandParameter[] = []; |
| 99 | + |
| 100 | + constructor( |
| 101 | + private $pluginsService: IPluginsService, |
| 102 | + $projectData: IProjectData, |
| 103 | + $errors: IErrors, |
| 104 | + $fs: IFileSystem, |
| 105 | + $logger: ILogger, |
| 106 | + ) { |
| 107 | + super($projectData, $errors, $fs, $logger); |
| 108 | + } |
| 109 | + |
| 110 | + public async execute(): Promise<void> { |
| 111 | + const plugins: IPluginData[] = |
| 112 | + await this.$pluginsService.getAllInstalledPlugins(this.$projectData); |
| 113 | + if (plugins && plugins.length > 0) { |
| 114 | + const pluginsWithHooks: IPluginData[] = []; |
| 115 | + for (const plugin of plugins) { |
| 116 | + if (plugin.nativescript?.hooks?.length > 0) { |
| 117 | + pluginsWithHooks.push(plugin); |
| 118 | + } |
| 119 | + } |
| 120 | + await this.verifyHooksLock( |
| 121 | + pluginsWithHooks, |
| 122 | + path.join(this.$projectData.projectDir, LOCK_FILE_NAME), |
| 123 | + ); |
| 124 | + } else { |
| 125 | + this.$logger.info("No plugins with hooks found."); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + public async canExecute(args: string[]): Promise<boolean> { |
| 130 | + return true; |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +injector.registerCommand(["hooks|lock"], HooksLockPluginCommand); |
| 135 | +injector.registerCommand(["hooks|verify"], HooksVerifyPluginCommand); |
0 commit comments