Skip to content

Commit cce36b6

Browse files
author
Pablo Mendez
committed
validate notifications file
1 parent 05234d5 commit cce36b6

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

src/files/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from "./compose/index.js";
22
export * from "./manifest/index.js";
33
export * from "./setupWizard/index.js";
4+
export * from "./notifications/index.js";

src/files/notifications/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { readNotificationsIfExists } from "./readNotificationsIfExists.js";
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import fs from "fs";
2+
import path from "path";
3+
import yaml from "js-yaml";
4+
import { defaultDir } from "../../params.js";
5+
import { readFile } from "../../utils/file.js";
6+
import { NotificationsConfig, releaseFiles} from "@dappnode/types";
7+
8+
export function readNotificationsIfExists(dir?: string): NotificationsConfig | null {
9+
const dirPath = dir || defaultDir;
10+
const notificationsFileName = fs
11+
.readdirSync(dirPath)
12+
.find(file => releaseFiles.notifications.regex.test(file));
13+
14+
if (!notificationsFileName) return null;
15+
const data = readFile(path.join(dirPath, notificationsFileName));
16+
17+
// Parse notifications in try catch block to show a comprehensive error message
18+
try {
19+
return yaml.load(data);
20+
} catch (e) {
21+
throw Error(`Error parsing notifications: ${e.message}`);
22+
}
23+
}

src/tasks/buildAndUpload/getFileValidationTask.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import {
44
validateComposeSchema,
55
validateManifestSchema,
66
validateSetupWizardSchema,
7-
validateDappnodeCompose
7+
validateDappnodeCompose,
8+
validateNotificationsSchema
89
} from "@dappnode/schemas";
9-
import { getComposePath, readSetupWizardIfExists } from "../../files/index.js";
10+
import { getComposePath, readNotificationsIfExists, readSetupWizardIfExists } from "../../files/index.js";
1011
import { CliError } from "../../params.js";
1112

1213
export function getFileValidationTask({
@@ -30,9 +31,11 @@ async function validatePackageFiles({
3031
rootDir: string;
3132
}): Promise<void> {
3233
const setupWizard = readSetupWizardIfExists(rootDir);
33-
3434
if (setupWizard) validateSetupWizardSchema(setupWizard);
3535

36+
const notifications = readNotificationsIfExists(rootDir);
37+
if (notifications) validateNotificationsSchema(notifications);
38+
3639
for (const pkgProps of packagesToBuildProps)
3740
await validateVariantFiles(pkgProps);
3841
}

0 commit comments

Comments
 (0)