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