|
1 | 1 | import fs from "fs"; |
2 | 2 | import path from "path"; |
3 | 3 | import yaml from "js-yaml"; |
| 4 | +import { merge } from "lodash-es"; |
4 | 5 | import { defaultDir } from "../../params.js"; |
5 | 6 | import { readFile } from "../../utils/file.js"; |
6 | 7 | import { SetupWizard, releaseFiles } from "@dappnode/types"; |
| 8 | +import { SetupWizardPaths } from "./types.js"; |
7 | 9 |
|
8 | | -export function readSetupWizardIfExists(dir?: string): SetupWizard | null { |
9 | | - const dirPath = dir || defaultDir; |
10 | | - const setupWizardFileName = fs |
11 | | - .readdirSync(dirPath) |
12 | | - .find(file => releaseFiles.setupWizard.regex.test(file)); |
| 10 | +/** |
| 11 | + * Reads one or multiple setup-wizard YAML files and merges them. Returns null if none exist. |
| 12 | + * @param {SetupWizardPaths[]} paths - Optional array of directory/file specs. |
| 13 | + * @returns {SetupWizard | null} |
| 14 | + * @throws {Error} Throws if parsing fails or non-YAML format is encountered. |
| 15 | + */ |
| 16 | +export function readSetupWizardIfExists( |
| 17 | + paths?: SetupWizardPaths[] |
| 18 | +): SetupWizard | null { |
| 19 | + try { |
| 20 | + // Determine list of file specs (default spec if no paths provided) |
| 21 | + const specs = paths && paths.length > 0 ? paths : [{}]; |
13 | 22 |
|
14 | | - if (!setupWizardFileName) return null; |
15 | | - const data = readFile(path.join(dirPath, setupWizardFileName)); |
| 23 | + // Resolve existing file paths |
| 24 | + const filePaths = specs |
| 25 | + .map(spec => { |
| 26 | + try { |
| 27 | + return findSetupWizardPath(spec); |
| 28 | + } catch { |
| 29 | + return undefined; |
| 30 | + } |
| 31 | + }) |
| 32 | + .filter((p): p is string => typeof p === "string"); |
16 | 33 |
|
17 | | - // Parse setupWizard in try catch block to show a comprehensive error message |
18 | | - try { |
19 | | - return yaml.load(data); |
| 34 | + if (filePaths.length === 0) return null; |
| 35 | + |
| 36 | + // Load and validate YAML-only files |
| 37 | + const wizards = filePaths.map(fp => { |
| 38 | + if (!/\.(yml|yaml)$/i.test(fp)) |
| 39 | + throw new Error("Only YAML format supported for setup-wizard: " + fp); |
| 40 | + const data = readFile(fp); |
| 41 | + const parsed = yaml.load(data); |
| 42 | + if (!parsed || typeof parsed === "string") |
| 43 | + throw new Error(`Could not parse setup-wizard: ${fp}`); |
| 44 | + return parsed as SetupWizard; |
| 45 | + }); |
| 46 | + |
| 47 | + // Merge all specs |
| 48 | + return merge({}, ...wizards); |
20 | 49 | } catch (e) { |
21 | | - throw Error(`Error parsing setup-wizard: ${e.message}`); |
| 50 | + throw new Error(`Error parsing setup-wizard: ${e.message}`); |
22 | 51 | } |
23 | 52 | } |
| 53 | + |
| 54 | +// Find a setup-wizard file, throws if not found |
| 55 | +function findSetupWizardPath(spec?: SetupWizardPaths): string { |
| 56 | + const dirPath = spec?.dir || defaultDir; |
| 57 | + if (spec?.setupWizardFileName) |
| 58 | + return path.join(dirPath, spec.setupWizardFileName); |
| 59 | + const files: string[] = fs.readdirSync(dirPath); |
| 60 | + const match = files.find(f => releaseFiles.setupWizard.regex.test(f)); |
| 61 | + if (!match) |
| 62 | + throw new Error(`No setup-wizard file found in directory ${dirPath}`); |
| 63 | + return path.join(dirPath, match); |
| 64 | +} |
0 commit comments