|
1 | 1 | import fs from 'fs/promises' |
| 2 | +import path from 'node:path' |
2 | 3 |
|
3 | 4 | import YAML from 'yaml' |
4 | 5 |
|
5 | 6 | /** |
6 | 7 | * FileFormService abstract class |
7 | 8 | */ |
8 | | -class FileFormService { |
| 9 | +export class FileFormService { |
9 | 10 | /** |
10 | 11 | * The map of form metadatas by slug |
11 | 12 | * @type {Map<string, FormMetadata>} |
@@ -38,11 +39,47 @@ class FileFormService { |
38 | 39 | * @param {string} filepath - the file path |
39 | 40 | * @returns {Promise<FormDefinition>} |
40 | 41 | */ |
41 | | - // eslint-disable-next-line @typescript-eslint/require-await |
42 | 42 | async readForm(filepath) { |
43 | | - throw new Error( |
44 | | - `Error reading path '${filepath}'. 'readForm' not implemented in abstract class` |
45 | | - ) |
| 43 | + const ext = path.extname(filepath).toLowerCase() |
| 44 | + |
| 45 | + switch (ext) { |
| 46 | + case '.json': |
| 47 | + return this.readJsonForm(filepath) |
| 48 | + case '.yaml': |
| 49 | + return this.readYamlForm(filepath) |
| 50 | + default: |
| 51 | + throw new Error(`Invalid file extension '${ext}'`) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Read the form definition from a json file |
| 57 | + * @param {string} filepath |
| 58 | + * @returns {Promise<FormDefinition>} |
| 59 | + */ |
| 60 | + async readJsonForm(filepath) { |
| 61 | + /** |
| 62 | + * @type {FormDefinition} |
| 63 | + */ |
| 64 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
| 65 | + const definition = JSON.parse(await fs.readFile(filepath, 'utf8')) |
| 66 | + |
| 67 | + return definition |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Read the form definition from a yaml file |
| 72 | + * @param {string} filepath |
| 73 | + * @returns {Promise<FormDefinition>} |
| 74 | + */ |
| 75 | + async readYamlForm(filepath) { |
| 76 | + /** |
| 77 | + * @type {FormDefinition} |
| 78 | + */ |
| 79 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
| 80 | + const definition = YAML.parse(await fs.readFile(filepath, 'utf8')) |
| 81 | + |
| 82 | + return definition |
46 | 83 | } |
47 | 84 |
|
48 | 85 | /** |
@@ -102,48 +139,6 @@ class FileFormService { |
102 | 139 | } |
103 | 140 | } |
104 | 141 |
|
105 | | -/** |
106 | | - * JsonFileFormService class |
107 | | - * @augments FileFormService |
108 | | - */ |
109 | | -export class JsonFileFormService extends FileFormService { |
110 | | - /** |
111 | | - * Read the form definition from a json file |
112 | | - * @param {string} filepath |
113 | | - * @returns {Promise<FormDefinition>} |
114 | | - */ |
115 | | - async readForm(filepath) { |
116 | | - /** |
117 | | - * @type {FormDefinition} |
118 | | - */ |
119 | | - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
120 | | - const definition = JSON.parse(await fs.readFile(filepath, 'utf8')) |
121 | | - |
122 | | - return definition |
123 | | - } |
124 | | -} |
125 | | - |
126 | | -/** |
127 | | - * YamlFileFormService class |
128 | | - * @augments FileFormService |
129 | | - */ |
130 | | -export class YamlFileFormService extends FileFormService { |
131 | | - /** |
132 | | - * Read the form definition from a yaml file |
133 | | - * @param {string} filepath |
134 | | - * @returns {Promise<FormDefinition>} |
135 | | - */ |
136 | | - async readForm(filepath) { |
137 | | - /** |
138 | | - * @type {FormDefinition} |
139 | | - */ |
140 | | - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
141 | | - const definition = YAML.parse(await fs.readFile(filepath, 'utf8')) |
142 | | - |
143 | | - return definition |
144 | | - } |
145 | | -} |
146 | | - |
147 | 142 | /** |
148 | 143 | * @import { FormMetadata, FormDefinition } from '@defra/forms-model' |
149 | 144 | */ |
0 commit comments